jsonapi-converter
jsonapi-converter copied to clipboard
`@RelationshipLinks` wihtout `@Relationship` not working
I'm trying to create a resource containing a relationship with links.
Note that this particular relationship has no data
and meta
which is is valid according to the spec as only 1 of the three (links
, data
, or meta
) is required.
E.g.
@Type("author")
public class Author {
@Id
private String id;
private String name;
@RelationshipLinks("books")
private Links booksLinks
}
will output:
"data": [
{
"type": "author",
"id": "1",
"attributes": {
"name": "Rowling",
"books-links": {
"links": {
"related": {
"href": "..",
"meta": null
},
"self": {
"href": "..",
"meta": null
}
}
}
}
},
..
Note the (invalid) books-links
element under attributes
.
I would have expected a book
relationship
.
Trying to workaround this, when changing the model like:
@Type("author")
public class Author {
@Id
private String id;
private String name;
@Relationship("books")
private List<Book> books = new ArrayList<>();
@RelationshipLinks("books")
private Links booksLinks
}
and leaving the books
array empty I get:
"data": [
{
"type": "author",
"id": "1",
"attributes": {
"name": "Rowling"
}
"relationships": {
"books": {
"links": {
"related": {
"href": "..",
"meta": null
},
"self": {
"href": "..",
"meta": null
}
},
"data": []
}
}
}
},
..
This looks better but includes an empty []
data
element under the books
relationship.
Which is also wrong as it would indicate this particular author has no books.
Changing the model to:
@Type("author")
public class Author {
@Id
private String id;
private String name;
@Relationship("books")
private List<Book> books = null;
@RelationshipLinks("books")
private Links booksLinks
}
But this messes up the response again unfortunately:
"data": [
{
"type": "author",
"id": "1",
"attributes": {
"name": "Rowling",
"books": null,
"books-links": {
"links": {
"related": {
"href": "..",
"meta": null
},
"self": {
"href": "..",
"meta": null
}
}
}
}
},
..
So currently is seems not possible to specify relationship links without actual relationship data.
I am having same issue, There has to be some neat way to ignore data part of relationship.
Unless noone wants to fork and fix the issue I suggest to create and register a resource like DummyResource
with dummy-resurce
type or something like that. Then create a field of this resource type with @Relationship
annotation and just keep it private, without any getters and setters. Having @Relationship
field in the model will allow Converter to resolve @RelationshipLinks
with the same key
¯\_(ツ)_/¯
Will take look to see what can be done.
Is there an ETA on a fix for this? I also have this problem, and I don't think the suggested workaround is viable due to how jsonapi-converter
generates data
. Perhaps a parameter could be added to the @Relationship
annotation to disable data
creation?
@jasminb - Any updates? Thanks!