Skip to content

Commit

Permalink
Fix enum serialization (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlevers committed Jul 30, 2022
1 parent ce045c0 commit be83e5f
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit be83e5f

Please sign in to comment.