Skip to content

Commit 9421ba5

Browse files
authored
fix(serializer): propertyFilter should apply to arrays as well (#5444)
fix(PropertyFilter): should apply to arrays as well
1 parent 8a88e0c commit 9421ba5

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

features/filter/property_filter.feature

+8
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ Feature: Set properties to include
1818
And the JSON node "alias" should be equal to "Alias #0"
1919
And the JSON node "relatedDummy.name" should be equal to "RelatedDummy #1"
2020
And the JSON node "relatedDummies" should not exist
21+
22+
Scenario: Test property filter on not resource relations
23+
When I send a "GET" request to "/dummy-with-array-of-objects/1?properties[notResourceObject][]=foo&properties[arrayOfNotResourceObjects][]=bar"
24+
Then the JSON node "notResourceObject.foo" should be equal to "foo"
25+
And the JSON node "notResourceObject.bar" should not exist
26+
And the JSON node "arrayOfNotResourceObjects[0].foo" should not exist
27+
And the JSON node "arrayOfNotResourceObjects[0].bar" should be equal to "bar"
28+
And the JSON node "id" should not exist

src/Serializer/AbstractItemNormalizer.php

+7
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,13 @@ protected function getAttributeValue(object $object, string $attribute, string $
641641
return $this->serializer->normalize($attributeValue, $format, $childContext);
642642
}
643643

644+
if ($type && 'array' === $type->getBuiltinType()) {
645+
$childContext = $this->createChildContext($context, $attribute, $format);
646+
unset($childContext['iri'], $childContext['uri_variables']);
647+
648+
return $this->serializer->normalize($attributeValue, $format, $childContext);
649+
}
650+
644651
return $this->serializer->normalize($attributeValue, $format, $context);
645652
}
646653

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\Operation;
19+
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\DummyNonResource;
20+
21+
#[Get('/dummy-with-array-of-objects/{id}', filters: ['my_dummy.property'], provider: [DummyWithArrayOfNotResourceObjects::class, 'getData'])]
22+
class DummyWithArrayOfNotResourceObjects
23+
{
24+
public function __construct(
25+
public readonly int $id,
26+
#[ApiProperty(genId: false)]
27+
public readonly DummyNonResource $notResourceObject,
28+
/** @var array<DummyNonResource> */
29+
public readonly array $arrayOfNotResourceObjects
30+
) {
31+
}
32+
33+
public static function getData(Operation $operation, array $uriVariables = []): self
34+
{
35+
return new self(
36+
$uriVariables['id'],
37+
new DummyNonResource('foo', 'foo'),
38+
[
39+
new DummyNonResource('bar', 'bar'),
40+
new DummyNonResource('baz', 'baz'),
41+
]
42+
);
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Dto;
15+
16+
final class DummyNonResource
17+
{
18+
public function __construct(
19+
public ?string $foo = null,
20+
public ?string $bar = null
21+
) {
22+
}
23+
}

0 commit comments

Comments
 (0)