kontent-ai / delivery-sdk-js

Kontent Delivery SDK for Javascript
https://kontent.ai
MIT License
50 stars 34 forks source link

SDK resolves items excluded by depthParameter #345

Closed ondrabus closed 2 years ago

ondrabus commented 2 years ago

Brief bug description

Consider the following content model:

Blog Post

Author

If I use the following query:

deliveryClient
  .items<BlogPost>()
  ...
  .depthParameter(1)

I expect to receive an array of Blog Posts and their Authors. The linked items element "Blog Posts" on every Author should only contain an array of codenames of assigned blog posts. However, all the blog posts from the initial query level get mapped and resolved also on the Author level. As a result, I don't get an array of codenames, but an array of resolved Blog Posts.

This behavior is understandable from a technical point of view but very misleading and unpredictable as it relies solely on content from the CMS. When using framework such as Next.js that requires all data to be serializable, this behavior can (and does) cause circular references and breaks build.

Expected behavior

The depthParameter should ensure that resolution only happens for the specified amount of levels, not to all levels based solely on the existence of resolved items.

Enngage commented 2 years ago

This is very difficult to do and it might not even be feasible to do. For example, when you set depth=1 and a content item "links" to different item in modular_content, this linked item can link back to items present in items part of response. This means that you can have circular references even with depth=1.

You could duplicate each content item so that there can't be any circular references, but then the performance suffers, memory allocation is increased and you can still end in situation where the "same" content item contains linked items in one of the duplicate objects, and others don't (it depends on path which you used to get to the content item).

As it stands, I don't think we can solve this in a reasonable way.

When you need to serialize circular objects you may:

1) Use different parsers, such as https://www.npmjs.com/package/flatted 2) Set linkedItemsReferenceHandler to ignore within the SDK configuration which was created for this purpose.