Giter Site home page Giter Site logo

Comments (13)

jasminb avatar jasminb commented on August 17, 2024

Hello @puneethere, thank you for submitting the ticket. Will take a look when I get some time and update you here.

from jsonapi-converter.

jasminb avatar jasminb commented on August 17, 2024

@puneethere taken a quick look, If I am understanding you correctly, you are expecting the included attribute to be present regardless of the fact that there is no data to be included as the relationship that you are including has no elements?

from jsonapi-converter.

puneethere avatar puneethere commented on August 17, 2024

@jasminb your understanding is correct, as per json specification, the included element must be present as an empty array if the relationship is empty

from jsonapi-converter.

puneethere avatar puneethere commented on August 17, 2024

@jasminb have a look at the section 8.3 of Json specification https://jsonapi.org/format/.

Also, please do let me know if this can be fixed? Thanks

from jsonapi-converter.

jasminb avatar jasminb commented on August 17, 2024

@puneethere will try to create a new release this week.

from jsonapi-converter.

jasminb avatar jasminb commented on August 17, 2024

@puneethere please checkout the latest version of the tool (0.12) and if ok close the issue.

from jsonapi-converter.

puneethere avatar puneethere commented on August 17, 2024

Thanks for the quick turnaround on this one @jasminb. I will update the library and will get back to you. Thanks

from jsonapi-converter.

puneethere avatar puneethere commented on August 17, 2024

@jasminb I have tested the latest version (0.12) and the API response now do have the included key, but it is present irrespective of whether the client supplies the include parameter or not.

from jsonapi-converter.

puneethere avatar puneethere commented on August 17, 2024

For instance, all endpoints now produce the following output, even when client does not supply the 'include' parameter:
{"data": [], "included": []}
rather when client doesn't supply the 'include' parameter then the response should not have the 'included' key.

from jsonapi-converter.

jasminb avatar jasminb commented on August 17, 2024

Please provide a unit test that reproduces the issue, here is a unit test that is passing in case relationship inclusion is disabled:

  @Test
  public void testRelationshipInclusionDisabled() throws DocumentSerializationException {
    ResourceConverter customConverter = new ResourceConverter(Article.class, Author.class);
    customConverter.disableSerializationOption(SerializationFeature.INCLUDE_RELATIONSHIP_ATTRIBUTES);

    Author author = new Author();
    author.setId("author_id");
    author.setLastName("Bar");
    author.setFirstName("Foo");
    author.setArticles(new ArrayList<Article>());

    byte[] serialized = customConverter.writeDocument(new JSONAPIDocument<>(author));
    // included element is not there as the relationship inclusion is disabled on the converter level
    Assert.assertFalse(new String(serialized).contains("\"included\":[]"));

    SerializationSettings serializationSettings =
      new SerializationSettings.Builder().includeRelationship("articles").build();

    // included element is present as the settings are enabling the serialization of relationship
    serialized = customConverter.writeDocument(new JSONAPIDocument<>(author), serializationSettings);
    Assert.assertTrue(new String(serialized).contains("\"included\":[]"));
  }

Note that inclusion of the empty included[] element depends on serialization options on ResourceConverter level or on the level of writeDocument invocation using SerializationSettings.

from jsonapi-converter.

puneethere avatar puneethere commented on August 17, 2024

I understand that the library would only respond with included key in the response when the SerializationFeature.INCLUDE_RELATIONSHIP_ATTRIBUTES is enabled on ResourceConverter. And I need this feature as I have endpoints which based on client request needs to respond with 'included' array.
The presence of included should be govern by whether the client is requesting for it or not by supplying the 'include' parameter in the endpoint request. Currently what is happening is that even when client is not supplying the 'include' parameter in the endpoint request, the library is producing the response with blank 'included' array. Now if we disable the INCLUDE_RELATIONSHIP_ATTRIBUTES then it will stop sending the included array even when client has requested with 'include' parameter.

In summary, what I am alluding to is that once we have INCLUDE_RELATIONSHIP_ATTRIBUTES enabled on the resource converter, then the library should also check for the 'include' parameter in the request before adding the 'included' array in the response. This is as per the JSON specification.

from jsonapi-converter.

jasminb avatar jasminb commented on August 17, 2024

Ok, so if I understand you correctly, you are using the lib on the server side as a framework to serialise responses to user requests? If this is the case, I suggest configuring different flavours of the resource converter and using them depending on the user preference, e.g. if the user sent the include param, use the instance that has the INCLUDE_RELATIOINSHIP_ATTRIBUTES enabled, if no param is sent than use an instance with disabled rel attributes.

Another way would be to have a single instance and use the SerializationSettings that are built for each requests using the user-provided params.

from jsonapi-converter.

jasminb avatar jasminb commented on August 17, 2024

Closing as fixed and the requested feature is out of scope for the library (understanding user requests in context of an HTTP API).

from jsonapi-converter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.