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

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

Open
AmauryD opened this issue Apr 11, 2022 · 0 comments

Comments

@AmauryD
Copy link

AmauryD commented Apr 11, 2022

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"
			        }
		        }
	        ]
}
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

1 participant