Skip to content

Incorrectly deserializing relationship when alternativeKey is set from default and relationship data is not in the included array #116

Closed
@itskingori

Description

@itskingori

I was trying out this library last night and it works really great! Thanks for the awesome work. Thought I'd share what I think might be a bug. I'm going to use a simplified example to demonstrate the issue I am having. So, here goes.


Given the JSONAPISerializer configuration below ...

Serializer = new JSONAPISerializer({ unconvertCase: 'snake_case', });

Serializer.register('application', {
    id: 'id',
    relationships: {
        repository: {
            alternativeKey: 'repository_id',
            type: 'repository',
        },
    },
});

Serializer.register('deployment', {
    id: 'id',
    relationships: {
        application: {
            alternativeKey: 'application_id',
            type: 'application',
        },
    },
});

I tried to deserialize the below response ...

{
    "data": [
        {
            "id": "1",
            "type": "deployment",
            "attributes": {
                "commit": "ee6c16c94700a56d497f64b0be3e1fdd36a0524c",
                "created_at": "2021-01-19T17:34:38Z",
                "updated_at": "2021-01-19T17:34:38Z"
            },
            "relationships": {
                "application": {
                    "data": {
                        "id": "1",
                        "type": "application"
                    }
                }
            }
        }
    ],
    "included": [
        {
            "id": "1",
            "type": "application",
            "attributes": {
                "name": "Konklab",
                "created_at": "2021-01-19T17:34:38Z",
                "updated_at": "2021-01-19T17:34:38Z"
            },
            "relationships": {
                "repository": {
                    "data": {
                        "id": "1",
                        "type": "repository"
                    }
                }
            }
        }
    ]
}

The thing to note here is that I have an application related to a deployment and since I had ?include=application in my api-call, I correctly have the information of the related application in included:. Also note that the application information in included: has a relationship to repository but then we don't have information on that repository in included: (if I make the api-call have ?include=application,application.repository it's added).

Anyway, when I try to deserialize, I get this ...

[
  {
    "id": "1",
    "commit": "ee6c16c94700a56d497f64b0be3e1fdd36a0524c",
    "created_at": "2021-01-19T17:34:38Z",
    "updated_at": "2021-01-19T17:34:38Z",
    "application_id": "1",
    "application": {
      "id": "1",
      "name": "Konklab",
      "created_at": "2021-01-19T17:34:38Z",
      "updated_at": "2021-01-19T17:34:38Z",
      "repository_id": "1",
      "repository": "1"
    }
  }
]

Instead of this ...

[
  {
    "id": "1",
    "commit": "ee6c16c94700a56d497f64b0be3e1fdd36a0524c",
    "created_at": "2021-01-19T17:34:38Z",
    "updated_at": "2021-01-19T17:34:38Z",
    "application_id": "1",
    "application": {
      "id": "1",
      "name": "Konklab",
      "created_at": "2021-01-19T17:34:38Z",
      "updated_at": "2021-01-19T17:34:38Z",
      "repository_id": "1"
    }
  }
]

You can see I have "repository_id": "1" and "repository": "1" where I expected application.repository not to be set (since it's not available in the includes). If it can't resolve the relationship to the object then I expect to only have "repository_id": "1". When I make the api-call with ?include=application,application.repository then the application.repository is set to an object with the repository information as I expected ... so that's not the scenario with the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions