jsonapi-converter icon indicating copy to clipboard operation
jsonapi-converter copied to clipboard

`@RelationshipLinks` wihtout `@Relationship` not working

Open marceloverdijk opened this issue 6 years ago • 5 comments

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.

marceloverdijk avatar Apr 23 '18 19:04 marceloverdijk

I am having same issue, There has to be some neat way to ignore data part of relationship.

manish-kevre avatar Feb 07 '19 13:02 manish-kevre

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 ¯\_(ツ)_/¯

Radiokot avatar Feb 07 '19 13:02 Radiokot

Will take look to see what can be done.

jasminb avatar Feb 07 '19 23:02 jasminb

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?

mflagg2814 avatar Aug 19 '19 16:08 mflagg2814

@jasminb - Any updates? Thanks!

mflagg2814 avatar Sep 30 '19 19:09 mflagg2814