Skip to content

Deserializing relationships to an object #65

Closed
@stefanvanherwijnen

Description

@stefanvanherwijnen

The default behaviour of deserialize() seems to be to return an array of relationships keys. If the top-level key "included" is set, it returns the relationships as an object with an 'id' key however.

For example:

{
	"data": {
		"id": 1,
		"attributes": {
			"name": "randomname"
		},
		"relationships": {
			"roles": {
				"data": {
					"type": "role",
					"id": 1
				}
			}
		}
	}
}

deserializes to { id: 1, name: 'randomname', roles: 1 }.

{
	"data": {
		"id": 1,
		"attributes": {
			"name": "randomname"
		},
		"relationships": {
			"roles": {
				"data": {
					"type": "role",
					"id": 1
				}
			}
		}
	},
	"included": [{
		"type": "role",
		"id": 1
	}]
}

deserializes to: { id: 1, name: 'randomname', roles: { id: 1 } }

Now, I would like to get the second result, but the JSON API spec doesn't seem to require the "included" key on updates: https://jsonapi.org/format/#crud-updating-resource-relationships

So I am wondering, is there a specific reason why the result of deserialization differs when the "included" key exists in the request?

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