Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

References to objects in array documents and object documents #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/src/main/java/moe/banana/jsonapi2/ArrayDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ public boolean remove(Object o) {
return false;
}

@Override
public <T extends Resource> T find(ResourceIdentifier resourceIdentifier) {
for (DATA resource : data) {
if (resource.getId().equals(resourceIdentifier.getId()) &&
resource.getType().equals(resourceIdentifier.getType())) {
//noinspection unchecked
return (T) resource;
}
}

return super.find(resourceIdentifier);
}

@Override
public boolean containsAll(Collection<?> c) {
return data.containsAll(c);
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/moe/banana/jsonapi2/ObjectDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public boolean hasData() {
return hasData;
}

@Override
public <T extends Resource> T find(ResourceIdentifier resourceIdentifier) {
if (data != null &&
data.getId().equals(resourceIdentifier.getId()) &&
data.getType().equals(resourceIdentifier.getType())) {
//noinspection unchecked
return (T) data;
}

return super.find(resourceIdentifier);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/moe/banana/jsonapi2/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public void deserialize_object() throws Exception {
assertOnArticle1(document.<Article>asObjectDocument().get());
}

@Test
public void find_in_object_document_data() throws Exception {
ObjectDocument document = (ObjectDocument) getDocumentAdapter(Article.class)
.fromJson(TestUtil.fromResource("/single.json"));

assertOnArticle1((Article) document.find(new ResourceIdentifier("articles", "1")));
}

@Test
public void deserialize_object_null() throws Exception {
Document document = getDocumentAdapter(Article.class)
Expand Down Expand Up @@ -203,6 +211,15 @@ public void deserialize_array_to_array_typed_document() throws Exception {
assertOnArticle1(arrayDocument.get(0));
}

@Test
public void deserialize_refences_to_data_objects_in_array_document() throws Exception {
Moshi moshi = TestUtil.moshi(Article.class, Comment.class);
JsonAdapter<?> adapter = moshi.adapter(Types.newParameterizedType(ArrayDocument.class, Article.class));
ArrayDocument<Article> arrayDocument = ((ArrayDocument<Article>) adapter.fromJson(TestUtil.fromResource("/multiple_compound.json")));

assertOnArticle1((Article) arrayDocument.find(new ResourceIdentifier("articles", "1")));
}

@Test
public void serialize_null() {
ObjectDocument document = new ObjectDocument();
Expand Down
9 changes: 9 additions & 0 deletions core/src/test/java/moe/banana/jsonapi2/model/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class Comment extends Resource {
private String body;
private HasOne<Person> author;
private HasOne<Article> article;

public String getBody() {
return body;
Expand All @@ -24,4 +25,12 @@ public HasOne<Person> getAuthor() {
public void setAuthor(HasOne<Person> author) {
this.author = author;
}

public HasOne<Article> getArticle() {
return this.article;
}

public void setArticle(HasOne<Article> article) {
this.article = article;
}
}
2 changes: 1 addition & 1 deletion core/src/test/resources/multiple_compound.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@
"cache_hit": true
}
}]
}
}