Skip to content

Commit 2fa2ea6

Browse files
author
Gregory Fernandez
committed
Requested fields on every ancestor type are now taken into account.
1 parent 4d4e10c commit 2fa2ea6

File tree

9 files changed

+86
-53
lines changed

9 files changed

+86
-53
lines changed

crnk-core/src/main/java/io/crnk/core/engine/internal/document/mapper/DocumentMapperUtil.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ public DocumentMapperUtil(ResourceRegistry resourceRegistry, ObjectMapper object
6161
serializerUtil = new SerializerUtil(serializeLinksAsObjects);
6262
}
6363

64-
protected static List<ResourceField> getRequestedFields(ResourceInformation resourceInformation, QueryAdapter queryAdapter,
64+
protected List<ResourceField> getRequestedFields(ResourceInformation resourceInformation, QueryAdapter queryAdapter,
6565
List<ResourceField> fields, boolean relation) {
6666
Map<String, Set<PathSpec>> includedFieldsSet = queryAdapter != null ? queryAdapter.getIncludedFields() : null;
6767
final Set<PathSpec> includedFields = new HashSet<>();
6868

69-
if (includedFieldsSet != null) {
70-
addIfNotNull(includedFields, includedFieldsSet.get(resourceInformation.getResourceType()));
71-
addIfNotNull(includedFields, includedFieldsSet.get(resourceInformation.getSuperResourceType()));
69+
RegistryEntry entry = resourceRegistry.getEntry(resourceInformation.getResourceType());
70+
while(entry != null){
71+
addIfNotNull(includedFields, includedFieldsSet.get(entry.getResourceInformation().getResourceType()));
72+
entry = entry.getParentRegistryEntry();
7273
}
7374
if (noResourceIncludedFieldsSpecified(includedFields)) {
7475
return fields;

crnk-core/src/main/java/io/crnk/core/engine/internal/document/mapper/ResourceMapper.java

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

202201
protected void setRelationships(Resource resource, Object entity, ResourceInformation resourceInformation,
203202
QueryAdapter queryAdapter, ResourceMappingConfig mappingConfig) {
204-
List<ResourceField> fields = DocumentMapperUtil
205-
.getRequestedFields(resourceInformation, queryAdapter, resourceInformation.getRelationshipFields(), true);
203+
List<ResourceField> fields = util.getRequestedFields(resourceInformation, queryAdapter, resourceInformation.getRelationshipFields(), true);
206204
QueryContext queryContext = queryAdapter.getQueryContext();
207205
for (ResourceField field : fields) {
208206
if (!isIgnored(field, queryContext)) {

crnk-core/src/test/java/io/crnk/core/CoreTestModule.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import io.crnk.core.mock.repository.RelationIdTestRepository;
1515
import io.crnk.core.mock.repository.RelationshipBehaviorTestRepository;
1616
import io.crnk.core.mock.repository.ScheduleRepositoryImpl;
17-
import io.crnk.core.mock.repository.SuperTaskRepository;
17+
import io.crnk.core.mock.repository.TopTaskRepository;
1818
import io.crnk.core.mock.repository.TaskRepository;
1919
import io.crnk.core.mock.repository.TaskToProjectRepository;
2020
import io.crnk.core.mock.repository.TaskWithLookupRepository;
@@ -39,12 +39,12 @@ public void setupModule(ModuleContext context) {
3939
context.addRepository(new ProjectToTaskRepository());
4040
context.addRepository(new ProjectPolymorphicRepository());
4141
context.addRepository(new ScheduleRepositoryImpl());
42-
context.addRepository(new SuperTaskRepository());
4342
context.addRepository(new ThingRepository());
4443
context.addRepository(new TaskRepository());
4544
context.addRepository(new TaskToProjectRepository());
4645
context.addRepository(new TaskWithLookupRepository());
4746
context.addRepository(new TaskWithLookupToProjectRepository());
47+
context.addRepository(new TopTaskRepository());
4848
context.addRepository(new UserRepository());
4949
context.addRepository(new UserToProjectRepository());
5050
context.addRepository(new UserToTaskRepository());

crnk-core/src/test/java/io/crnk/core/engine/internal/document/mapper/DocumentMapperTest.java

+27-12
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@
1616
import com.fasterxml.jackson.databind.JsonNode;
1717
import com.fasterxml.jackson.databind.ObjectWriter;
1818
import com.fasterxml.jackson.databind.node.ObjectNode;
19+
1920
import io.crnk.core.engine.document.Document;
2021
import io.crnk.core.engine.document.ErrorData;
2122
import io.crnk.core.engine.document.Relationship;
2223
import io.crnk.core.engine.document.Resource;
2324
import io.crnk.core.engine.document.ResourceIdentifier;
2425
import io.crnk.core.engine.query.QueryAdapter;
26+
import io.crnk.core.mock.models.BottomTask;
2527
import io.crnk.core.mock.models.LazyTask;
28+
import io.crnk.core.mock.models.MiddleTask;
2629
import io.crnk.core.mock.models.Project;
2730
import io.crnk.core.mock.models.Schedule;
28-
import io.crnk.core.mock.models.SpecialTask;
29-
import io.crnk.core.mock.models.SuperTask;
3031
import io.crnk.core.mock.models.Task;
32+
import io.crnk.core.mock.models.TopTask;
3133
import io.crnk.core.queryspec.PathSpec;
3234
import io.crnk.core.queryspec.QuerySpec;
3335
import io.crnk.core.queryspec.internal.QuerySpecAdapter;
@@ -43,6 +45,7 @@
4345
import org.junit.Test;
4446
import org.mockito.Mockito;
4547

48+
4649
public class DocumentMapperTest extends AbstractDocumentMapperTest {
4750

4851
@Test
@@ -650,25 +653,37 @@ public void testAttributesSelection() {
650653

651654
@Test
652655
public void testSupertypeAttributesSelection() {
653-
SpecialTask task = createSpecialTask(2, "sample task");
656+
BottomTask task = createTreeTask(2, "sample task");
654657
task.setEnd("next month");
658+
task.setRecurring(true);
659+
task.setPublicComment("public");
660+
task.setPrivateComment("private");
661+
655662
JsonApiResponse response = new JsonApiResponse();
656663
response.setEntity(task);
657664

658-
QuerySpec querySpec = new QuerySpec(SpecialTask.class);
659-
querySpec.includeField(PathSpec.of("end"));
660-
final QuerySpec superQuerySpec = new QuerySpec(SuperTask.class);
661-
superQuerySpec.includeField(PathSpec.of("name"));
662-
querySpec.setNestedSpecs(Collections.singletonList(superQuerySpec));
665+
QuerySpec bottomQuerySpec = new QuerySpec(BottomTask.class);
666+
bottomQuerySpec.includeField(PathSpec.of("end"));
667+
QuerySpec middleQuerySpec = new QuerySpec(MiddleTask.class);
668+
middleQuerySpec.includeField(PathSpec.of("publicComment"));
669+
final QuerySpec topQuerySpec = new QuerySpec(TopTask.class);
670+
topQuerySpec.includeField(PathSpec.of("name"));
663671

664-
Document document = mapper.toDocument(response, toAdapter(querySpec), mappingConfig).get();
672+
bottomQuerySpec.setNestedSpecs(Arrays.asList(topQuerySpec, middleQuerySpec));
673+
674+
Document document = mapper.toDocument(response, toAdapter(bottomQuerySpec), mappingConfig).get();
665675
Resource resource = document.getSingleData().get();
666676
Assert.assertEquals("2", resource.getId());
667-
Assert.assertEquals("specialTask", resource.getType());
677+
Assert.assertEquals("bottomTask", resource.getType());
668678
Assert.assertNull(resource.getAttributes().get("category"));
669679
Assert.assertNull(resource.getAttributes().get("recurring"));
680+
Assert.assertNull(resource.getAttributes().get("privateComment"));
681+
Assert.assertNotNull(resource.getAttributes().get("name"));
682+
Assert.assertNotNull(resource.getAttributes().get("end"));
683+
Assert.assertNotNull(resource.getAttributes().get("publicComment"));
670684
Assert.assertEquals("sample task", resource.getAttributes().get("name").asText());
671685
Assert.assertEquals("next month", resource.getAttributes().get("end").asText());
686+
Assert.assertEquals("public", resource.getAttributes().get("publicComment").asText());
672687
}
673688

674689
@Test
@@ -713,8 +728,8 @@ private LazyTask createLazyTask(long id) {
713728
return task;
714729
}
715730

716-
private SpecialTask createSpecialTask(long id, String name) {
717-
SpecialTask task = new SpecialTask();
731+
private BottomTask createTreeTask(long id, String name) {
732+
BottomTask task = new BottomTask();
718733
task.setId(id);
719734
task.setName(name);
720735
return task;

crnk-core/src/test/java/io/crnk/core/mock/models/SpecialTask.java renamed to crnk-core/src/test/java/io/crnk/core/mock/models/BottomTask.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

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

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

88
private boolean recurring;
99

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.crnk.core.mock.models;
2+
3+
import io.crnk.core.resource.annotations.JsonApiResource;
4+
5+
@JsonApiResource(type = "middleTask", subTypes = BottomTask.class, resourcePath = "treeTasks")
6+
public abstract class MiddleTask extends TopTask {
7+
8+
private String publicComment;
9+
10+
private String privateComment;
11+
12+
public String getPublicComment() {
13+
return publicComment;
14+
}
15+
16+
public void setPublicComment(final String publicComment) {
17+
this.publicComment = publicComment;
18+
}
19+
20+
public String getPrivateComment() {
21+
return privateComment;
22+
}
23+
24+
public void setPrivateComment(final String privateComment) {
25+
this.privateComment = privateComment;
26+
}
27+
}

crnk-core/src/test/java/io/crnk/core/mock/models/SuperTask.java renamed to crnk-core/src/test/java/io/crnk/core/mock/models/TopTask.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import io.crnk.core.resource.annotations.JsonApiId;
44
import io.crnk.core.resource.annotations.JsonApiResource;
55

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

99
@JsonApiId
1010
private Long id;
@@ -17,7 +17,7 @@ public Long getId() {
1717
return id;
1818
}
1919

20-
public SuperTask setId(Long id) {
20+
public TopTask setId(Long id) {
2121
this.id = id;
2222
return this;
2323
}

crnk-core/src/test/java/io/crnk/core/mock/repository/SuperTaskRepository.java

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.crnk.core.mock.repository;
2+
3+
import io.crnk.core.mock.models.TopTask;
4+
import io.crnk.core.queryspec.QuerySpec;
5+
import io.crnk.core.repository.ReadOnlyResourceRepositoryBase;
6+
import io.crnk.core.resource.list.ResourceList;
7+
8+
public class TopTaskRepository extends ReadOnlyResourceRepositoryBase<TopTask, Long> {
9+
10+
public TopTaskRepository() {
11+
super(TopTask.class);
12+
}
13+
14+
@Override
15+
public ResourceList<TopTask> findAll(final QuerySpec querySpec) {
16+
return null;
17+
}
18+
}

0 commit comments

Comments
 (0)