Skip to content

Commit 138c512

Browse files
authored
fix(serializer): skip unknown property and use the name converter
* fix(symfony): pass 'api_platform.name_converter' as argument to JsonLd\ContextBuilder * fix(serializer): skip if the property is not found
1 parent aa2dfe7 commit 138c512

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

features/hydra/error.feature

+12
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,15 @@ Feature: Error handling
139139
And the JSON node "@id" should be equal to "/relation_embedders/1"
140140
And the JSON node "anotherRelated.@id" should be equal to "/related_dummies/1"
141141
And the JSON node "anotherRelated.symfony" should be equal to "phalcon"
142+
143+
Scenario: Get an error because of sending bad type property
144+
When I add "Content-Type" header equal to "application/json"
145+
And I send a "POST" request to "/greetings" with body:
146+
"""
147+
{
148+
"0": 1
149+
}
150+
"""
151+
Then the response status code should be 201
152+
And the response should be in JSON
153+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"

features/jsonld/context.feature

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Feature: JSON-LD contexts generation
4040
},
4141
"jsonData": "Dummy/jsonData",
4242
"arrayData": "Dummy/arrayData",
43-
"nameConverted": "Dummy/nameConverted",
43+
"name_converted": "Dummy/name_converted",
4444
"name": "https://schema.org/name",
4545
"alias": "https://schema.org/alternateName",
4646
"foo": "Dummy/foo"

src/Serializer/AbstractItemNormalizer.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,21 @@ public function denormalize(mixed $data, string $class, string $format = null, a
222222
return $object;
223223
}
224224

225+
$options = $this->getFactoryOptions($context);
226+
$propertyNames = iterator_to_array($this->propertyNameCollectionFactory->create($resourceClass, $options));
227+
225228
// Revert attributes that aren't allowed to be changed after a post-denormalize check
226229
foreach (array_keys($data) as $attribute) {
230+
$attribute = $this->nameConverter ? $this->nameConverter->denormalize((string) $attribute) : $attribute;
231+
if (!\in_array($attribute, $propertyNames, true)) {
232+
continue;
233+
}
234+
227235
if (!$this->canAccessAttributePostDenormalize($object, $previousObject, $attribute, $context)) {
228236
if (null !== $previousObject) {
229237
$this->setValue($object, $attribute, $this->propertyAccessor->getValue($previousObject, $attribute));
230238
} else {
231-
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $attribute, $this->getFactoryOptions($context));
239+
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $attribute, $options);
232240
$this->setValue($object, $attribute, $propertyMetadata->getDefault());
233241
}
234242
}

src/Symfony/Bundle/Resources/config/jsonld.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<argument type="service" id="api_platform.metadata.property.metadata_factory" />
1313
<argument type="service" id="api_platform.router" />
1414
<argument type="service" id="api_platform.symfony.iri_converter.skolem" />
15-
<argument>null</argument>
15+
<argument type="service" id="api_platform.name_converter"/>
1616
</service>
1717

1818
<!-- Serializer -->

tests/Serializer/ItemNormalizerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function testDenormalizeWithIri(): void
143143
{
144144
$context = ['resource_class' => Dummy::class, 'api_allow_update' => true];
145145

146-
$propertyNameCollection = new PropertyNameCollection(['name']);
146+
$propertyNameCollection = new PropertyNameCollection(['id', 'name']);
147147
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
148148
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled();
149149

0 commit comments

Comments
 (0)