danivek/json-api-serializer

bug: Duplicated models from includes are removed from response

EspadaV8 opened this issue · 1 comments

The title here is a probably a little off, but I can't think of a nicer way to explain it. We were using v1.9.1 and tried to update to v2.1.0 and one of our tests started failing and it looks like both v1 and v2 have different but semi-related issues.

Our test did a long include along the lines of (it isn't the same but the actual request wouldn't make sense with a lot more background knowledge)

group/list?include=people.comments.person.comments.person.photos

The include works fine and fetches a bunch of people and comments and a few photos, however, with v1 if a person has both comments and photos returned there would be 2 objects within the included array 1 with a relationship for comments and another with a relationship for photos. This clearly breaks the JSON-API standard since there are 2 objects in includes both with the same id (and this is what our test was checking for, an oversight when writing the test).

In v2 this behaviour changes and correctly sees that there are 2 objects with the same id and so only adds one to the includes. However, the relationships are not combined so now we have some missing photos relationships that should've been part of the response.

v1 used _uniqWith(included, _isEqual); to deduplicate included, while v2 is using a Map for included maintaining key uniqueness. There is no combination or merge behavior.

Merging relationship if it already exists in included should fix your issue.