mattpolzin / JSONAPI

Swift Codable JSON:API framework
MIT License
75 stars 19 forks source link

Included Data Issue #104

Closed scotsimon closed 1 year ago

scotsimon commented 1 year ago

Hey Matt!

Running into an issue when decoding /included data in a json response.

Simplified description:

This particular object (Event) has three relationships, "Organization", "Address", and "Facility". The object also contains an array of objects (RSVP) with a single relationship, "Person".

The /included data will contain data for all of the objects - so Organization, Address, Facility, and Person

However, when decoding the primary object (Event), it is generating this error:

**decodingFailure(OttoPay.API.ErrorDetails(error: Out of the 2 includes in the document, the 2nd one failed to parse: Found JSON:API type 'person' but expected one of 'organization', 'address', 'facility', description: "The operation couldn’t be completed. (JSONAPI.DocumentDecodingError error 1.)"))**

My understanding is that all /included data has to be in the top-level container - so is there something that I need to be doing different for this to work?

Thanks in advance! Scot

mattpolzin commented 1 year ago

I'm not 100% sure I understand the problem you are encountering, but here's a crack at an answer:

Included entities are decoded as part of the document rather than as part of the primary resource. A document has one primary resource type and any number of included types. Separate from the consideration of what your type of primary resource is, you can specify all the types of includes you might possibly encounter. A common way in which the types of includes can differ from the types of relationships on the primary resource is when requesting indirect includes; say the primary resource is a person and a person has an address and an address has a city, you might request both address and address.city be included in the response.

That is all just to say, if in your case you want your document to include all of the 4 types of things you listed above, you must specify all 4 things in the Include type for the document, and that is a perfectly valid thing to do. Perhaps that looks like Include4<Organization, Address, Facility, Person> in your codebase.

Does that help or am I missing the question?

scotsimon commented 1 year ago

Hey Matt! I spent a long time in your documentation (great resource) and came to the same conclusion. Thank you for taking the time to respond to my question!