Skip to content

Commit

Permalink
fix: Pass the child context when normalizing nested non-resource obje…
Browse files Browse the repository at this point in the history
…cts (api-platform#4521)

* fix: Pass the child context when normalizing nested non-resource objects

Currently, the `createChildContext()` method isn't used when normalizing embedded objects that are not resources. Hence, for example, the `PropertiesFilter` won't work for these objects.

This PR fixes that by invoking `createChildContext()` also for nested non-resource objects.

* Add behat test

Co-authored-by: Bernhard Schussek <[email protected]>
  • Loading branch information
norkunas and webmozart authored Oct 22, 2021
1 parent 3063220 commit 5272238
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
28 changes: 28 additions & 0 deletions features/jsonld/non_resource.feature
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ Feature: JSON-LD non-resource handling
}
"""

Scenario: Get a resource containing a raw object with selected properties
Given there are 1 dummy objects with relatedDummy and its thirdLevel
When I send a "GET" request to "/contain_non_resources/1?properties[]=id&properties[nested][notAResource][]=foo&properties[notAResource][]=bar"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be a superset of:
"""
{
"@context": "/contexts/ContainNonResource",
"@id": "/contain_non_resources/1",
"@type": "ContainNonResource",
"id": 1,
"nested": {
"@id": "/contain_non_resources/1-nested",
"@type": "ContainNonResource",
"notAResource": {
"@type": "NotAResource",
"foo": "f2"
}
},
"notAResource": {
"@type": "NotAResource",
"bar": "b1"
}
}
"""

@!mongodb
@createSchema
Scenario: Create a resource that has a non-resource relation.
Expand Down
11 changes: 11 additions & 0 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,17 @@ protected function getAttributeValue($object, $attribute, $format = null, array

unset($context['resource_class']);

if ($type && $type->getClassName()) {
if (!\is_object($attributeValue) && null !== $attributeValue) {
throw new UnexpectedValueException('Unexpected non-object value for object property.');
}

$childContext = $this->createChildContext($context, $attribute, $format);
unset($childContext['iri']);

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

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

Expand Down
7 changes: 5 additions & 2 deletions tests/Fixtures/TestBundle/Document/ContainNonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
* @ODM\Document
*
* @ApiResource(
* normalizationContext={
* "groups"={"contain_non_resource"},
* attributes={
* "filters"={"my_dummy.property"}
* },
* normalizationContext={
* "groups"={"contain_non_resource"}
* }
* )
*
* @author Kévin Dunglas <[email protected]>
Expand Down
7 changes: 5 additions & 2 deletions tests/Fixtures/TestBundle/Entity/ContainNonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
* @ORM\Entity
*
* @ApiResource(
* normalizationContext={
* "groups"={"contain_non_resource"},
* attributes={
* "filters"={"my_dummy.property"}
* },
* normalizationContext={
* "groups"={"contain_non_resource"}
* }
* )
*
* @author Kévin Dunglas <[email protected]>
Expand Down

0 comments on commit 5272238

Please sign in to comment.