Skip to content

Commit 5272238

Browse files
norkunaswebmozart
andauthored
fix: Pass the child context when normalizing nested non-resource objects (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]>
1 parent 3063220 commit 5272238

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

features/jsonld/non_resource.feature

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ Feature: JSON-LD non-resource handling
3838
}
3939
"""
4040

41+
Scenario: Get a resource containing a raw object with selected properties
42+
Given there are 1 dummy objects with relatedDummy and its thirdLevel
43+
When I send a "GET" request to "/contain_non_resources/1?properties[]=id&properties[nested][notAResource][]=foo&properties[notAResource][]=bar"
44+
Then the response status code should be 200
45+
And the response should be in JSON
46+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
47+
And the JSON should be a superset of:
48+
"""
49+
{
50+
"@context": "/contexts/ContainNonResource",
51+
"@id": "/contain_non_resources/1",
52+
"@type": "ContainNonResource",
53+
"id": 1,
54+
"nested": {
55+
"@id": "/contain_non_resources/1-nested",
56+
"@type": "ContainNonResource",
57+
"notAResource": {
58+
"@type": "NotAResource",
59+
"foo": "f2"
60+
}
61+
},
62+
"notAResource": {
63+
"@type": "NotAResource",
64+
"bar": "b1"
65+
}
66+
}
67+
"""
68+
4169
@!mongodb
4270
@createSchema
4371
Scenario: Create a resource that has a non-resource relation.

src/Serializer/AbstractItemNormalizer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,17 @@ protected function getAttributeValue($object, $attribute, $format = null, array
639639

640640
unset($context['resource_class']);
641641

642+
if ($type && $type->getClassName()) {
643+
if (!\is_object($attributeValue) && null !== $attributeValue) {
644+
throw new UnexpectedValueException('Unexpected non-object value for object property.');
645+
}
646+
647+
$childContext = $this->createChildContext($context, $attribute, $format);
648+
unset($childContext['iri']);
649+
650+
return $this->serializer->normalize($attributeValue, $format, $childContext);
651+
}
652+
642653
return $this->serializer->normalize($attributeValue, $format, $context);
643654
}
644655

tests/Fixtures/TestBundle/Document/ContainNonResource.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
* @ODM\Document
2525
*
2626
* @ApiResource(
27-
* normalizationContext={
28-
* "groups"={"contain_non_resource"},
27+
* attributes={
28+
* "filters"={"my_dummy.property"}
2929
* },
30+
* normalizationContext={
31+
* "groups"={"contain_non_resource"}
32+
* }
3033
* )
3134
*
3235
* @author Kévin Dunglas <[email protected]>

tests/Fixtures/TestBundle/Entity/ContainNonResource.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
* @ORM\Entity
2525
*
2626
* @ApiResource(
27-
* normalizationContext={
28-
* "groups"={"contain_non_resource"},
27+
* attributes={
28+
* "filters"={"my_dummy.property"}
2929
* },
30+
* normalizationContext={
31+
* "groups"={"contain_non_resource"}
32+
* }
3033
* )
3134
*
3235
* @author Kévin Dunglas <[email protected]>

0 commit comments

Comments
 (0)