Skip to content

Commit d9dfe60

Browse files
committed
Merge pull request #276 from dunglas/check_attribute_value_type
Avoid an error if the attribute isn't an array when expected
2 parents 72fc8e1 + b8891af commit d9dfe60

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

JsonLd/Serializer/ItemNormalizer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,14 @@ public function denormalize($data, $class, $format = null, array $context = [])
236236
$type = $types[0];
237237

238238
if (
239-
$attributeValue &&
240239
$type->isCollection() &&
241240
($collectionType = $type->getCollectionType()) &&
242241
($class = $collectionType->getClass())
243242
) {
243+
if (!is_array($attributeValue)) {
244+
continue;
245+
}
246+
244247
$values = [];
245248
foreach ($attributeValue as $index => $obj) {
246249
$values[$index] = $this->denormalizeRelation(

features/unknown_attributes.feature renamed to features/invalid_data.feature

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
Feature: Ignore unknown attributes
2-
In order to be robust
1+
Feature: Handle properly invalid data submitted to the API
2+
In order to have robust API
33
As a client software developer
44
I can send unsupported attributes that will be ignored
55

66
@createSchema
7-
@dropSchema
87
Scenario: Create a resource
98
When I send a "POST" request to "/dummies" with body:
109
"""
@@ -32,3 +31,32 @@ Feature: Ignore unknown attributes
3231
"name_converted": null
3332
}
3433
"""
34+
35+
@dropSchema
36+
Scenario: Send non-array data when an array is expected
37+
When I send a "POST" request to "/dummies" with body:
38+
"""
39+
{
40+
"name": "Invalid",
41+
"relatedDummies": "hello"
42+
}
43+
"""
44+
Then the response status code should be 201
45+
And the response should be in JSON
46+
And the header "Content-Type" should be equal to "application/ld+json"
47+
"""
48+
And the JSON should be equal to:
49+
{
50+
"@context": "/contexts/Dummy",
51+
"@id": "/dummies/2",
52+
"@type": "Dummy",
53+
"name": "Invalid",
54+
"alias": null,
55+
"dummyDate": null,
56+
"jsonData": [],
57+
"dummy": null,
58+
"relatedDummy": null,
59+
"relatedDummies": [],
60+
"name_converted": null
61+
}
62+
"""

0 commit comments

Comments
 (0)