jsonapi-serializer icon indicating copy to clipboard operation
jsonapi-serializer copied to clipboard

relationshipLinks does not allow self links

Open ghost opened this issue 6 years ago • 2 comments

The relationshipLinks option only allow related where the 1.0 specification allows both self and related.

possible section of code related to the issue https://github.com/SeyZ/jsonapi-serializer/blob/master/lib/serializer-utils.js#L162

1.0 specification section https://jsonapi.org/format/#document-resource-object-relationships

I cannot do the following and have "self" links:

let people = [
	{
		id: 10,
		fname: 'aaa',
		lname: 'bbb',
		title: 'king',
		subPerson: {
			id: 12,
			fname: 'ccc',
			lname: 'ddd',
			title: 'knight'
		}
	},
	{
		id: 11,
		fname: 'eee',
		lname: 'fff',
		title: 'queen',
		subPerson: {
			id: 13,
			fname: 'ggg',
			lname: 'hhh',
			title: 'jester'
		}
	}
];

let response = new JSONAPI('people', {
	meta: { count: 2 },
	attributes: ['fname', 'lname', 'title', 'subPerson'],
	subPerson: {
		ref: 'id',
		//included: false,
		attributes: ['fname', 'lname', 'title'],
		relationshipLinks: {
			self: function(record, current, parent) {
				return '/api/jsonapi/people/' + current.id;
			}
		}
	}
}).serialize(people);

ghost avatar Nov 29 '18 19:11 ghost

I encountered this today. A temporary solution is to also have a related link.

roycetech avatar Jan 04 '19 07:01 roycetech

This is a regression. self links in relationships were working in 3.4.3 but no longer seem to work in 3.6.6

After a little digging it appears that this is in fact a regression introduced by this commit:

https://github.com/SeyZ/jsonapi-serializer/commit/3d776ef46b3cb150d09c9d7efc209a5a7ecc7bc0

Starting in 3.6.1 of the library you can no longer have self links on their own which is definitely a bug.

@roycetech I created a PR that should fix this issue if you'd like to try it out.

sarus avatar Feb 14 '20 04:02 sarus