Skip to content

Commit

Permalink
Merge c982f32 into c01aea8
Browse files Browse the repository at this point in the history
  • Loading branch information
kate-kate committed Apr 22, 2017
2 parents c01aea8 + c982f32 commit 9e522c3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
31 changes: 30 additions & 1 deletion features/main/relation.feature
Expand Up @@ -321,7 +321,6 @@ Feature: Relations support
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"

@dropSchema
Scenario: Update an embedded relation
When I add "Content-Type" header equal to "application/ld+json"
And I send a "PUT" request to "/relation_embedders/2" with body:
Expand Down Expand Up @@ -352,3 +351,33 @@ Feature: Relations support
"related": null
}
"""

@dropSchema
Scenario: Validation error on empty string relation
Given there is a DummyCar entity with related colors
When I add "Content-Type" header equal to "application/ld+json"
And I send a "PUT" request to "/dummy_car_colors/1" with body:
"""
{
"prop": "red",
"car": ""
}
"""
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 should be equal to:
"""
{
"@context": "\/contexts\/ConstraintViolationList",
"@type": "ConstraintViolationList",
"hydra:title": "An error occurred",
"hydra:description": "car: This value should not be blank.",
"violations": [
{
"propertyPath": "car",
"message": "This value should not be blank."
}
]
}
"""
3 changes: 3 additions & 0 deletions src/Serializer/AbstractItemNormalizer.php
Expand Up @@ -280,6 +280,9 @@ private function denormalizeRelation(string $attributeName, PropertyMetadata $pr
} catch (ItemNotFoundException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (InvalidArgumentException $e) {
if ('' === $value && ($propertyMetadata->isWritableLink() || $this->resourceClassResolver->isResourceClass($className))) {
return null;
}
// Give a chance to other normalizers (e.g.: DateTimeNormalizer)
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestBundle/Entity/DummyCarColor.php
Expand Up @@ -70,7 +70,7 @@ public function getCar()
*
* @return static
*/
public function setCar(DummyCar $car)
public function setCar(DummyCar $car = null)
{
$this->car = $car;

Expand Down

0 comments on commit 9e522c3

Please sign in to comment.