kontent-ai / java-packages

Delivery Java SDK for Kontent.ai including examples for Spring, Gradle, Kotlin and Java Android app
https://kontent.ai/learn/tutorials/develop-apps/overview/?tech=java
MIT License
14 stars 28 forks source link

Nested support for Custom Models #128

Open D-Fox opened 3 years ago

D-Fox commented 3 years ago

Motivation

I want to fetch my model A class using client.getItems(A.class, params). The problem is that conversion for custom types is not working deeper in hierarchy.

@ContentItemMapping("a")
class A {
@ContentItemMapping("b") List<B> bList;
}

@ContentItemMapping("b")
class B {
  @ElementMapping("name") String name;
  @ContentItemMapping("b") List<C> cList;
}

@ContentItemMapping("c")
class C {
  @ElementMapping("name") String name;
}

Class A will contain list of B classes, but inside this class List<C> cList will be mapped to empty list (String name inside B class is ok).

If i fetch class B direcly using client.getItems(B.class, params), List<C> cList will be mapped correctly. So it seems it's not working only if im deeper in hierarchy.

Proposed solution

Add support to nested custom models mapping.

Additional context

There is also parameter linkedItemsDepth(int), but it seems that this is only valid for ContentItem object model, not custom models.

Simply007 commented 3 years ago

Hello @D-Fox.

Thanks for the issue. I will take a look at it and create a test for that to verify if I get the problem right.

There might be a problem when a circular reference is modeled in Kontent (maybe the reason why it is not implemented).

Would it be OK for you to just extend the possibility to specify how many levels should the mapping works? It should prevent an infinite loop for circular reference (I might find another solution without the necessity of the depth parameter using some memorization - we have it in other SDKs - but I haven't seen this codebase for a while).

D-Fox commented 3 years ago

Thanks for the reply. Parameter similar to the linkedItemsDepth will be sufficient.

Simply007 commented 3 years ago

Hello @D-Fox, I have created a test for your situation in #129. Does the test fit the situation you are describing?

I have also drafted an enhancement in the linked pull request (hence the test is passing). But it is a change in the SDK's core and it would require a longer time to put to production to ensure we don't make any breaking changes for current users of the SDKs.