Skip to content

Having no attributes in the relation data does not add relation to included array #134

Description

@AmauryD

When the relationship's value only have an id key in their structure, the relation is in "relationships" but not in the "included" array.

The serializer code (simplified)

const serializer = new JSONAPISerializer({

});
this.serializer.register('user', {
  whitelist: ['firstName', 'lastName'],
  relationships: {
    documents: {
      type: 'document',
    },
  },
});
this.serializer.register('document', {
  whitelist: ['filename', 'mimetype', 'originalName', 'path', 'size']
});

return this.serializer.serializeAsync(
    'user',
    { id: '12345678910abcdef', documents: [ { id: '123456789' } ] },
    extraData ?? ({} as any),
  );

The API response

{
	"jsonapi": {
		"version": "1.0"
	},
	"links": {
		"self": "/api/v1/users/12345678910abcdef"
	},
	"data": {
		"type": "user",
		"id": "12345678910abcdef",
		"relationships": {
			"documents": {
				"data": [
					{
						"type": "document",
						"id": "123456789"
					}
				]
			}
		},
		"links": {
			"self": "/api/v1/users/12345678910abcdef"
		}
	}
}

I think the correct output would be something like this :

{
	"jsonapi": {
		"version": "1.0"
	},
	"links": {
		"self": "/api/v1/users/12345678910abcdef"
	},
	"data": {
		"type": "user",
		"id": "12345678910abcdef",
		"relationships": {
			"documents": {
				"data": [
					{
						"type": "document",
						"id": "123456789"
					}
				]
			}
		},
		"links": {
			"self": "/api/v1/users/12345678910abcdef"
		}
	}
        "included": [
		        {
			        "type": "document",
			        "id": "123456789",
			        "attributes": {},
			        "links": {
				        "self": "/api/v1/documents/123456789"
			        }
		        }
	        ]
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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