From f5680019b2e6e56cedd3d9d642bbed2bc0192ad6 Mon Sep 17 00:00:00 2001 From: Maddin <9324423+reinfi@users.noreply.github.com> Date: Sat, 7 Oct 2023 16:56:31 +0200 Subject: [PATCH] fix handling with enums in object serializer (#16741) --- .../php-nextgen/ObjectSerializer.mustache | 22 ++++++++++++++----- .../php-nextgen/src/ObjectSerializer.php | 22 ++++++++++++++----- .../src/ObjectSerializer.php | 22 ++++++++++++++----- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache index 1e3d50271107..a4f1c451686c 100644 --- a/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache @@ -68,6 +68,10 @@ class ObjectSerializer return ($format === 'date') ? $data->format('Y-m-d') : $data->format(self::$dateTimeFormat); } + if ($data instanceof \BackedEnum) { + return $data->value; + } + if (is_array($data)) { foreach ($data as $property => $value) { $data[$property] = self::sanitizeForSerialization($value); @@ -84,10 +88,18 @@ class ObjectSerializer $value = $data->$getter(); if ($value !== null && !in_array($openAPIType, [{{&primitives}}], true)) { if (is_subclass_of($openAPIType, '\BackedEnum')) { - $data = $openAPIType::tryFrom($data); - if ($data === null) { - $imploded = implode("', '", array_map(fn($case) => $case->value, $openAPIType::cases())); - throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'"); + if (is_scalar($value)) { + $value = $openAPIType::tryFrom($value); + if ($value === null) { + $imploded = implode("', '", array_map(fn($case) => $case->value, $openAPIType::cases())); + throw new \InvalidArgumentException( + sprintf( + "Invalid value for enum '%s', must be one of: '%s'", + $openAPIType::class, + $imploded + ) + ); + } } } } @@ -444,7 +456,7 @@ class ObjectSerializer // determine file name if ( is_array($httpHeaders) - && array_key_exists('Content-Disposition', $httpHeaders) + && array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match) ) { $filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . DIRECTORY_SEPARATOR . self::sanitizeFilename($match[1]); diff --git a/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php b/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php index 06314dec577c..d3816c8b64ca 100644 --- a/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php +++ b/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php @@ -78,6 +78,10 @@ public static function sanitizeForSerialization(mixed $data, string $type = null return ($format === 'date') ? $data->format('Y-m-d') : $data->format(self::$dateTimeFormat); } + if ($data instanceof \BackedEnum) { + return $data->value; + } + if (is_array($data)) { foreach ($data as $property => $value) { $data[$property] = self::sanitizeForSerialization($value); @@ -94,10 +98,18 @@ public static function sanitizeForSerialization(mixed $data, string $type = null $value = $data->$getter(); if ($value !== null && !in_array($openAPIType, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { if (is_subclass_of($openAPIType, '\BackedEnum')) { - $data = $openAPIType::tryFrom($data); - if ($data === null) { - $imploded = implode("', '", array_map(fn($case) => $case->value, $openAPIType::cases())); - throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'"); + if (is_scalar($value)) { + $value = $openAPIType::tryFrom($value); + if ($value === null) { + $imploded = implode("', '", array_map(fn($case) => $case->value, $openAPIType::cases())); + throw new \InvalidArgumentException( + sprintf( + "Invalid value for enum '%s', must be one of: '%s'", + $openAPIType::class, + $imploded + ) + ); + } } } } @@ -454,7 +466,7 @@ public static function deserialize(mixed $data, string $class, string $httpHeade // determine file name if ( is_array($httpHeaders) - && array_key_exists('Content-Disposition', $httpHeaders) + && array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match) ) { $filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . DIRECTORY_SEPARATOR . self::sanitizeFilename($match[1]); diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php index afee7e684f27..df5405d178ac 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php @@ -77,6 +77,10 @@ public static function sanitizeForSerialization(mixed $data, string $type = null return ($format === 'date') ? $data->format('Y-m-d') : $data->format(self::$dateTimeFormat); } + if ($data instanceof \BackedEnum) { + return $data->value; + } + if (is_array($data)) { foreach ($data as $property => $value) { $data[$property] = self::sanitizeForSerialization($value); @@ -93,10 +97,18 @@ public static function sanitizeForSerialization(mixed $data, string $type = null $value = $data->$getter(); if ($value !== null && !in_array($openAPIType, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { if (is_subclass_of($openAPIType, '\BackedEnum')) { - $data = $openAPIType::tryFrom($data); - if ($data === null) { - $imploded = implode("', '", array_map(fn($case) => $case->value, $openAPIType::cases())); - throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'"); + if (is_scalar($value)) { + $value = $openAPIType::tryFrom($value); + if ($value === null) { + $imploded = implode("', '", array_map(fn($case) => $case->value, $openAPIType::cases())); + throw new \InvalidArgumentException( + sprintf( + "Invalid value for enum '%s', must be one of: '%s'", + $openAPIType::class, + $imploded + ) + ); + } } } } @@ -453,7 +465,7 @@ public static function deserialize(mixed $data, string $class, string $httpHeade // determine file name if ( is_array($httpHeaders) - && array_key_exists('Content-Disposition', $httpHeaders) + && array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match) ) { $filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . DIRECTORY_SEPARATOR . self::sanitizeFilename($match[1]);