You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have run into situations where the API gets a bit ahead of our application and the endpoint returns a relationship that hasn't been defined in the application's object model yet.
For example, a JSONAPI ResourceObjectDescription might include a relationship for 'person' and 'address'. The API returns /included data for 'person', 'address' and 'email'.
In this case, the decoding fails and returns a message along the lines of: Out of the 3 includes in the document, the 1st one failed to parse: Found JSON:API type ‘email’ but expected one of ‘person’, ‘address’
The API team is trying to avoid strict API versioning, so I have been asked if there is a way to circumvent this error in the event that unexpected data is found in the json.
This is definitely doable but I appear to have not written about it specifically in the README yet (A PR adding it would be accepted but there's no expectation that you contribute that). The high level approach is to create a ResourceObjectProxy that will succeed in decoding any type of resource and add that to the allowable set of includes for the document.
It's important to note that these "other" types will only decode a type and id given the code I've linked you to. You certainly could use something like AnyCodable (or any other Codable implementation of an arbitrary dictionary of values) to also decode attributes/relationships if that was desirable, but I am guessing by the time you are dealing with attributes and relationships you are going to want to add a known JSON:API type instead of using the generic fallback one anyway.
Activity
mattpolzin commentedon Apr 21, 2025
This is definitely doable but I appear to have not written about it specifically in the README yet (A PR adding it would be accepted but there's no expectation that you contribute that). The high level approach is to create a
ResourceObjectProxythat will succeed in decoding any type of resource and add that to the allowable set of includes for the document.I've added a test of exactly this to the test suite so that you can snag the code and apply it directly to your code base: https://github.com/mattpolzin/JSONAPI/pull/121/files
It's important to note that these "other" types will only decode a type and id given the code I've linked you to. You certainly could use something like
AnyCodable(or any otherCodableimplementation of an arbitrary dictionary of values) to also decode attributes/relationships if that was desirable, but I am guessing by the time you are dealing with attributes and relationships you are going to want to add a known JSON:API type instead of using the generic fallback one anyway.