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 1e3d5027110..a4f1c451686 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 06314dec577..d3816c8b64c 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 @@ 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); @@ -94,10 +98,18 @@ class ObjectSerializer $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 @@ 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/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php index afee7e684f2..df5405d178a 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 @@ 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); @@ -93,10 +97,18 @@ class ObjectSerializer $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 @@ 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]);