Skip to content

Commit 666caa0

Browse files
authored
Merge pull request #2112 from ilicmsreten/2.3
Fix JSON API inlcude form when remove duplicate data
2 parents 14756cb + 049eb8f commit 666caa0

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,32 @@ public function thereAreDummyPropertyObjectsWithASharedGroup(int $nb)
244244
$this->manager->flush();
245245
}
246246

247+
/**
248+
* @Given there are :nb dummy property objects with different number of related groups
249+
*/
250+
public function thereAreDummyPropertyObjectsWithADifferentNumberRelatedGroups(int $nb)
251+
{
252+
for ($i = 1; $i <= $nb; ++$i) {
253+
$dummyGroup = new DummyGroup();
254+
$dummyProperty = new DummyProperty();
255+
256+
foreach (['foo', 'bar', 'baz'] as $property) {
257+
$dummyProperty->$property = $dummyGroup->$property = ucfirst($property).' #'.$i;
258+
}
259+
260+
$this->manager->persist($dummyGroup);
261+
$dummyGroups[$i] = $dummyGroup;
262+
263+
for ($j = 1; $j <= $i; ++$j) {
264+
$dummyProperty->groups[] = $dummyGroups[$j];
265+
}
266+
267+
$this->manager->persist($dummyProperty);
268+
}
269+
270+
$this->manager->flush();
271+
}
272+
247273
/**
248274
* @Given there are :nb dummy property objects with :nb2 groups
249275
*/

features/jsonapi/related-resouces-inclusion.feature

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,81 @@ Feature: JSON API Inclusion of Related Resources
584584
]
585585
}
586586
"""
587+
588+
@createSchema
589+
Scenario: Request inclusion of a related resources on collection should not duplicated included object
590+
Given there are 2 dummy property objects with different number of related groups
591+
When I send a "GET" request to "/dummy_properties?include=groups"
592+
Then the response status code should be 200
593+
And the response should be in JSON
594+
And the JSON should be valid according to the JSON API schema
595+
And the JSON should be deep equal to:
596+
"""
597+
{
598+
"links": {
599+
"self": "/dummy_properties?include=groups"
600+
},
601+
"meta": {
602+
"totalItems": 2,
603+
"itemsPerPage": 3,
604+
"currentPage": 1
605+
},
606+
"data": [{
607+
"id": "/dummy_properties/1",
608+
"type": "DummyProperty",
609+
"attributes": {
610+
"_id": 1,
611+
"foo": "Foo #1",
612+
"bar": "Bar #1",
613+
"baz": "Baz #1"
614+
},
615+
"relationships": {
616+
"groups": {
617+
"data": [{
618+
"type": "DummyGroup",
619+
"id": "/dummy_groups/1"
620+
}]
621+
}
622+
}
623+
}, {
624+
"id": "/dummy_properties/2",
625+
"type": "DummyProperty",
626+
"attributes": {
627+
"_id": 2,
628+
"foo": "Foo #2",
629+
"bar": "Bar #2",
630+
"baz": "Baz #2"
631+
},
632+
"relationships": {
633+
"groups": {
634+
"data": [{
635+
"type": "DummyGroup",
636+
"id": "/dummy_groups/1"
637+
}, {
638+
"type": "DummyGroup",
639+
"id": "/dummy_groups/2"
640+
}]
641+
}
642+
}
643+
}],
644+
"included": [{
645+
"id": "/dummy_groups/1",
646+
"type": "DummyGroup",
647+
"attributes": {
648+
"_id": 1,
649+
"foo": "Foo #1",
650+
"bar": "Bar #1",
651+
"baz": "Baz #1"
652+
}
653+
}, {
654+
"id": "/dummy_groups/2",
655+
"type": "DummyGroup",
656+
"attributes": {
657+
"_id": 2,
658+
"foo": "Foo #2",
659+
"bar": "Bar #2",
660+
"baz": "Baz #2"
661+
}
662+
}]
663+
}
664+
"""

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_unique(array_merge($data['included'] ?? [], $item['included']), SORT_REGULAR);
93+
$data['included'] = array_values(array_unique(array_merge($data['included'] ?? [], $item['included']), SORT_REGULAR));
9494
}
9595
}
9696

0 commit comments

Comments
 (0)