Skip to content

Commit

Permalink
Requested fields on every ancestor type are now taken into account.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Fernandez committed Sep 12, 2022
1 parent 3466bf5 commit 947352d
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ 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;
final Set<PathSpec> includedFields = new HashSet<>();

if (includedFieldsSet != null) {
addIfNotNull(includedFields, includedFieldsSet.get(resourceInformation.getResourceType()));
addIfNotNull(includedFields, includedFieldsSet.get(resourceInformation.getSuperResourceType()));
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 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
4 changes: 2 additions & 2 deletions crnk-core/src/test/java/io/crnk/core/CoreTestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +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.SuperTaskRepository;
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 All @@ -39,12 +39,12 @@ public void setupModule(ModuleContext context) {
context.addRepository(new ProjectToTaskRepository());
context.addRepository(new ProjectPolymorphicRepository());
context.addRepository(new ScheduleRepositoryImpl());
context.addRepository(new SuperTaskRepository());
context.addRepository(new ThingRepository());
context.addRepository(new TaskRepository());
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
Expand Up @@ -16,18 +16,20 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.node.ObjectNode;

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.SpecialTask;
import io.crnk.core.mock.models.SuperTask;
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 @@ -650,25 +653,37 @@ public void testAttributesSelection() {

@Test
public void testSupertypeAttributesSelection() {
SpecialTask task = createSpecialTask(2, "sample task");
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 querySpec = new QuerySpec(SpecialTask.class);
querySpec.includeField(PathSpec.of("end"));
final QuerySpec superQuerySpec = new QuerySpec(SuperTask.class);
superQuerySpec.includeField(PathSpec.of("name"));
querySpec.setNestedSpecs(Collections.singletonList(superQuerySpec));
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"));

Document document = mapper.toDocument(response, toAdapter(querySpec), mappingConfig).get();
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("specialTask", resource.getType());
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
Expand Down Expand Up @@ -713,8 +728,8 @@ private LazyTask createLazyTask(long id) {
return task;
}

private SpecialTask createSpecialTask(long id, String name) {
SpecialTask task = new SpecialTask();
private BottomTask createTreeTask(long id, String name) {
BottomTask task = new BottomTask();
task.setId(id);
task.setName(name);
return task;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

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

private boolean recurring;

Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import io.crnk.core.resource.annotations.JsonApiId;
import io.crnk.core.resource.annotations.JsonApiResource;

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

@JsonApiId
private Long id;
Expand All @@ -17,7 +17,7 @@ public Long getId() {
return id;
}

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

This file was deleted.

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;
}
}

0 comments on commit 947352d

Please sign in to comment.