Comments (13)
Hello @puneethere, thank you for submitting the ticket. Will take a look when I get some time and update you here.
from jsonapi-converter.
@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.
@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.
@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.
@puneethere will try to create a new release this week.
from jsonapi-converter.
@puneethere please checkout the latest version of the tool (0.12
) and if ok close the issue.
from jsonapi-converter.
Thanks for the quick turnaround on this one @jasminb. I will update the library and will get back to you. Thanks
from jsonapi-converter.
@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.
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.
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.
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.
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.
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)
- How to generate this nested JSON (tenor) ? HOT 1
- Should ALLOW_UNKNOWN_TYPE_IN_RELATIONSHIP be less restrictive HOT 7
- @Id obfuscated by proguard even after excluding the annotation HOT 2
- POST Example with RestTemplate HOT 2
- How to configure ObjectMapper? HOT 2
- @Type serialization HOT 2
- Document collection always throw error while using readCollections HOT 1
- Object with relations itself HOT 1
- @Relationship is not being deserialized HOT 2
- Creating a resource on the server - required id field HOT 2
- Does the library support Javas webclient and the reactor implementation? HOT 2
- "jsonapi": { "version": "1.0" } Parser HOT 2
- Get value above "attributes" level HOT 2
- Relationship attributes dont serialized HOT 3
- Class doesn't have a Type annotation error, even though the class does have one HOT 3
- Proguard issues with jsonapi-converter when Android project to targets java 17 HOT 2
- InvalidJsonApiResourceException: Resource must contain at least one of 'data', 'error' or 'meta' nodes
- Handling local identifier ("lid") when creating resource HOT 4
- Backward compatibility HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi-converter.