Skip to content

Deserializing resource with relationships without includes fails #352

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

Closed
dugsmith opened this issue Jul 19, 2019 · 1 comment
Closed

Deserializing resource with relationships without includes fails #352

dugsmith opened this issue Jul 19, 2019 · 1 comment

Comments

@dugsmith
Copy link
Contributor

Here's the scenario. I have a resource that needs to be created with a relationships. I POST to the server /api/accountAuthorizations with this request body:

{
  "data": {
    "type": "accountAuthorization",
    "attributes": {
      "accountKey": "ACCT_ID1",
      "accessLevel": "full"
    },
    "relationships": {
      "saUser": {
        "data": {
          "id": "USER_ID1",
          "type": "saUser"
        }
      }
    }
  }
}

The response back from the server includes the data section of the relationships I passed in, like so:

{
    "data": {
        "id": "AUTH_ID1",
        "type": "accountAuthorization",
        "attributes": {
            "updatedBy": "USER_ID99",
            "accessLevel": "full",
            "createdBy": "USER_ID99",
            "accountKey": "ACCT_ID1"
        },
        "relationships": {
            "saUser": {
                "data": {
                    "id": "USER_ID1",
                    "type": "saUser"
                },
                "links": {
                    "self": "/api/accountAuthorizations/AUTH_ID1/relationships/saUser",
                    "related": "/api/accountAuthorizations/AUTH_ID1/saUser"
                }
            }
        },
        "links": {
            "self": "/api/accountAuthorizations/AUTH_ID1"
        }
    }
}

Unfortunately, json_api_client version 0.14.0 throws an exception when calling included_data_for on the resource while deserializing this response. Partial stack:

/ruby/2.4.0/lib/ruby/gems/2.4.0/gems/json_api_client-1.14.0/lib/json_api_client/included_data.rb:56:in `record_for'
/ruby/2.4.0/lib/ruby/gems/2.4.0/gems/json_api_client-1.14.0/lib/json_api_client/included_data.rb:44:in `data_for'
/ruby/2.4.0/lib/ruby/gems/2.4.0/gems/json_api_client-1.14.0/lib/json_api_client/resource.rb:534:in `included_data_for'

It appears that json_api_client expects that if the relationships has a resource with a data object like in my example, there must be a matching full resource in the included section of the response payload. This doesn't have that.

I don't see this requirement in the JSON:API spec, and the Java client I'm also testing with (Crnk) handles this gracefully.

I'm going to try and create a pull request that just returns a nil in included_data_for in this scenario.

dugsmith pushed a commit to dugsmith/json_api_client that referenced this issue Jul 19, 2019
dugsmith pushed a commit to dugsmith/json_api_client that referenced this issue Jul 19, 2019
@dugsmith
Copy link
Contributor Author

FYI: this validator says the response above is valid: https://jsonapi-validator.herokuapp.com/.

dugsmith pushed a commit to dugsmith/json_api_client that referenced this issue Jul 22, 2019
gaorlov added a commit that referenced this issue Jul 23, 2019
fix issue #352: relationships w/o included data
@gaorlov gaorlov closed this as completed Aug 6, 2019
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