From eaa3c730fa705bd2405b1204718011b589f4bc7e Mon Sep 17 00:00:00 2001 From: Ian Lord Date: Thu, 2 Jul 2020 06:33:22 -0400 Subject: [PATCH] [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 --- .../src/main/resources/php/ObjectSerializer.mustache | 5 +++++ .../petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache index 4b4d39ad6be2..2694de44fe76 100644 --- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache @@ -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) { diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php index 1a12604649f8..4c7108c94b6f 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php @@ -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) {