kamikat / moshi-jsonapi

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

com.squareup.moshi.JsonDataException: Unknown type of resource: place_address #104

Open di0n0s opened 4 years ago

di0n0s commented 4 years ago

Hi,

I receive this JSON:

{
    "data": [{
        "type": "place",
        "id": "00d66be8-7315-4f75-9dc1-7dc737a055ef",
        "attributes": {
            "name": "Test Pharmacy",
            "lat": 42.394898,
            "lng": 2.151117,
            "image_url": "",
            "user_distance": 0.03
        },
        "relationships": {
            "place_address": {
                "data": {
                    "type": "place_address",
                    "id": "00d66be8-7315-4f75-9dc1-7dc737a055ef"
                }
            }
        }
    }, {
        "type": "place",
        "id": "6f781c99-c991-442f-ae14-9630ee475f41",
        "attributes": {
            "name": "Farmacia Test",
            "lat": 42.394898,
            "lng": 2.112117,
            "image_url": "http:\/\/test.test.com\/image.png",
            "user_distance": 0.03
        },
        "relationships": {
            "place_address": {
                "data": {
                    "type": "place_address",
                    "id": "6f781c99-c991-442f-ae14-9630ee475f41"
                }
            }
        }
    }],
    "included": [{
        "type": "place_address",
        "id": "00d66be8-7315-4f75-9dc1-7dc737a055ef",
        "attributes": {
            "name": "Test Pharmacy Address",
            "country_code": "ES",
            "postal_code": "",
            "city": "",
            "region": "",
            "postal_address_line_one": "",
            "postal_address_line_two": "",
            "phone_number_one": "",
            "phone_number_two": ""
        }
    }, {
        "type": "place_address",
        "id": "6f781c99-c991-442f-ae14-9630ee475f41",
        "attributes": {
            "name": "address",
            "country_code": "ES",
            "postal_code": "",
            "city": "",
            "region": "",
            "postal_address_line_one": "",
            "postal_address_line_two": "",
            "phone_number_one": "",
            "phone_number_two": ""
        }
    }]
}

And I have this resource:

@JsonApi(type = "place")
public class PlaceResponse extends Resource {

    @Json(name = "name")
    public String name;
    @Json(name = "image")
    public String imageUrl;
    @Json(name = "lat")
    public Double latitude;
    @Json(name = "lng")
    public Double longitude;
    @Json(name = "user_distance")
    public Double userDistance;

}

When I'm trying to parse that JSON I got this exception: "com.squareup.moshi.JsonDataException: Unknown type of resource: place_address"

Why I'm getting this exception if I'm not parsing the relationship? Is mandatory to parse every field of the JSON? I hope not.

Thanks in advance.

Dill-Lan commented 4 years ago

Hey @di0n0s I think this is a duplicate of https://github.com/kamikat/moshi-jsonapi/issues/71

I ran in to the same problem and found yours and the above issue. Basically, the answer is to do as described here: https://github.com/kamikat/moshi-jsonapi#default-resource-type