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

Resolve nested mappings #129

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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 @@ -193,6 +193,8 @@ private Object getValueForField(
}
//Check to see if this is an explicitly mapped ContentItem
ContentItemMapping contentItemMapping = field.getAnnotation(ContentItemMapping.class);

// Modular content mapping - linked items element
if (contentItemMapping != null &&
isListOrMap(field.getType()) &&
item.getElements().containsKey(contentItemMapping.value()) &&
Expand All @@ -203,8 +205,9 @@ private Object getValueForField(
for (String codename : linkedItemElement.getValue()) {
referencedLinkedItems.put(codename, linkedItems.get(codename));
}
return getCastedLinkedItemsForListOrMap(bean, field, referencedLinkedItems);
return getCastedLinkedItemsForListOrMap(bean, field, referencedLinkedItems, linkedItems);
}
// Single Linked Item mapping
if (contentItemMapping != null && linkedItems.containsKey(contentItemMapping.value())) {
return getCastedLinkedItemsForField(field.getType(), contentItemMapping.value(), linkedItems);
}
Expand All @@ -222,7 +225,7 @@ private Object getValueForField(

//Check to see if this is a collection of implicitly mapped ContentItem
if (isListOrMap(field.getType())) {
return getCastedLinkedItemsForListOrMap(bean, field, linkedItems);
return getCastedLinkedItemsForListOrMap(bean, field, linkedItems, linkedItems);
}
return null;
}
Expand All @@ -239,15 +242,15 @@ private Object getCastedLinkedItemsForField(
}

private Object getCastedLinkedItemsForListOrMap(
Object bean, Field field, Map<String, ContentItem> linkedItems) {
Object bean, Field field, Map<String, ContentItem> referencedLinkedItems, Map<String, ContentItem> allLinkedItems) {
Type type = getType(bean, field);
if (type == null) {
// We have failed to get the type, probably due to a missing setter, skip this field
log.debug("Failed to get type from {} (probably due to a missing setter), {} skipped", bean, field);
return null;
}
if (type == ContentItem.class) {
return castCollection(field.getType(), linkedItems);
return castCollection(field.getType(), referencedLinkedItems);
}
Class<?> listClass = (Class<?>) type;
String contentType = null;
Expand All @@ -259,11 +262,12 @@ private Object getCastedLinkedItemsForListOrMap(
contentType = classNameToContentTypeMapping.get(listClass.getName());
}
if (contentType != null) {
HashMap convertedLinkedItems = new HashMap<>();
for (Map.Entry<String, ContentItem> entry : linkedItems.entrySet()) {
// This type preserves the order of the insertion, to have the same order as in delivery response
LinkedHashMap convertedLinkedItems = new LinkedHashMap();
for (Map.Entry<String, ContentItem> entry : referencedLinkedItems.entrySet()) {
if (entry.getValue() != null && contentType.equals(entry.getValue().getSystem().getType())) {
Map<String, ContentItem> linkedItemsForRecursion =
copyLinkedItemsWithExclusion(linkedItems, entry.getKey());
copyLinkedItemsWithExclusion(allLinkedItems, entry.getKey());
convertedLinkedItems.put(entry.getKey(),
convert(entry.getValue(), linkedItemsForRecursion, listClass));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public void testCustomHeadersPropagatedWithSdkIdHeader() throws Exception {
String projectId = "02a70003-e864-464e-b62c-e0ede97deb8c";
String previewApiKey = "preview_api_key";
List<Header> headers = Arrays.asList(
new Header("test-header-name1", "test-header-value1"),
new Header("test-header-name2", "test-header-value2")
new Header("test-header-name1", "test-header-value1"),
new Header("test-header-name2", "test-header-value2")
);

this.serverBootstrap.registerHandler(
Expand Down Expand Up @@ -150,7 +150,7 @@ public void testCustomHeadersDoNotOverwriteReservedHeaders() throws Exception {
Assert.assertNotEquals(headers.get(0).getValue(), request.getHeaders(headers.get(0).getName())[0].getValue());
Assert.assertNotEquals(headers.get(1).getValue(), request.getHeaders(headers.get(1).getName())[0].getValue());
Assert.assertNotEquals(headers.get(2).getValue(), request.getHeaders(headers.get(2).getName())[0].getValue());
Assert.assertArrayEquals(new Header[] {}, request.getHeaders(headers.get(3).getName()));
Assert.assertArrayEquals(new Header[]{}, request.getHeaders(headers.get(3).getName()));

response.setEntity(
new InputStreamEntity(
Expand Down Expand Up @@ -837,6 +837,47 @@ public void testGetStronglyTypedItem() throws Exception {
Assert.assertEquals(2, item.getAllLinkedItemsMap().size());
}

@Test
public void testGetStronglyTypedNestedItems() throws Exception {
String projectId = "bac6b90c-4f0d-01e9-a3d8-3bc0ec36c3e3";

this.serverBootstrap.registerHandler(
String.format("/%s/%s", projectId, "items/sample_page"),
(request, response, context) -> response.setEntity(
new InputStreamEntity(
this.getClass().getResourceAsStream("SampleNestedItemWithDepth2.json")
)
));
HttpHost httpHost = this.start();
DeliveryClient client = new DeliveryClient(projectId);

String testServerUri = httpHost.toURI();
client.getDeliveryOptions().setProductionEndpoint(testServerUri);

kentico.kontent.delivery.nestedmodels.Page item = client.getItem(
"sample_page",
kentico.kontent.delivery.nestedmodels.Page.class,
DeliveryParameterBuilder.params()
.linkedItemsDepth(2)
.build()
).toCompletableFuture()
.get();

Assert.assertNotNull(item);
Assert.assertEquals("Sample page",item.getTitle());
Assert.assertEquals(2, item.getSections().size());

Assert.assertEquals("Section 1", item.getSections().get(0).getHeadline());
Assert.assertEquals(2, item.getSections().get(0).getSectionParts().size());
Assert.assertEquals("Part 1", item.getSections().get(0).getSectionParts().get(0).getTitle());
Assert.assertEquals("Part 2", item.getSections().get(0).getSectionParts().get(1).getTitle());

Assert.assertEquals("Section 2", item.getSections().get(1).getHeadline());
Assert.assertEquals(2, item.getSections().get(1).getSectionParts().size());
Assert.assertEquals("Part A", item.getSections().get(1).getSectionParts().get(0).getTitle());
Assert.assertEquals("Part B", item.getSections().get(1).getSectionParts().get(1).getTitle());
}

@Test

public void securedAPI() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package kentico.kontent.delivery.nestedmodels;

import kentico.kontent.delivery.ContentItemMapping;
import kentico.kontent.delivery.ElementMapping;
import kentico.kontent.delivery.System;

import java.util.List;

@ContentItemMapping("page")
public class Page {
@ElementMapping("title")
String title;

// Custom strongly type mapping to List
@ContentItemMapping("sections")
List<Section> sections;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO:

  • - extend tests for Map/All linked items/... as is is in ListItem


System system;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public List<Section> getSections() {
return sections;
}

public void setSections(List<Section> sections) {
this.sections = sections;
}

public System getSystem() {
return system;
}

public void setSystem(System system) {
this.system = system;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package kentico.kontent.delivery.nestedmodels;

import kentico.kontent.delivery.ContentItemMapping;
import kentico.kontent.delivery.ElementMapping;
import kentico.kontent.delivery.System;

import java.util.List;

@ContentItemMapping("section")
public class Section {

// Custom strongly type mapping to List
@ContentItemMapping("section_parts")
List<SectionPart> sectionParts;

@ElementMapping("headline")
String headline;

System system;

public List<SectionPart> getSectionParts() {
return sectionParts;
}

public void setSectionParts(List<SectionPart> sectionParts) {
this.sectionParts = sectionParts;
}

public String getHeadline() {
return headline;
}

public void setHeadline(String headline) {
this.headline = headline;
}

public System getSystem() {
return system;
}

public void setSystem(System system) {
this.system = system;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package kentico.kontent.delivery.nestedmodels;

import kentico.kontent.delivery.ContentItemMapping;
import kentico.kontent.delivery.ElementMapping;
import kentico.kontent.delivery.System;

@ContentItemMapping("section_part")
public class SectionPart {

// Custom strongly type mapping to List
@ElementMapping("content")
String content;

@ElementMapping("title")
String title;

System system;

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public System getSystem() {
return system;
}

public void setSystem(System system) {
this.system = system;
}
}
Loading