Closed skyred closed 7 years ago
included
is not "calculated" automatically but with explicit call to Document.include(ResourceIdentifier)
. And edit a resource without detaching it's associated document can have the document attached to that resource being serialized as well when serializing the resource object.
Therefore, a new document object should be attach to updated resource object. https://github.com/kamikat/moshi-jsonapi#document
(It should be less confusing to treat resource as immutable object here)
Thanks for your reply and the explanation. I am still having trouble to understand this statement:
Document is wrap/unwrapped automatically by the converter.
I can do this no problem:
Article article = new Article()
article.title = ....
article.otherProperty = ...
...
// create the article with retrofit
Call<Article> call = retrofit.createArticle(article);
However, if I try to do:
User author = retrofit.getAuthor(...); // This can also be new Author();
article.setAuthor(author); // This throw exception, as the Document has not been initialized
// create the article with retrofit
Call<Article> call = retrofit.createArticle(article);
Above would throw exception as the Document is null.
I was wondering if I initialize a new Resource
, how can I set another resource as the Relationship
. Or how can I trigger auto wrapping Document
around the new Resource
?
The documentation shows another way: it first initializes the Document<Article>
, then includes Author
, but I don't know how to pass Document<Article>
to retrofit?
When I do Document<Article> document = new Document<>();
, I get the error: "Document is abstract, cannot be instantiated.". hmm...
I'm sorry for the misguide of documentation. Document
object is made abstract since 3.2.0. The replacing constructor is one of ArrayDocument
and ObjectDocument
.
Explicitly create a document to handle resources:
Document<Article> document = new ObjectDocument<>();
Document<User> author = retrofit.getAuthor(...);
document.set(article); // FIXME the _doc of article is set to document implicitly
document.include(author); // FIXME so does the author
retrofit.createArticle(article);
The abstraction is not yet perfect but I'll try to eliminate the ambiguity later.
I have Resource Point and PointMovement. They reference each other. When I tries to update the Point. Below is actually the request body generated:
I was wondering why does the "included" member include User, and other resources? It doesn't seem to be relevant to this PATCH request
This request body was generated by 'com.squareup.retrofit2:retrofit:2.3.0' 'moe.banana:moshi-jsonapi:3.2.2'