kamikat / moshi-jsonapi

JSON API v1.0 Specification in Moshi.
MIT License
156 stars 34 forks source link

Relationship query fails when resource type has kebab-case #27

Closed waritsan closed 7 years ago

waritsan commented 8 years ago

Hi, I get a java.lang.NullPointerException when I try to get the relationship resource with included: [] in the JSON body. This seems to only occur when the relationship resource type has a dash, for example when the type is "sports-articles" I get an error. But if I change the type both in the JSON body and JsonApi annotation to "articles", it works fine. I'm not sure why this is happening. Please advise. Here's an example of my code.

@JsonApi(type = "authors")
public class Author extends Resource {
    @Json(name = "name") String name;
    HasMany<Article> articles;
}
@JsonApi(type = "sports-articles")
public class Article extends Resource {
    @Json(name = "name") String name;
}
@Test
public void testAuthor_ShouldBeDeserializable() {
       JsonAdapter.Factory factory = ResourceAdapterFactory.builder()
                .add(Author.class)
                .add(Article.class)
                .build();
        Moshi moshi = new Moshi.Builder().add(factory).build();
        Author[] authors = moshi.adapter(Author[].class).fromJson("{\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"id\": \"1\",\n" +
                "      \"type\": \"authors\",\n" +
                "      \"links\": {\n" +
                "        \"self\": \"http://localhost:3000/authors/1\"\n" +
                "      },\n" +
                "      \"attributes\": {\n" +
                "        \"name\": \"Author\"\n" +
                "      },\n" +
                "      \"relationships\": {\n" +
                "        \"sports-articles\": {\n" +
                "          \"links\": {\n" +
                "            \"self\": \"http://localhost:3000/authors/1/relationships/sports-articles\",\n" +
                "            \"related\": \"http://localhost:3000/authors/1/sports-articles\"\n" +
                "          },\n" +
                "          \"data\": [\n" +
                "            {\n" +
                "              \"type\": \"sports-articles\",\n" +
                "              \"id\": \"1\"\n" +
                "            }\n" +
                "          ]\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  ],\n" +
                "  \"included\": [\n" +
                "    {\n" +
                "      \"id\": \"1\",\n" +
                "      \"type\": \"sports-articles\",\n" +
                "      \"links\": {\n" +
                "        \"self\": \"http://localhost:3000/sports-articles/1\"\n" +
                "      },\n" +
                "      \"attributes\": {\n" +
                "        \"name\": \"Article\",\n" +
                "        \"created-at\": \"2016-08-17T07:42:38.126Z\"\n" +
                "      },\n" +
                "      \"relationships\": {\n" +
                "        \"author\": {\n" +
                "          \"links\": {\n" +
                "            \"self\": \"http://localhost:3000/sports-articles/1/relationships/author\",\n" +
                "            \"related\": \"http://localhost:3000/sports-articles/1/author\"\n" +
                "          }\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  ]\n" +
                "}");
        Author author = authors[0];
        assertEquals(1, author.articles.getAll().length);  // author.articles => null
}
kamikat commented 8 years ago

Umm... I think you've forgotten to add a @Json annotation to the articles field.

@JsonApi(type = "authors")
public class Author extends Resource {
    @Json(name = "name") String name;
    @Json(name = "sports-articles")
    HasMany<Article> articles;
}

Note that the "key" in relationships object indicates a field name, and have no relationship with the type of the linked object, which is determined by type of a resource linkage object.

waritsan commented 7 years ago

Oh I see. Thank you for the clarification.