Skip to content

Deserializing resource with relationships without includes fails #352

Closed
@dugsmith

Description

@dugsmith

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions