Skip to content

Commit

Permalink
[PHP] Better handling of invalid data (array) (#6760)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Herrick19 and wing328 authored Jul 2, 2020
1 parent 38ab738 commit eaa3c73
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ public static function deserialize($data, $class, $httpHeaders = null)
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) {
Expand Down

0 comments on commit eaa3c73

Please sign in to comment.