Allowing relationship links to get serialized for null relationship object#246
Conversation
|
@shipra-aeron will take a look and comment/merge. Thanks for the PR. Edit: |
| // Make sure that relationship object i.e. statuses is null | ||
| assertNull(users.get(0).getStatuses()); | ||
|
|
||
| byte[] convertedData = converter.writeObjectCollection(users); |
There was a problem hiding this comment.
Looks like JSON produced here holds an empty statuses relationship object and breaks the spec which states that it must have one of links/da/data/meta.
There was a problem hiding this comment.
Thanks. I will fix it asap.
There was a problem hiding this comment.
Request change has been incorporated.
There was a problem hiding this comment.
Hello, first of all apologies for a such long time that it has taken for me to take a look again.
Lib still produces empty relationship object for resources that are serialised and are lacking relationship details in the annotation. For example:
@Relationship(value = "statuses")
private List<Status> statuses;Produces:
{
"data": [
{
"type": "users",
"id": "1",
"attributes": {
"name": "liz"
},
"relationships": {
"statuses": {}
},
"links": {
"self": "https://api.example.com/users/1"
}
},
{
"type": "users",
"id": "2",
"attributes": {
"name": "john"
},
"relationships": {
"statuses": {}
},
"links": {
"self": "https://api.example.com/users/2"
}
}
]
}It should omit relationships altogether as there are no non-empty elements in it:
{
"data": [
{
"type": "users",
"id": "1",
"attributes": {
"name": "liz"
},
"links": {
"self": "https://api.example.com/users/1"
}
},
{
"type": "users",
"id": "2",
"attributes": {
"name": "john"
},
"links": {
"self": "https://api.example.com/users/2"
}
}
]
}Adding relationship annotation fixes the issue but thats besides the point as its not required.
@Relationship(value = "statuses", path = "/relationship/statuses")
private List<Status> statuses;…inks and data in relationship node.
The purpose of this PR is to allow to have all the relationships of a resource object to be always present inside relationships node and have links if they exists for a relationship.
As per the json api spec ->
A “relationship object” MUST contain at least one of the following:
Since above we have allowed all the relationships node to be always present, hence one should also provide a way to have atleast one to be always present out of the (data, meta and links)
The sole need for the above change is to make the client be aware of all the relationships associated with the resource objects.