Skip to content

Commit

Permalink
fix handling with enums in object serializer (#16741)
Browse files Browse the repository at this point in the history
  • Loading branch information
reinfi committed Oct 7, 2023
1 parent 893154d commit f568001
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
)
);
}
}
}
}
Expand Down Expand Up @@ -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]);
Expand Down
22 changes: 17 additions & 5 deletions samples/client/echo_api/php-nextgen/src/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
)
);
}
}
}
}
Expand Down Expand Up @@ -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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
)
);
}
}
}
}
Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit f568001

Please sign in to comment.