use of com.yahoo.elide.jsonapi.models.Resource in project elide by yahoo.
the class RelationshipTerminalState method handleGet.
@Override
public Supplier<Pair<Integer, JsonNode>> handleGet(StateContext state) {
JsonApiDocument doc = new JsonApiDocument();
RequestScope requestScope = state.getRequestScope();
JsonApiMapper mapper = requestScope.getMapper();
MultivaluedMap<String, String> queryParams = requestScope.getQueryParams();
Map<String, Relationship> relationships = record.toResource(parentProjection).getRelationships();
if (relationships != null && relationships.containsKey(relationshipName)) {
Relationship relationship = relationships.get(relationshipName);
// Handle valid relationship
// Set data
Data<Resource> data = relationship.getData();
doc.setData(data);
// Run include processor
DocumentProcessor includedProcessor = new IncludedProcessor();
includedProcessor.execute(doc, record, queryParams);
return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
}
// Handle no data for relationship
if (relationshipType.isToMany()) {
doc.setData(new Data<>(new ArrayList<>()));
} else if (relationshipType.isToOne()) {
doc.setData(new Data<>((Resource) null));
} else {
throw new IllegalStateException("Failed to GET a relationship; relationship is neither toMany nor toOne");
}
return () -> Pair.of(HttpStatus.SC_OK, mapper.toJsonObject(doc));
}
use of com.yahoo.elide.jsonapi.models.Resource 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.Resource 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.Resource 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.Resource in project elide by yahoo.
the class JsonApiTest method readSingle.
@Test
public void readSingle() throws IOException {
String doc = "{\"data\":{\"type\":\"parent\",\"id\":\"123\",\"attributes\":{\"firstName\":\"bob\"},\"relationships\":{\"children\":{\"data\":{\"type\":\"child\",\"id\":\"2\"}}}}}";
JsonApiDocument jsonApiDocument = mapper.readJsonApiDocument(doc);
Data<Resource> dataObj = jsonApiDocument.getData();
Resource data = dataObj.getSingleValue();
Map<String, Object> attributes = data.getAttributes();
Map<String, Relationship> relations = data.getRelationships();
assertEquals("parent", data.getType());
assertEquals("123", data.getId());
assertEquals("bob", attributes.get("firstName"));
assertEquals("child", relations.get("children").getData().getSingleValue().getType());
assertEquals("2", relations.get("children").getData().getSingleValue().getId());
checkEquality(jsonApiDocument);
}
Aggregations