From b8891af1e2ce0c2c89868f9ab7d863b93121b320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Thu, 17 Sep 2015 15:10:43 +0200 Subject: [PATCH] Avoid an error if the attribute isn't an array when expected --- JsonLd/Serializer/ItemNormalizer.php | 5 ++- ...ttributes.feature => invalid_data.feature} | 34 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) rename features/{unknown_attributes.feature => invalid_data.feature} (50%) diff --git a/JsonLd/Serializer/ItemNormalizer.php b/JsonLd/Serializer/ItemNormalizer.php index 82f2d1ae3a9..3b910f1f587 100644 --- a/JsonLd/Serializer/ItemNormalizer.php +++ b/JsonLd/Serializer/ItemNormalizer.php @@ -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( diff --git a/features/unknown_attributes.feature b/features/invalid_data.feature similarity index 50% rename from features/unknown_attributes.feature rename to features/invalid_data.feature index 7f9d1de7818..94ff21084b6 100644 --- a/features/unknown_attributes.feature +++ b/features/invalid_data.feature @@ -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: """ @@ -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 + } + """