kamikat / moshi-jsonapi

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

Support attribute and relationship with same name? #115

Closed pmurph0 closed 2 years ago

pmurph0 commented 2 years ago

This is a valid JSON API spec payload, with an attribute and relationship both called payments

{ "id": "ID_HERE", "type": "checks", "attributes": { "payments": [ { "id": "ID_HERE", "check_id": "ID_HERE", } ] }, "relationships": { "payments": { "data": [ { "type": "payments", "id": "ID_HERE" } ] }

But I can't deserialize it to my model which looks like this:

@JsonApi(type = "checks") data class Check( @Json(name = "payments") val paymentsResource: HasMany? = null, @Json(name = "payments") val payments: List? = emptyList() )

As the deserializer complains of a clash when mapping because of having 2 fields with the same Json name:

com.squareup.moshi.JsonDataException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $.data[0].relationships.payments

Is there a way to handle this?

kamikat commented 2 years ago

I'm sorry but actually that's not a valid JSONAPI resource according to the spec:

a resource can not have an attribute and relationship with the same name

Maybe we can have some test case for this :smile: