From 5318319f4ee722eb457f4b82855d84496dd2bd17 Mon Sep 17 00:00:00 2001 From: Ian Lord Date: Wed, 24 Jun 2020 13:58:36 -0400 Subject: [PATCH 1/3] Add a deserialized Data Validator This part of code is really needed in my opinion for anyone wanting to take a JSON deserialize it and validate it. --- .../resources/php/ObjectSerializer.mustache | 25 +++++++++++++++++++ 1 file changed, 25 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..1e1aacfe51ac 100644 --- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache @@ -339,4 +339,29 @@ class ObjectSerializer return $instance; } } + + /** + * Verify the Data structure to make sure all the objects are validated + * @param mided $data object to be validated + * @throws \InvalidArgumentException + */ + public static function verifyDeserializedData ($data) { + if (is_object ($data) && $data instanceof {{invokerPackage}}\Model\ModelInterface) { + $a_invalidProperties = $data->listInvalidProperties(); + + if (count($a_invalidProperties) != 0) { + throw new \InvalidArgumentException(implode (' ', $a_invalidProperties)); + } + + foreach($data->getters() as $sProperty => &$sGetter) { + self::verifyDeserializedData($data->{$sGetter}()); + } + + } + else if (is_array ($data)) { + foreach ($data as $item) { + self::verifyDeserializedData($item); + } + } + } } From 803054a45d55569cc1a1514629161928903d99a8 Mon Sep 17 00:00:00 2001 From: Ian Lord Date: Wed, 24 Jun 2020 14:02:45 -0400 Subject: [PATCH 2/3] Pass by reference in the Array for performance --- .../src/main/resources/php/ObjectSerializer.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache index 1e1aacfe51ac..cf0046211d5e 100644 --- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache @@ -359,7 +359,7 @@ class ObjectSerializer } else if (is_array ($data)) { - foreach ($data as $item) { + foreach ($data as &$item) { self::verifyDeserializedData($item); } } From fa979d4843b2fbc244bb6b4db3c12b5dcb2d09b0 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 22 Oct 2020 11:22:25 +0800 Subject: [PATCH 3/3] update samples --- .../lib/ObjectSerializer.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php index 1a12604649f8..09958ab87cca 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php @@ -349,4 +349,29 @@ public static function deserialize($data, $class, $httpHeaders = null) return $instance; } } + + /** + * Verify the Data structure to make sure all the objects are validated + * @param mided $data object to be validated + * @throws \InvalidArgumentException + */ + public static function verifyDeserializedData ($data) { + if (is_object ($data) && $data instanceof OpenAPI\Client\Model\ModelInterface) { + $a_invalidProperties = $data->listInvalidProperties(); + + if (count($a_invalidProperties) != 0) { + throw new \InvalidArgumentException(implode (' ', $a_invalidProperties)); + } + + foreach($data->getters() as $sProperty => &$sGetter) { + self::verifyDeserializedData($data->{$sGetter}()); + } + + } + else if (is_array ($data)) { + foreach ($data as &$item) { + self::verifyDeserializedData($item); + } + } + } }