diff --git a/features/main/relation.feature b/features/main/relation.feature index d43d381e110..bb4b5850f28 100644 --- a/features/main/relation.feature +++ b/features/main/relation.feature @@ -442,3 +442,28 @@ Feature: Relations support "related": null } """ + + Scenario: Issue #1547 Passing wrong argument to a relation + When I add "Content-Type" header equal to "application/ld+json" + And I send a "POST" request to "/relation_embedders" with body: + """ + { + "related": "certainly not an iri" + } + """ + Then the response status code should be 400 + 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 node "hydra:description" should contain "Expected IRI or nested document" + + When I add "Content-Type" header equal to "application/ld+json" + And I send a "POST" request to "/relation_embedders" with body: + """ + { + "related": 8 + } + """ + Then the response status code should be 400 + 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 node "hydra:description" should contain "Expected IRI or nested document" diff --git a/src/Serializer/AbstractItemNormalizer.php b/src/Serializer/AbstractItemNormalizer.php index cc0054e5fdb..dd42ebc5159 100644 --- a/src/Serializer/AbstractItemNormalizer.php +++ b/src/Serializer/AbstractItemNormalizer.php @@ -285,7 +285,10 @@ private function denormalizeRelation(string $attributeName, PropertyMetadata $pr } } - if (!$this->resourceClassResolver->isResourceClass($className) || $propertyMetadata->isWritableLink()) { + if ( + !$this->resourceClassResolver->isResourceClass($className) || + ($propertyMetadata->isWritableLink() && is_array($value)) + ) { return $this->serializer->denormalize($value, $className, $format, $this->createRelationSerializationContext($className, $context)); }