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

Reject nonunique resources #59

Merged
merged 5 commits into from
Aug 8, 2024

Conversation

20joshuaz
Copy link
Contributor

Issue: #33

About me

This PR adds logic to check that, per jsonapi's spec, a single canonical resource object is returned with each response. It checks resource objects across the primary data and included sections to find duplicates (duplicate being resources objects with the same type & id). It also checks every relationship section for duplicates.

The following documents were previously accepted but now rejected:

Duplicate resource objects in primary data

{
  "data": [
    {
      "id": "1",
      "type": "articles",
      "attributes": {
        "title": "A"
      }
    },
    {
      "id": "1",
      "type": "articles",
      "attributes": {
        "title": "B"
      }
    }
  ]
}

Duplicate resource objects in included section

{
  "data": {
      "id": "1",
      "type": "articles",
      "attributes": {
        "title": "A"
      },
      "relationships": {
        "comments": {
          "data": { "type": "comments", "id": "1" }
        }
      }
    },
  "included": [{
    "type": "comments",
    "id": "1",
    "attributes": {
      "body": "I like XML better"
    }
  }, {
    "type": "comments",
    "id": "1",
    "attributes": {
      "body": "I like YAML better"
    }
  }]
}

Duplicate resource objects across primary data and included

{
  "data": [{
      "id": "1",
      "type": "employees",
      "attributes": {
        "name": "John Doe"
      },
      "relationships": {
        "managers": {
          "data": { "type": "employees", "id": "2" }
        }
      }
    }, {
      "id": "2",
      "type": "employees",
      "attributes": {
        "name": "Mary Jane"
      },
      "relationships": {
        "managers": {
          "data": { "type": "employees", "id": "3" }
        }
      }
    }],
  "included": [{
    "type": "employees",
    "id": "2",
    "attributes": {
      "name": "Mary Jane"
    }
  }]
}

Duplicate resource linkages

{
  "data": {
      "id": "1",
      "type": "articles",
      "attributes": {
        "title": "A"
      },
      "relationships": {
        "comments": {
          "data": [{ "type": "comments", "id": "1" }, { "type": "comments", "id": "1"}]
        }
      }
   }
}

Testing

Added tests that cause error to be thrown

Questions

  • The jsonapi spec specifically calls out resource objects to be unique, but it doesn't mention resource linkages (the last example in the About me section). Is checking for duplicate resource linkages in every relationship section overkill?

@20joshuaz 20joshuaz marked this pull request as ready for review August 5, 2024 13:02
jsonapi.go Outdated Show resolved Hide resolved
@DQSevilla DQSevilla merged commit 465e04d into DataDog:main Aug 8, 2024
6 checks passed
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