Skip to content

Commit

Permalink
Requested fields on super resource type are now taken into account.
Browse files Browse the repository at this point in the history
* Update DocumentMapperUtil.getRequestedFields to read requested fields
  on super type.
* Add test case.
  • Loading branch information
Gregory Fernandez committed Sep 7, 2022
1 parent 7164f37 commit 3466bf5
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 3 deletions.
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 @@ -63,7 +64,12 @@ public DocumentMapperUtil(ResourceRegistry resourceRegistry, ObjectMapper object
protected static 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<>();

if (includedFieldsSet != null) {
addIfNotNull(includedFields, includedFieldsSet.get(resourceInformation.getResourceType()));
addIfNotNull(includedFields, includedFieldsSet.get(resourceInformation.getSuperResourceType()));
}
if (noResourceIncludedFieldsSpecified(includedFields)) {
return fields;
}
Expand All @@ -72,6 +78,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
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.SuperTaskRepository;
import io.crnk.core.mock.repository.TaskRepository;
import io.crnk.core.mock.repository.TaskToProjectRepository;
import io.crnk.core.mock.repository.TaskWithLookupRepository;
Expand All @@ -38,6 +39,7 @@ 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());
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,7 +16,6 @@
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;
Expand All @@ -27,6 +25,8 @@
import io.crnk.core.mock.models.LazyTask;
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.queryspec.PathSpec;
import io.crnk.core.queryspec.QuerySpec;
Expand Down Expand Up @@ -648,6 +648,29 @@ public void testAttributesSelection() {
Assert.assertEquals("sample category", resource.getAttributes().get("category").asText());
}

@Test
public void testSupertypeAttributesSelection() {
SpecialTask task = createSpecialTask(2, "sample task");
task.setEnd("next month");
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));

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

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

private SpecialTask createSpecialTask(long id, String name) {
SpecialTask task = new SpecialTask();
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/SpecialTask.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 = "specialTask", resourcePath = "superTasks")
public class SpecialTask extends SuperTask {

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;
}
}
40 changes: 40 additions & 0 deletions crnk-core/src/test/java/io/crnk/core/mock/models/SuperTask.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 = "superTasks", subTypes = SpecialTask.class)
public abstract class SuperTask {

@JsonApiId
private Long id;

private String name;

private String category;

public Long getId() {
return id;
}

public SuperTask 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,26 @@
package io.crnk.core.mock.repository;

import java.util.HashSet;
import java.util.Set;

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

public class SuperTaskRepository extends ReadOnlyResourceRepositoryBase<SuperTask, Long> {

private Set<SpecialTask> tasks = new HashSet<>();

private long nextId = 0;

public SuperTaskRepository() {
super(SuperTask.class);
}

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

0 comments on commit 3466bf5

Please sign in to comment.