From 4188f532b677646108e47aa1d3fd3ba9e22f2cd9 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Wed, 6 Dec 2017 07:33:23 +0100 Subject: [PATCH] Fix: Passing nor an iri or a array in an embedded relation throws an error --- features/main/relation.feature | 25 +++++++++++++++++++++++ src/Serializer/AbstractItemNormalizer.php | 5 ++++- 2 files changed, 29 insertions(+), 1 deletion(-) 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)); }