Skip to content

Handle unexpected relationships in json? #120

Open

Description

@scotsimon

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.

Thoughts?

Activity

  1. added
    question
    on Apr 21, 2025
  2. mattpolzin commented on Apr 21, 2025

    @mattpolzin
    Owner

    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.

    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 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions