Skip to content

Commit

Permalink
Merge pull request #276 from dunglas/check_attribute_value_type
Browse files Browse the repository at this point in the history
Avoid an error if the attribute isn't an array when expected
  • Loading branch information
dunglas committed Sep 17, 2015
2 parents 72fc8e1 + b8891af commit d9dfe60
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
5 changes: 4 additions & 1 deletion JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,14 @@ public function denormalize($data, $class, $format = null, array $context = [])
$type = $types[0];

if (
$attributeValue &&
$type->isCollection() &&
($collectionType = $type->getCollectionType()) &&
($class = $collectionType->getClass())
) {
if (!is_array($attributeValue)) {
continue;
}

$values = [];
foreach ($attributeValue as $index => $obj) {
$values[$index] = $this->denormalizeRelation(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Feature: Ignore unknown attributes
In order to be robust
Feature: Handle properly invalid data submitted to the API
In order to have robust API
As a client software developer
I can send unsupported attributes that will be ignored

@createSchema
@dropSchema
Scenario: Create a resource
When I send a "POST" request to "/dummies" with body:
"""
Expand Down Expand Up @@ -32,3 +31,32 @@ Feature: Ignore unknown attributes
"name_converted": null
}
"""

@dropSchema
Scenario: Send non-array data when an array is expected
When I send a "POST" request to "/dummies" with body:
"""
{
"name": "Invalid",
"relatedDummies": "hello"
}
"""
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"
"""
And the JSON should be equal to:
{
"@context": "/contexts/Dummy",
"@id": "/dummies/2",
"@type": "Dummy",
"name": "Invalid",
"alias": null,
"dummyDate": null,
"jsonData": [],
"dummy": null,
"relatedDummy": null,
"relatedDummies": [],
"name_converted": null
}
"""

0 comments on commit d9dfe60

Please sign in to comment.