Search in sources :

Example 41 with Resource

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));
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) DocumentProcessor(com.yahoo.elide.jsonapi.document.processors.DocumentProcessor) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) ArrayList(java.util.ArrayList) Data(com.yahoo.elide.jsonapi.models.Data) RequestScope(com.yahoo.elide.core.RequestScope) Relationship(com.yahoo.elide.jsonapi.models.Relationship) JsonApiMapper(com.yahoo.elide.jsonapi.JsonApiMapper) IncludedProcessor(com.yahoo.elide.jsonapi.document.processors.IncludedProcessor)

Example 42 with Resource

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);
    }
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PatchVisitor(com.yahoo.elide.jsonapi.parser.PatchVisitor) IOException(java.io.IOException)

Example 43 with Resource

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);
    }
}
Also used : InvalidEntityBodyException(com.yahoo.elide.core.exceptions.InvalidEntityBodyException) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) HttpStatusException(com.yahoo.elide.core.exceptions.HttpStatusException) IOException(java.io.IOException) PostVisitor(com.yahoo.elide.jsonapi.parser.PostVisitor)

Example 44 with Resource

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);
}
Also used : ResourceIdentifier(com.yahoo.elide.jsonapi.models.ResourceIdentifier) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Example 45 with Resource

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);
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) Relationship(com.yahoo.elide.jsonapi.models.Relationship) Resource(com.yahoo.elide.jsonapi.models.Resource) PersistentResource(com.yahoo.elide.core.PersistentResource) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (com.yahoo.elide.jsonapi.models.Resource)55 Test (org.junit.jupiter.api.Test)35 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)31 PersistentResource (com.yahoo.elide.core.PersistentResource)28 Relationship (com.yahoo.elide.jsonapi.models.Relationship)21 ArrayList (java.util.ArrayList)17 ResourceIdentifier (com.yahoo.elide.jsonapi.models.ResourceIdentifier)16 PatchRequestScope (com.yahoo.elide.jsonapi.extensions.PatchRequestScope)13 Data (com.yahoo.elide.jsonapi.models.Data)13 InvalidEntityBodyException (com.yahoo.elide.core.exceptions.InvalidEntityBodyException)9 LinkedHashSet (java.util.LinkedHashSet)9 Parent (example.Parent)8 HashSet (java.util.HashSet)8 EntityProjection (com.yahoo.elide.core.request.EntityProjection)7 Child (example.Child)7 LinkedHashMap (java.util.LinkedHashMap)7 RequestScope (com.yahoo.elide.core.RequestScope)6 TestUser (com.yahoo.elide.core.security.TestUser)6 User (com.yahoo.elide.core.security.User)6 TestRequestScope (com.yahoo.elide.core.TestRequestScope)5