Skip to content

Commit 2fbf647

Browse files
authored
Merge pull request #1547 from jdeniau/fix-relationWithWrongIri
Fix: Passing nor an unknown iri nor an array in an embedded relation throws an error
2 parents 29e6490 + 4188f53 commit 2fbf647

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

features/main/relation.feature

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,28 @@ Feature: Relations support
442442
"related": null
443443
}
444444
"""
445+
446+
Scenario: Issue #1547 Passing wrong argument to a relation
447+
When I add "Content-Type" header equal to "application/ld+json"
448+
And I send a "POST" request to "/relation_embedders" with body:
449+
"""
450+
{
451+
"related": "certainly not an iri"
452+
}
453+
"""
454+
Then the response status code should be 400
455+
And the response should be in JSON
456+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
457+
And the JSON node "hydra:description" should contain "Expected IRI or nested document"
458+
459+
When I add "Content-Type" header equal to "application/ld+json"
460+
And I send a "POST" request to "/relation_embedders" with body:
461+
"""
462+
{
463+
"related": 8
464+
}
465+
"""
466+
Then the response status code should be 400
467+
And the response should be in JSON
468+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
469+
And the JSON node "hydra:description" should contain "Expected IRI or nested document"

src/Serializer/AbstractItemNormalizer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,10 @@ private function denormalizeRelation(string $attributeName, PropertyMetadata $pr
285285
}
286286
}
287287

288-
if (!$this->resourceClassResolver->isResourceClass($className) || $propertyMetadata->isWritableLink()) {
288+
if (
289+
!$this->resourceClassResolver->isResourceClass($className) ||
290+
($propertyMetadata->isWritableLink() && is_array($value))
291+
) {
289292
return $this->serializer->denormalize($value, $className, $format, $this->createRelationSerializationContext($className, $context));
290293
}
291294

0 commit comments

Comments
 (0)