Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore meta in included block #70

Merged
merged 2 commits into from
Aug 9, 2024
Merged

Conversation

thisAAY
Copy link
Collaborator

@thisAAY thisAAY commented Aug 9, 2024

Support for Ignoring meta Inside included Block

Summary

In the current version of the JsonApix library, the presence of a meta field inside the included block causes a crash during the deserialization process. This PR addresses this issue by ensuring that any meta fields within the included block are ignored during deserialization, thus preventing the crash.

What’s Changed

  • Meta Handling in included Block:
    • Previously, adding a meta field inside the included block would result in a crash because there was no default deserializer for the Meta class. This PR introduces logic to ignore meta fields within the included block, preventing the crash from occurring.
    • We've introduced an UnknownMeta class, which serves as a default deserializer for any unrecognized Meta types, ensuring that the deserialization process does not fail.

This problem also extends to links and errors fields within the included block. However, crashes were not occurring in those cases because we had already defined DefaultLinks and DefaultError classes, which provided a fallback during deserialization.

For meta, no such default existed until now. To solve this, we introduced the UnknownMeta class, which is an empty class designed solely to avoid serialization issues when unexpected meta fields are encountered.

Furthermore, we've updated our polymorphic serialization configuration as follows:

polymorphic(Meta::class) {
    // ... other subclasses
    defaultDeserializer { UnknownMeta.serializer() }
}

Do’s and Don’ts

Do:

You can safely add meta fields in the following locations:

  • Meta in Root Block:

    {
      "meta": { "version": "1.0" },
      "data": { ... }
    }
  • Meta in Resource Object:

    {
      "data": {
        "type": "articles",
        "id": "1",
        "meta": { "author": "John Doe" }
      }
    }
  • **Meta Relationships **:

    {
      "data": {
        "type": "articles",
        "id": "1",
        "relationships": {
          "author": {
            "data": { "type": "people", "id": "9" },
            "meta": { "role": "author" }
          }
        }
      }
    }

Don’t:

Adding a meta field inside the included block will still be ignored, but previously would cause a crash:

  • Meta Inside included Block (Causes Crash):
    {
      "data": { ... },
      "included": [
        {
          "type": "people",
          "id": "9",
          "meta": { "author": "John Doe" }  // Previously caused a crash
        }
      ]
    }

@thisAAY
Copy link
Collaborator Author

thisAAY commented Aug 9, 2024

Support for included block meta,links and errors is tracked here #71

@thisAAY thisAAY merged commit 5c3d124 into master Aug 9, 2024
@thisAAY thisAAY deleted the fix/ignore-meta-in-includedblock branch August 9, 2024 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants