Skip to content

Commit 1da849f

Browse files
authored
Merge pull request #2088 from antograssiot/tests-json-api-inclusion
Remove duplicate data from JSON API include
2 parents 80bccbd + c43a02a commit 1da849f

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,31 @@ public function thereAreDummyPropertyObjects(int $nb)
219219
$this->manager->flush();
220220
}
221221

222+
/**
223+
* @Given there are :nb dummy property objects with a shared group
224+
*/
225+
public function thereAreDummyPropertyObjectsWithASharedGroup(int $nb)
226+
{
227+
$dummyGroup = new DummyGroup();
228+
foreach (['foo', 'bar', 'baz'] as $property) {
229+
$dummyGroup->$property = ucfirst($property).' #shared';
230+
}
231+
$this->manager->persist($dummyGroup);
232+
233+
for ($i = 1; $i <= $nb; ++$i) {
234+
$dummyProperty = new DummyProperty();
235+
236+
foreach (['foo', 'bar', 'baz'] as $property) {
237+
$dummyProperty->$property = ucfirst($property).' #'.$i;
238+
}
239+
240+
$dummyProperty->group = $dummyGroup;
241+
$this->manager->persist($dummyProperty);
242+
}
243+
244+
$this->manager->flush();
245+
}
246+
222247
/**
223248
* @Given there are :nb dummy property objects with :nb2 groups
224249
*/

features/jsonapi/related-resouces-inclusion.feature

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,91 @@ Feature: JSON API Inclusion of Related Resources
496496
}
497497
"""
498498

499+
@createSchema
500+
Scenario: Request inclusion of a related resources on collection should not duplicated included object
501+
Given there are 3 dummy property objects with a shared group
502+
When I send a "GET" request to "/dummy_properties?include=group"
503+
Then the response status code should be 200
504+
And the response should be in JSON
505+
And the JSON should be valid according to the JSON API schema
506+
And the JSON should be deep equal to:
507+
"""
508+
{
509+
"links": {
510+
"self": "\/dummy_properties?include=group"
511+
},
512+
"meta": {
513+
"totalItems": 3,
514+
"itemsPerPage": 3,
515+
"currentPage": 1
516+
},
517+
"data": [
518+
{
519+
"id": "/dummy_properties/1",
520+
"type": "DummyProperty",
521+
"attributes": {
522+
"_id": 1,
523+
"foo": "Foo #1",
524+
"bar": "Bar #1",
525+
"baz": "Baz #1"
526+
},
527+
"relationships": {
528+
"group": {
529+
"data": {
530+
"type": "DummyGroup",
531+
"id": "/dummy_groups/1"
532+
}
533+
}
534+
}
535+
},
536+
{
537+
"id": "/dummy_properties/2",
538+
"type": "DummyProperty",
539+
"attributes": {
540+
"_id": 2,
541+
"foo": "Foo #2",
542+
"bar": "Bar #2",
543+
"baz": "Baz #2"
544+
},
545+
"relationships": {
546+
"group": {
547+
"data": {
548+
"type": "DummyGroup",
549+
"id": "/dummy_groups/1"
550+
}
551+
}
552+
}
553+
},
554+
{
555+
"id": "/dummy_properties/3",
556+
"type": "DummyProperty",
557+
"attributes": {
558+
"_id": 3,
559+
"foo": "Foo #3",
560+
"bar": "Bar #3",
561+
"baz": "Baz #3"
562+
},
563+
"relationships": {
564+
"group": {
565+
"data": {
566+
"type": "DummyGroup",
567+
"id": "/dummy_groups/1"
568+
}
569+
}
570+
}
571+
}
572+
],
573+
"included": [
574+
{
575+
"id": "\/dummy_groups\/1",
576+
"type": "DummyGroup",
577+
"attributes": {
578+
"_id": 1,
579+
"foo": "Foo #shared",
580+
"bar": "Bar #shared",
581+
"baz": "Baz #shared"
582+
}
583+
}
584+
]
585+
}
586+
"""

src/JsonApi/Serializer/CollectionNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function getItemsData($object, string $format = null, array $context =
9090
$data['data'][] = $item['data'];
9191

9292
if (isset($item['included'])) {
93-
$data['included'] = array_merge($data['included'] ?? [], $item['included']);
93+
$data['included'] = array_unique(array_merge($data['included'] ?? [], $item['included']), SORT_REGULAR);
9494
}
9595
}
9696

0 commit comments

Comments
 (0)