forked from loafle/openapi-generator-original
* fixed enum template; added enum handling to ObjectSerializer * regenerated templates * improved InvalidArgumentException message * regenerated sample client * added value check of OuterEnum during sanitization * regenerated samples
This commit is contained in:
@@ -49,10 +49,16 @@ class ObjectSerializer
|
||||
return $data;
|
||||
} elseif (is_object($data)) {
|
||||
$values = [];
|
||||
foreach (array_keys($data::swaggerTypes()) as $property) {
|
||||
foreach ($data::swaggerTypes() as $property => $swaggerType) {
|
||||
$getter = $data::getters()[$property];
|
||||
if ($data->$getter() !== null) {
|
||||
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($data->$getter());
|
||||
$value = $data->$getter();
|
||||
if (method_exists($swaggerType, 'getAllowableEnumValues')
|
||||
&& !in_array($value, $swaggerType::getAllowableEnumValues())) {
|
||||
$imploded = implode("', '", $swaggerType::getAllowableEnumValues());
|
||||
throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'");
|
||||
}
|
||||
if ($value !== null) {
|
||||
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value);
|
||||
}
|
||||
}
|
||||
return (object)$values;
|
||||
@@ -259,6 +265,12 @@ class ObjectSerializer
|
||||
}
|
||||
|
||||
return $deserialized;
|
||||
} elseif (method_exists($class, 'getAllowableEnumValues')) {
|
||||
if (!in_array($data, $class::getAllowableEnumValues())) {
|
||||
$imploded = implode("', '", $class::getAllowableEnumValues());
|
||||
throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'");
|
||||
}
|
||||
return $data;
|
||||
} else {
|
||||
// If a discriminator is defined and points to a valid subclass, use it.
|
||||
$discriminator = $class::DISCRIMINATOR;
|
||||
|
||||
@@ -20,8 +20,10 @@
|
||||
*/
|
||||
|
||||
namespace {{modelPackage}};
|
||||
{{^isEnum}}
|
||||
|
||||
use \ArrayAccess;
|
||||
{{/isEnum}}
|
||||
|
||||
/**
|
||||
* {{classname}} Class Doc Comment
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
class {{classname}} {
|
||||
/**
|
||||
* Possible values of this enum
|
||||
*/
|
||||
{{#allowableValues}}{{#enumVars}}const {{{name}}} = {{{value}}};
|
||||
{{/enumVars}}{{/allowableValues}}
|
||||
|
||||
{{#vars}}{{#isEnum}}
|
||||
/**
|
||||
* Gets allowable values of the enum
|
||||
* @return string[]
|
||||
*/
|
||||
public function {{getter}}AllowableValues()
|
||||
public static function getAllowableEnumValues()
|
||||
{
|
||||
return [
|
||||
{{#allowableValues}}{{#enumVars}}self::{{datatypeWithEnum}}_{{{name}}},{{^-last}}
|
||||
{{#allowableValues}}{{#enumVars}}self::{{{name}}},{{^-last}}
|
||||
{{/-last}}{{/enumVars}}{{/allowableValues}}
|
||||
];
|
||||
}
|
||||
{{/isEnum}}{{/vars}}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user