Skip to content

Commit

Permalink
Fixing Spring JSON-API controller to allow JSON-API PATCH content type (
Browse files Browse the repository at this point in the history
#1258)

Co-authored-by: Aaron Klish <klish@verizonmedia.com>
  • Loading branch information
aklish and Aaron Klish authored Apr 11, 2020
1 parent 90a4f1e commit 1d18f9f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.yahoo.elide.spring.controllers;

import static com.yahoo.elide.Elide.JSONAPI_CONTENT_TYPE;
import static com.yahoo.elide.Elide.JSONAPI_CONTENT_TYPE_WITH_JSON_PATCH_EXTENSION;

import com.yahoo.elide.Elide;
import com.yahoo.elide.ElideResponse;
Expand Down Expand Up @@ -45,6 +46,7 @@ public class JsonApiController {
private final Elide elide;
private final ElideConfigProperties settings;
public static final String JSON_API_CONTENT_TYPE = JSONAPI_CONTENT_TYPE;
public static final String JSON_API_PATCH_CONTENT_TYPE = JSONAPI_CONTENT_TYPE_WITH_JSON_PATCH_EXTENSION;

@Autowired
public JsonApiController(Elide elide, ElideConfigProperties settings) {
Expand Down Expand Up @@ -72,13 +74,13 @@ public ResponseEntity<String> elidePost(@RequestBody String body,
return ResponseEntity.status(response.getResponseCode()).body(response.getBody());
}

@PatchMapping(value = "/**", consumes = JSON_API_CONTENT_TYPE)
@PatchMapping(value = "/**", consumes = { JSON_API_CONTENT_TYPE, JSON_API_PATCH_CONTENT_TYPE})
public ResponseEntity<String> elidePatch(@RequestBody String body,
HttpServletRequest request, Principal authentication) {
String pathname = getJsonApiPath(request, settings.getJsonApi().getPath());

ElideResponse response = elide
.patch(JSON_API_CONTENT_TYPE, JSON_API_CONTENT_TYPE, pathname, body, authentication);
.patch(request.getContentType(), request.getContentType(), pathname, body, authentication);
return ResponseEntity.status(response.getResponseCode()).body(response.getBody());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.datum;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.id;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.linkage;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.patchOperation;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.patchSet;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.relation;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.relationships;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.resource;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.type;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.elements.PatchOperationType.add;
import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.contains;
Expand Down Expand Up @@ -93,6 +96,7 @@ public void jsonApiPatchTest() {
.then()
.statusCode(HttpStatus.SC_NO_CONTENT);


when()
.get("/json/group")
.then()
Expand Down Expand Up @@ -137,6 +141,30 @@ public void jsonForbiddenApiPatchTest() {
.statusCode(HttpStatus.SC_FORBIDDEN);
}

@Test
public void jsonApiPatchExtensionTest() {
given()
.contentType(JsonApiController.JSON_API_PATCH_CONTENT_TYPE)
.accept(JsonApiController.JSON_API_PATCH_CONTENT_TYPE)
.body(
patchSet(
patchOperation(add, "/group",
resource(
type("group"),
id("com.example.repository.foo"),
attributes(
attr("commonName", "Foo")
)
)
)
)
)
.when()
.patch("/json")
.then()
.statusCode(HttpStatus.SC_OK);
}

@Test
public void jsonApiPostTest() {
given()
Expand Down

0 comments on commit 1d18f9f

Please sign in to comment.