use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiPatch method handleReplaceOp.
/**
* Replace data via patch extension.
*/
private Supplier<Pair<Integer, JsonNode>> handleReplaceOp(String path, JsonNode patchVal, PatchRequestScope requestScope, PatchAction action) {
try {
JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchVal);
if (!path.contains("relationships")) {
// Reserved
Data<Resource> data = value.getData();
Collection<Resource> resources = data.get();
// Defer relationship updating until the end
getSingleResource(resources).setRelationships(null);
// Reparse since we mangle it first
action.doc = requestScope.getMapper().readJsonApiPatchExtValue(patchVal);
action.path = path;
action.isPostProcessing = true;
}
// Defer relationship updating until the end
PatchVisitor visitor = new PatchVisitor(new PatchRequestScope(path, value, requestScope));
return visitor.visit(JsonApiParser.parse(path));
} catch (IOException e) {
throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchVal);
}
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiPatch method handleAddOp.
/**
* Add a document via patch extension.
*/
private Supplier<Pair<Integer, JsonNode>> handleAddOp(String path, JsonNode patchValue, PatchRequestScope requestScope, PatchAction action) {
try {
JsonApiDocument value = requestScope.getMapper().readJsonApiPatchExtValue(patchValue);
Data<Resource> data = value.getData();
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 (StringUtils.isEmpty(id)) {
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);
// Reparse since we mangle it first
action.doc = requestScope.getMapper().readJsonApiPatchExtValue(patchValue);
action.path = fullPath;
action.isPostProcessing = true;
}
PostVisitor visitor = new PostVisitor(new PatchRequestScope(path, value, requestScope));
return visitor.visit(JsonApiParser.parse(path));
} catch (HttpStatusException e) {
action.cause = e;
throw e;
} catch (IOException e) {
throw new InvalidEntityBodyException("Could not parse patch extension value: " + patchValue);
}
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class AbstractHibernateTestService method assertEqualDocuments.
protected void assertEqualDocuments(String actual, String expected) {
try {
JsonApiDocument expectedDoc = jsonApiMapper.readJsonApiDocument(expected);
JsonApiDocument actualDoc = jsonApiMapper.readJsonApiDocument(actual);
assertEquals(expectedDoc, actualDoc, "\n" + actual + "\n" + expected + "\n");
} catch (IOException e) {
fail("\n" + actual + "\n" + expected + "\n", e);
}
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiTest method readListIncluded.
@Test
public void readListIncluded() throws IOException {
String doc = "{\"data\":[{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"links\":{\"self\":\"/parent/123/relationships/child\",\"related\":\"/parent/123/child\"},\"data\":{\"type\":\"child\",\"id\":\"2\"}}}}],\"included\":[{\"type\":\"child\",\"id\":\"2\",\"relationships\":{\"parents\":{\"links\":{\"self\":\"/parent/123/relationships/child\",\"related\":\"/parent/123/child\"},\"data\":{\"type\":\"parent\",\"id\":\"123\"}}}}]}";
JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
Data<Resource> list = jsonApiDocument.getData();
Resource data = list.get().iterator().next();
Map<String, Object> attributes = data.getAttributes();
List<Resource> included = jsonApiDocument.getIncluded();
Resource includedChild = IterableUtils.first(included);
ResourceIdentifier parent = includedChild.getRelationships().get("parents").getResourceIdentifierData().getSingleValue();
assertEquals("parent", data.getType());
assertEquals("123", data.getId());
assertEquals("bob", attributes.get("firstName"));
assertEquals("child", includedChild.getType());
assertEquals("2", includedChild.getId());
assertEquals("123", parent.getId());
checkEquality(jsonApiDocument);
}
use of com.yahoo.elide.jsonapi.models.JsonApiDocument in project elide by yahoo.
the class JsonApiTest method checkEquality.
private void checkEquality(JsonApiDocument doc1) {
JsonApiDocument doc2;
try {
String json = mapper.writeJsonApiDocument(doc1);
doc2 = mapper.readJsonApiDocument(json);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
assertEquals(doc1, doc2);
assertEquals(doc1.hashCode(), doc2.hashCode());
}
Aggregations