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

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

Closed
itskingori opened this issue Jan 21, 2021 · 2 comments

Comments

@itskingori
Copy link

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.

@danivek
Copy link
Owner

danivek commented Jan 22, 2021

Thank you for the clear and concise explanation of the behavior

@itskingori
Copy link
Author

Anytime. Thanks for the quick fix. 🙇🏽‍♂️ Just tested it out and it works great! 👌🏽

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

No branches or pull requests

2 participants