Skip to content

[BUG] Related fields are included in attributes by default #77

@egmacke

Description

@egmacke

Describe the bug*

When serializing a complex object with defined relationships, all related fields are included in the attributes by default as well as in relationships.

To avoid this, you currently have to explicitly define a projection that excludes the relationship fields which adds an unnecessary overhead.

To Reproduce*

See replit: https://replit.com/@egmacke/ts-japi-issue-76

Result is

{
  "jsonapi": {
    "version": "1.0"
  },
  "data": {
    "type": "User",
    "id": "1",
    "attributes": {
      "name": "Foo",
      "articles": [
        {
          "id": "1",
          "title": "Article 1",
          "body": "Article 1 body"
        },
        {
          "id": "2",
          "title": "Article 2",
          "body": "Article 2 body"
        }
      ]
    },
    "relationships": {
      "articles": {
        "data": [
          {
            "type": "Article",
            "id": "1"
          },
          {
            "type": "Article",
            "id": "2"
          }
        ]
      }
    }
  }
}

I would expect the result to be (which can be achieved using the serializer option {projection: {articles: 0}})

{
  "jsonapi": {
    "version": "1.0"
  },
  "data": {
    "type": "User",
    "id": "1",
    "attributes": {
      "name": "Foo",
    },
    "relationships": {
      "articles": {
        "data": [
          {
            "type": "Article",
            "id": "1"
          },
          {
            "type": "Article",
            "id": "2"
          }
        ]
      }
    }
  }
}

Expected behavior*

When a relator is defined for a given field, it should be automatically excluded from the serialized attributes.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions