Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions features/filter/property_filter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ Feature: Set properties to include
And the JSON node "alias" should be equal to "Alias #0"
And the JSON node "relatedDummy.name" should be equal to "RelatedDummy #1"
And the JSON node "relatedDummies" should not exist

Scenario: Test property filter on not resource relations
When I send a "GET" request to "/dummy-with-array-of-objects/1?properties[notResourceObject][]=foo&properties[arrayOfNotResourceObjects][]=bar"
Then the JSON node "notResourceObject.foo" should be equal to "foo"
And the JSON node "notResourceObject.bar" should not exist
And the JSON node "arrayOfNotResourceObjects[0].foo" should not exist
And the JSON node "arrayOfNotResourceObjects[0].bar" should be equal to "bar"
And the JSON node "id" should not exist
7 changes: 7 additions & 0 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,13 @@ protected function getAttributeValue(object $object, string $attribute, string $
return $this->serializer->normalize($attributeValue, $format, $childContext);
}

if ($type && 'array' === $type->getBuiltinType()) {
$childContext = $this->createChildContext($context, $attribute, $format);
unset($childContext['iri'], $childContext['uri_variables']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: check if we shouldn't remove operation also

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you explain me what choices lead to unset() these contexts?
because we're serializing "non api resource" objects, there would be no need to have the operation available in the context?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it'll re-use the old operation if any when creating an IRI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless I'm misunderstanding something, no IRI should be generated, since we're dealing with non-apiresource objects?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they have Skolem IRIs unless disabled by using the genId: false attribute.


return $this->serializer->normalize($attributeValue, $format, $childContext);
}

return $this->serializer->normalize($attributeValue, $format, $context);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\DummyNonResource;

#[Get('/dummy-with-array-of-objects/{id}', filters: ['my_dummy.property'], provider: [DummyWithArrayOfNotResourceObjects::class, 'getData'])]
class DummyWithArrayOfNotResourceObjects
{
public function __construct(
public readonly int $id,
#[ApiProperty(genId: false)]
public readonly DummyNonResource $notResourceObject,
/** @var array<DummyNonResource> */
public readonly array $arrayOfNotResourceObjects
) {
}

public static function getData(Operation $operation, array $uriVariables = []): self
{
return new self(
$uriVariables['id'],
new DummyNonResource('foo', 'foo'),
[
new DummyNonResource('bar', 'bar'),
new DummyNonResource('baz', 'baz'),
]
);
}
}
23 changes: 23 additions & 0 deletions tests/Fixtures/TestBundle/Dto/DummyNonResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Dto;

final class DummyNonResource
{
public function __construct(
public ?string $foo = null,
public ?string $bar = null
) {
}
}