From be83e5fd701a32d0a56b1a7a51729f72edf9f718 Mon Sep 17 00:00:00 2001 From: Jesse Evers Date: Sat, 30 Jul 2022 12:23:12 -0400 Subject: [PATCH] Fix enum serialization (#253) --- lib/ObjectSerializer.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/ObjectSerializer.php b/lib/ObjectSerializer.php index 1cd81f74c..ac7887ce5 100644 --- a/lib/ObjectSerializer.php +++ b/lib/ObjectSerializer.php @@ -72,21 +72,19 @@ public static function sanitizeForSerialization($data, $type = null, $format = n foreach ($data::openAPITypes() as $property => $openAPIType) { $getter = $data::getters()[$property]; $value = $data->$getter(); - if ($value !== null && !in_array($openAPIType, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { - $callable = [$openAPIType, 'getAllowableEnumValues']; - if (is_callable($callable)) { - /** array $callable */ - $allowedEnumTypes = $callable(); - if (!in_array((string)$value, $allowedEnumTypes, true)) { - $imploded = implode("', '", $allowedEnumTypes); - throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'"); - } - } - } if ($value !== null) { $values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]); } } + } else if (is_callable([$data, 'getAllowableEnumValues'])) { + $callable = [$data, 'getAllowableEnumValues']; + $allowedEnumTypes = $callable(); + if (!in_array((string)$data->value, $allowedEnumTypes, true)) { + $imploded = implode("', '", $allowedEnumTypes); + throw new \InvalidArgumentException("Invalid value for enum '$type', must be one of: '$imploded'"); + } + + return self::sanitizeForSerialization($data->value); } else { foreach($data as $property => $value) { $values[$property] = self::sanitizeForSerialization($value);