[PHP] Better handling of invalid data (array) (#6760)

* Update ObjectSerializer.mustache

If the $data is a wrongly formatted Json or if data is not an array, php gives error:

Invalid argument supplied for foreach() at line 257 (Now line is 262)

* update samples

Co-authored-by: William Cheng <wing328hk@gmail.com>
This commit is contained in:
Ian Lord
2020-07-02 06:33:22 -04:00
committed by GitHub
parent 38ab7383f2
commit eaa3c730fa
2 changed files with 10 additions and 0 deletions

View File

@@ -252,6 +252,11 @@ class ObjectSerializer
return null;
} elseif (strcasecmp(substr($class, -2), '[]') === 0) {
$data = is_string($data) ? json_decode($data) : $data;
if (!is_array($data)) {
throw new \InvalidArgumentException("Invalid array '$class'");
}
$subClass = substr($class, 0, -2);
$values = [];
foreach ($data as $key => $value) {

View File

@@ -262,6 +262,11 @@ class ObjectSerializer
return null;
} elseif (strcasecmp(substr($class, -2), '[]') === 0) {
$data = is_string($data) ? json_decode($data) : $data;
if (!is_array($data)) {
throw new \InvalidArgumentException("Invalid array '$class'");
}
$subClass = substr($class, 0, -2);
$values = [];
foreach ($data as $key => $value) {