Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requested fields on super resource type are now taken into account. #853

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.crnk.core.boot.CrnkProperties;
import io.crnk.core.engine.document.Resource;
import io.crnk.core.engine.document.ResourceIdentifier;
Expand Down Expand Up @@ -60,10 +61,16 @@ public DocumentMapperUtil(ResourceRegistry resourceRegistry, ObjectMapper object
serializerUtil = new SerializerUtil(serializeLinksAsObjects);
}

protected static List<ResourceField> getRequestedFields(ResourceInformation resourceInformation, QueryAdapter queryAdapter,
protected List<ResourceField> getRequestedFields(ResourceInformation resourceInformation, QueryAdapter queryAdapter,
List<ResourceField> fields, boolean relation) {
Map<String, Set<PathSpec>> includedFieldsSet = queryAdapter != null ? queryAdapter.getIncludedFields() : null;
Set<PathSpec> includedFields = includedFieldsSet != null ? includedFieldsSet.get(resourceInformation.getResourceType()) : null;
final Set<PathSpec> includedFields = new HashSet<>();

RegistryEntry entry = resourceRegistry.getEntry(resourceInformation.getResourceType());
while(entry != null){
addIfNotNull(includedFields, includedFieldsSet.get(entry.getResourceInformation().getResourceType()));
entry = entry.getParentRegistryEntry();
}
if (noResourceIncludedFieldsSpecified(includedFields)) {
return fields;
}
Expand All @@ -72,6 +79,12 @@ protected static List<ResourceField> getRequestedFields(ResourceInformation reso
}
}

private static <T> void addIfNotNull(Set<T> set, Collection<T> collection) {
if (collection != null) {
set.addAll(collection);
}
}

private static List<ResourceField> computeRequestedFields(Set<PathSpec> includedFields, boolean relation,
QueryAdapter queryAdapter, ResourceInformation resourceInformation, List<ResourceField> fields) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public LinksInformation getResourceLinks(Object entity, ResourceInformation reso
protected void setAttributes(Resource resource, Object entity, ResourceInformation resourceInformation,
QueryAdapter queryAdapter, ResourceMappingConfig mappingConfig) {
// fields legacy may further limit the number of fields
List<ResourceField> fields = DocumentMapperUtil
.getRequestedFields(resourceInformation, queryAdapter, resourceInformation.getAttributeFields(), false);
List<ResourceField> fields = util.getRequestedFields(resourceInformation, queryAdapter, resourceInformation.getAttributeFields(), false);
// serialize the individual attributes
QueryContext queryContext = queryAdapter.getQueryContext();
for (ResourceField field : fields) {
Expand Down Expand Up @@ -201,8 +200,7 @@ private boolean isValueIncluded(ResourceField field, Object value) {

protected void setRelationships(Resource resource, Object entity, ResourceInformation resourceInformation,
QueryAdapter queryAdapter, ResourceMappingConfig mappingConfig) {
List<ResourceField> fields = DocumentMapperUtil
.getRequestedFields(resourceInformation, queryAdapter, resourceInformation.getRelationshipFields(), true);
List<ResourceField> fields = util.getRequestedFields(resourceInformation, queryAdapter, resourceInformation.getRelationshipFields(), true);
QueryContext queryContext = queryAdapter.getQueryContext();
for (ResourceField field : fields) {
if (!isIgnored(field, queryContext)) {
Expand Down
2 changes: 2 additions & 0 deletions crnk-core/src/test/java/io/crnk/core/CoreTestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.crnk.core.mock.repository.RelationIdTestRepository;
import io.crnk.core.mock.repository.RelationshipBehaviorTestRepository;
import io.crnk.core.mock.repository.ScheduleRepositoryImpl;
import io.crnk.core.mock.repository.TopTaskRepository;
import io.crnk.core.mock.repository.TaskRepository;
import io.crnk.core.mock.repository.TaskToProjectRepository;
import io.crnk.core.mock.repository.TaskWithLookupRepository;
Expand Down Expand Up @@ -43,6 +44,7 @@ public void setupModule(ModuleContext context) {
context.addRepository(new TaskToProjectRepository());
context.addRepository(new TaskWithLookupRepository());
context.addRepository(new TaskWithLookupToProjectRepository());
context.addRepository(new TopTaskRepository());
context.addRepository(new UserRepository());
context.addRepository(new UserToProjectRepository());
context.addRepository(new UserToTaskRepository());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.crnk.core.engine.internal.document.mapper;

import java.net.URI;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -17,17 +16,20 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;

import io.crnk.core.engine.document.Document;
import io.crnk.core.engine.document.ErrorData;
import io.crnk.core.engine.document.Relationship;
import io.crnk.core.engine.document.Resource;
import io.crnk.core.engine.document.ResourceIdentifier;
import io.crnk.core.engine.query.QueryAdapter;
import io.crnk.core.mock.models.BottomTask;
import io.crnk.core.mock.models.LazyTask;
import io.crnk.core.mock.models.MiddleTask;
import io.crnk.core.mock.models.Project;
import io.crnk.core.mock.models.Schedule;
import io.crnk.core.mock.models.Task;
import io.crnk.core.mock.models.TopTask;
import io.crnk.core.queryspec.PathSpec;
import io.crnk.core.queryspec.QuerySpec;
import io.crnk.core.queryspec.internal.QuerySpecAdapter;
Expand All @@ -43,6 +45,7 @@
import org.junit.Test;
import org.mockito.Mockito;


public class DocumentMapperTest extends AbstractDocumentMapperTest {

@Test
Expand Down Expand Up @@ -648,6 +651,41 @@ public void testAttributesSelection() {
Assert.assertEquals("sample category", resource.getAttributes().get("category").asText());
}

@Test
public void testSupertypeAttributesSelection() {
BottomTask task = createTreeTask(2, "sample task");
task.setEnd("next month");
task.setRecurring(true);
task.setPublicComment("public");
task.setPrivateComment("private");

JsonApiResponse response = new JsonApiResponse();
response.setEntity(task);

QuerySpec bottomQuerySpec = new QuerySpec(BottomTask.class);
bottomQuerySpec.includeField(PathSpec.of("end"));
QuerySpec middleQuerySpec = new QuerySpec(MiddleTask.class);
middleQuerySpec.includeField(PathSpec.of("publicComment"));
final QuerySpec topQuerySpec = new QuerySpec(TopTask.class);
topQuerySpec.includeField(PathSpec.of("name"));

bottomQuerySpec.setNestedSpecs(Arrays.asList(topQuerySpec, middleQuerySpec));

Document document = mapper.toDocument(response, toAdapter(bottomQuerySpec), mappingConfig).get();
Resource resource = document.getSingleData().get();
Assert.assertEquals("2", resource.getId());
Assert.assertEquals("bottomTask", resource.getType());
Assert.assertNull(resource.getAttributes().get("category"));
Assert.assertNull(resource.getAttributes().get("recurring"));
Assert.assertNull(resource.getAttributes().get("privateComment"));
Assert.assertNotNull(resource.getAttributes().get("name"));
Assert.assertNotNull(resource.getAttributes().get("end"));
Assert.assertNotNull(resource.getAttributes().get("publicComment"));
Assert.assertEquals("sample task", resource.getAttributes().get("name").asText());
Assert.assertEquals("next month", resource.getAttributes().get("end").asText());
Assert.assertEquals("public", resource.getAttributes().get("publicComment").asText());
}

@Test
public void testAttributesOrdering() {
Task task = createTask(3, "sample task");
Expand Down Expand Up @@ -690,6 +728,13 @@ private LazyTask createLazyTask(long id) {
return task;
}

private BottomTask createTreeTask(long id, String name) {
BottomTask task = new BottomTask();
task.setId(id);
task.setName(name);
return task;
}

public static class TestLinksInformation implements LinksInformation {

public Link value;
Expand Down
27 changes: 27 additions & 0 deletions crnk-core/src/test/java/io/crnk/core/mock/models/BottomTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.crnk.core.mock.models;

import io.crnk.core.resource.annotations.JsonApiResource;

@JsonApiResource(type = "bottomTask", resourcePath = "treeTasks")
public class BottomTask extends MiddleTask {

private boolean recurring;

private String end;

public boolean isRecurring() {
return recurring;
}

public void setRecurring(final boolean recurring) {
this.recurring = recurring;
}

public String getEnd() {
return end;
}

public void setEnd(final String end) {
this.end = end;
}
}
27 changes: 27 additions & 0 deletions crnk-core/src/test/java/io/crnk/core/mock/models/MiddleTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.crnk.core.mock.models;

import io.crnk.core.resource.annotations.JsonApiResource;

@JsonApiResource(type = "middleTask", subTypes = BottomTask.class, resourcePath = "treeTasks")
public abstract class MiddleTask extends TopTask {

private String publicComment;

private String privateComment;

public String getPublicComment() {
return publicComment;
}

public void setPublicComment(final String publicComment) {
this.publicComment = publicComment;
}

public String getPrivateComment() {
return privateComment;
}

public void setPrivateComment(final String privateComment) {
this.privateComment = privateComment;
}
}
40 changes: 40 additions & 0 deletions crnk-core/src/test/java/io/crnk/core/mock/models/TopTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.crnk.core.mock.models;

import io.crnk.core.resource.annotations.JsonApiId;
import io.crnk.core.resource.annotations.JsonApiResource;

@JsonApiResource(type = "topTask", subTypes = MiddleTask.class, resourcePath = "treeTasks")
public abstract class TopTask {

@JsonApiId
private Long id;

private String name;

private String category;

public Long getId() {
return id;
}

public TopTask setId(Long id) {
this.id = id;
return this;
}

public String getName() {
return name;
}

public void setName(@SuppressWarnings("SameParameterValue") String name) {
this.name = name;
}

public String getCategory() {
return category;
}

public void setCategory(final String category) {
this.category = category;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.crnk.core.mock.repository;

import io.crnk.core.mock.models.TopTask;
import io.crnk.core.queryspec.QuerySpec;
import io.crnk.core.repository.ReadOnlyResourceRepositoryBase;
import io.crnk.core.resource.list.ResourceList;

public class TopTaskRepository extends ReadOnlyResourceRepositoryBase<TopTask, Long> {

public TopTaskRepository() {
super(TopTask.class);
}

@Override
public ResourceList<TopTask> findAll(final QuerySpec querySpec) {
return null;
}
}