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
12 changes: 12 additions & 0 deletions features/hydra/error.feature
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,15 @@ Feature: Error handling
And the JSON node "@id" should be equal to "/relation_embedders/1"
And the JSON node "anotherRelated.@id" should be equal to "/related_dummies/1"
And the JSON node "anotherRelated.symfony" should be equal to "phalcon"

Scenario: Get an error because of sending bad type property
When I add "Content-Type" header equal to "application/json"
And I send a "POST" request to "/greetings" with body:
"""
{
"0": 1
}
"""
Then the response status code should be 201
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
2 changes: 1 addition & 1 deletion features/jsonld/context.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Feature: JSON-LD contexts generation
},
"jsonData": "Dummy/jsonData",
"arrayData": "Dummy/arrayData",
"nameConverted": "Dummy/nameConverted",
"name_converted": "Dummy/name_converted",
"name": "https://schema.org/name",
"alias": "https://schema.org/alternateName",
"foo": "Dummy/foo"
Expand Down
10 changes: 9 additions & 1 deletion src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,21 @@ public function denormalize(mixed $data, string $class, string $format = null, a
return $object;
}

$options = $this->getFactoryOptions($context);
$propertyNames = iterator_to_array($this->propertyNameCollectionFactory->create($resourceClass, $options));

// Revert attributes that aren't allowed to be changed after a post-denormalize check
foreach (array_keys($data) as $attribute) {
$attribute = $this->nameConverter ? $this->nameConverter->denormalize((string) $attribute) : $attribute;
if (!\in_array($attribute, $propertyNames, true)) {
continue;
}

if (!$this->canAccessAttributePostDenormalize($object, $previousObject, $attribute, $context)) {
if (null !== $previousObject) {
$this->setValue($object, $attribute, $this->propertyAccessor->getValue($previousObject, $attribute));
} else {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $attribute, $this->getFactoryOptions($context));
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $attribute, $options);
$this->setValue($object, $attribute, $propertyMetadata->getDefault());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/Resources/config/jsonld.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<argument type="service" id="api_platform.metadata.property.metadata_factory" />
<argument type="service" id="api_platform.router" />
<argument type="service" id="api_platform.symfony.iri_converter.skolem" />
<argument>null</argument>
<argument type="service" id="api_platform.name_converter"/>
</service>

<!-- Serializer -->
Expand Down
2 changes: 1 addition & 1 deletion tests/Serializer/ItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function testDenormalizeWithIri(): void
{
$context = ['resource_class' => Dummy::class, 'api_allow_update' => true];

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

Expand Down