From 64dbe663f48d1e9a571f4bb97003c1cd237a2dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Leszczy=C5=84ski?= Date: Thu, 22 Jun 2023 08:58:43 +0200 Subject: [PATCH] Data deserialization problem after receiving response from api --- .../lib/ObjectSerializer.php | 45 +++++++++++++++++-- .../lib/ObjectSerializer.php | 45 +++++++++++++++++-- 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/samples/client/petstore-security-test/php/SwaggerClient-php/lib/ObjectSerializer.php b/samples/client/petstore-security-test/php/SwaggerClient-php/lib/ObjectSerializer.php index 4fcf557ae5a..a9a44a45ca3 100644 --- a/samples/client/petstore-security-test/php/SwaggerClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore-security-test/php/SwaggerClient-php/lib/ObjectSerializer.php @@ -301,12 +301,20 @@ public static function deserialize($data, $class, $httpHeaders = null) $instance = new $class(); foreach ($instance::swaggerTypes() as $property => $type) { $propertySetter = $instance::setters()[$property]; + $propertyName = self::convertToCamelCase($instance::attributeMap()[$property]); + if (!property_exists($data, $propertyName)) { + if (property_exists($data, self::convertCamelCaseToPascalCase($propertyName))) { + $propertyName = self::convertCamelCaseToPascalCase($propertyName); + } else { + $propertyName = self::convertCamelCaseToSnakeCase($propertyName); + } + } - if (!isset($propertySetter) || !isset($data->{$instance::attributeMap()[$property]})) { + if (!isset($propertySetter) || !isset($data->{$propertyName})) { continue; } - $propertyValue = $data->{$instance::attributeMap()[$property]}; + $propertyValue = $data->{$propertyName}; if (isset($propertyValue)) { $instance->$propertySetter(self::deserialize($propertyValue, $type, null)); } @@ -314,4 +322,35 @@ public static function deserialize($data, $class, $httpHeaders = null) return $instance; } } -} + + private static function convertToCamelCase($property): string + { + if (str_contains($property, '_')) { + $arrStrings = explode('_', $property); + $count = count($arrStrings); + if ($count > 1) { + foreach ($arrStrings as $key => &$string) { + if ($key > 0) { + $string = ucfirst($string); + } + } + } + return implode($arrStrings); + } + return lcfirst($property); + } + + private static function convertCamelCaseToPascalCase($property): string + { + return ucfirst($property); + } + + private static function convertCamelCaseToSnakeCase($property): string + { + $chunks = preg_split('/(?=[A-Z])/', $property); + forEach($chunks as &$chunk) { + $chunk = lcfirst($chunk); + } + return implode('_', $chunks); + } +} \ No newline at end of file diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php index 62d975f4b63..af0a9db64cf 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php @@ -302,12 +302,20 @@ public static function deserialize($data, $class, $httpHeaders = null) $instance = new $class(); foreach ($instance::swaggerTypes() as $property => $type) { $propertySetter = $instance::setters()[$property]; + $propertyName = self::convertToCamelCase($instance::attributeMap()[$property]); + if (!property_exists($data, $propertyName)) { + if (property_exists($data, self::convertCamelCaseToPascalCase($propertyName))) { + $propertyName = self::convertCamelCaseToPascalCase($propertyName); + } else { + $propertyName = self::convertCamelCaseToSnakeCase($propertyName); + } + } - if (!isset($propertySetter) || !isset($data->{$instance::attributeMap()[$property]})) { + if (!isset($propertySetter) || !isset($data->{$propertyName})) { continue; } - $propertyValue = $data->{$instance::attributeMap()[$property]}; + $propertyValue = $data->{$propertyName}; if (isset($propertyValue)) { $instance->$propertySetter(self::deserialize($propertyValue, $type, null)); } @@ -315,4 +323,35 @@ public static function deserialize($data, $class, $httpHeaders = null) return $instance; } } -} + + private static function convertToCamelCase($property): string + { + if (str_contains($property, '_')) { + $arrStrings = explode('_', $property); + $count = count($arrStrings); + if ($count > 1) { + foreach ($arrStrings as $key => &$string) { + if ($key > 0) { + $string = ucfirst($string); + } + } + } + return implode($arrStrings); + } + return lcfirst($property); + } + + private static function convertCamelCaseToPascalCase($property): string + { + return ucfirst($property); + } + + private static function convertCamelCaseToSnakeCase($property): string + { + $chunks = preg_split('/(?=[A-Z])/', $property); + forEach($chunks as &$chunk) { + $chunk = lcfirst($chunk); + } + return implode('_', $chunks); + } +} \ No newline at end of file