Skip to content

Commit

Permalink
Better errors for missing IDs in Patch Extension Request. (#1278)
Browse files Browse the repository at this point in the history
* Return a better error when handling invalid patch extension requests that are missing IDs

* Added tests

Co-authored-by: Aaron Klish <[email protected]>
  • Loading branch information
aklish and Aaron Klish authored Apr 24, 2020
1 parent 6a8da1b commit 10484b5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,15 @@ private Supplier<Pair<Integer, JsonNode>> handleAddOp(
if (data == null || data.get() == null) {
throw new InvalidEntityBodyException("Expected an entity body but received none.");
}

Collection<Resource> resources = data.get();
if (!path.contains("relationships")) { // Reserved key for relationships
String id = getSingleResource(resources).getId();

if (id == null || id.isEmpty()) {
throw new InvalidEntityBodyException("Patch extension requires all objects to have an assigned "
+ "ID (temporary or permanent) when assigning relationships.");
}
String fullPath = path + "/" + id;
// Defer relationship updating until the end
getSingleResource(resources).setRelationships(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,23 @@ public void createChildRelateExisting() {
.body(equalTo(expected));
}

@Test
public void invalidPatchMissingId() {
String request = jsonParser.getJson("/ResourceIT/invalidPatchMissingId.req.json");

String detail = "Bad Request Body'Patch extension requires all objects to have an assigned "
+ "ID (temporary or permanent) when assigning relationships.'";

given()
.contentType(JSONAPI_CONTENT_TYPE_WITH_JSON_PATCH_EXTENSION)
.accept(JSONAPI_CONTENT_TYPE_WITH_JSON_PATCH_EXTENSION)
.body(request)
.patch("/")
.then()
.statusCode(HttpStatus.SC_BAD_REQUEST)
.body("errors[0].detail", equalTo(detail));
}

@Test
public void updateChildRelationToExisting() {
String request = jsonParser.getJson("/ResourceIT/updateChildRelationToExisting.req.json");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"op":"add",
"path":"/book",
"value":{
"type":"book",
"relationships":{
"authors":{
"data":[
{
"type":"author",
"id":"authorId"
}
]
}
}
}
},
{
"op":"add",
"path":"/author",
"value":{
"id":"authorId",
"type":"author"
}
}
]

0 comments on commit 10484b5

Please sign in to comment.