From 0b8acb5b0c0400a82e30c464712c52d81132494a Mon Sep 17 00:00:00 2001 From: wing328 Date: Tue, 2 Aug 2016 17:34:12 +0800 Subject: [PATCH] set allowablevalues of inner enum's allowablevalues --- .../io/swagger/codegen/DefaultCodegen.java | 22 +- .../main/resources/php/model_generic.mustache | 10 + ...ith-fake-endpoints-models-for-testing.yaml | 19 ++ .../docs/Model/EnumArrays.md | 11 + .../lib/Model/EnumArrays.php | 299 ++++++++++++++++++ .../SwaggerClient-php/lib/Model/EnumTest.php | 3 + .../SwaggerClient-php/lib/Model/MapTest.php | 17 +- .../php/SwaggerClient-php/lib/Model/Order.php | 1 + .../php/SwaggerClient-php/lib/Model/Pet.php | 1 + .../lib/ObjectSerializer.php | 2 +- .../test/Model/EnumArraysTest.php | 114 +++++++ .../test/Model/ModelListTest.php | 2 +- samples/client/petstore/ruby/README.md | 3 +- samples/client/petstore/ruby/lib/petstore.rb | 1 + 14 files changed, 489 insertions(+), 16 deletions(-) create mode 100644 samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md create mode 100644 samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php create mode 100644 samples/client/petstore/php/SwaggerClient-php/test/Model/EnumArraysTest.php diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 47df8cded35..3e9be96f76f 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -1334,7 +1334,7 @@ public class DefaultCodegen { property.name = toVarName(name); property.baseName = name; - property.nameInCamelCase = camelize(name, false); + property.nameInCamelCase = camelize(property.name, false); property.description = escapeText(p.getDescription()); property.unescapedDescription = p.getDescription(); property.getter = "get" + getterAndSetterCapitalize(name); @@ -1606,11 +1606,14 @@ public class DefaultCodegen { property.items = innerProperty; // inner item is Enum if (isPropertyInnerMostEnum(property)) { + // isEnum is set to true when the type is an enum + // or the inner type of an array/map is an enum property.isEnum = true; // update datatypeWithEnum and default value for array // e.g. List => List updateDataTypeWithEnumForArray(property); - + // set allowable values to enum values (including array/map of enum) + property.allowableValues = getInnerEnumAllowableValues(property); } } } @@ -1633,10 +1636,14 @@ public class DefaultCodegen { property.items = innerProperty; // inner item is Enum if (isPropertyInnerMostEnum(property)) { + // isEnum is set to true when the type is an enum + // or the inner type of an array/map is an enum property.isEnum = true; // update datatypeWithEnum and default value for map // e.g. Dictionary => Dictionary updateDataTypeWithEnumForMap(property); + // set allowable values to enum values (including array/map of enum) + property.allowableValues = getInnerEnumAllowableValues(property); } } @@ -1657,6 +1664,17 @@ public class DefaultCodegen { return currentProperty.isEnum; } + protected Map getInnerEnumAllowableValues(CodegenProperty property) { + CodegenProperty currentProperty = property; + while (currentProperty != null && (Boolean.TRUE.equals(currentProperty.isMapContainer) + || Boolean.TRUE.equals(currentProperty.isListContainer))) { + currentProperty = currentProperty.items; + } + + return currentProperty.allowableValues; + } + + /** * Update datatypeWithEnum for array container * @param property Codegen property diff --git a/modules/swagger-codegen/src/main/resources/php/model_generic.mustache b/modules/swagger-codegen/src/main/resources/php/model_generic.mustache index 0a2cf4cbe27..ea32a9a95ab 100644 --- a/modules/swagger-codegen/src/main/resources/php/model_generic.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model_generic.mustache @@ -121,36 +121,44 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple } {{/required}} {{#isEnum}} + {{^isContainer}} $allowed_values = array({{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}); if (!in_array($this->container['{{name}}'], $allowed_values)) { $invalid_properties[] = "invalid value for '{{name}}', must be one of #{allowed_values}."; } + + {{/isContainer}} {{/isEnum}} {{#hasValidation}} {{#maxLength}} if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(strlen($this->container['{{name}}']) > {{maxLength}})) { $invalid_properties[] = "invalid value for '{{name}}', the character length must be smaller than or equal to {{{maxLength}}}."; } + {{/maxLength}} {{#minLength}} if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(strlen($this->container['{{name}}']) < {{minLength}})) { $invalid_properties[] = "invalid value for '{{name}}', the character length must be bigger than or equal to {{{minLength}}}."; } + {{/minLength}} {{#maximum}} if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}($this->container['{{name}}'] > {{maximum}})) { $invalid_properties[] = "invalid value for '{{name}}', must be smaller than or equal to {{maximum}}."; } + {{/maximum}} {{#minimum}} if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}($this->container['{{name}}'] < {{minimum}})) { $invalid_properties[] = "invalid value for '{{name}}', must be bigger than or equal to {{minimum}}."; } + {{/minimum}} {{#pattern}} if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}!preg_match("{{pattern}}", $this->container['{{name}}'])) { $invalid_properties[] = "invalid value for '{{name}}', must be conform to the pattern {{pattern}}."; } + {{/pattern}} {{/hasValidation}} {{/vars}} @@ -172,10 +180,12 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple } {{/required}} {{#isEnum}} + {{^isContainer}} $allowed_values = array({{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}); if (!in_array($this->container['{{name}}'], $allowed_values)) { return false; } + {{/isContainer}} {{/isEnum}} {{#hasValidation}} {{#maxLength}} diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml index 0bd6faecdb2..7ce7c2a1139 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml @@ -1143,6 +1143,25 @@ definitions: type: array items: type: number + EnumArrays: + type: object + properties: + array_enum: + type: array + items: + type: string + enum: + - fish + - crab + array_array_enum: + type: array + items: + type: array + items: + type: string + enum: + - Cat + - Dog externalDocs: description: Find out more about Swagger url: 'http://swagger.io' diff --git a/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md b/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md new file mode 100644 index 00000000000..20a130d1101 --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md @@ -0,0 +1,11 @@ +# EnumArrays + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**array_enum** | **string[]** | | [optional] +**array_array_enum** | [**string[][]**](array.md) | | [optional] + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php new file mode 100644 index 00000000000..aba320bf6ec --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php @@ -0,0 +1,299 @@ + 'string[]', + 'array_array_enum' => 'string[][]' + ); + + public static function swaggerTypes() + { + return self::$swaggerTypes; + } + + /** + * Array of attributes where the key is the local name, and the value is the original name + * @var string[] + */ + protected static $attributeMap = array( + 'array_enum' => 'array_enum', + 'array_array_enum' => 'array_array_enum' + ); + + public static function attributeMap() + { + return self::$attributeMap; + } + + /** + * Array of attributes to setter functions (for deserialization of responses) + * @var string[] + */ + protected static $setters = array( + 'array_enum' => 'setArrayEnum', + 'array_array_enum' => 'setArrayArrayEnum' + ); + + public static function setters() + { + return self::$setters; + } + + /** + * Array of attributes to getter functions (for serialization of requests) + * @var string[] + */ + protected static $getters = array( + 'array_enum' => 'getArrayEnum', + 'array_array_enum' => 'getArrayArrayEnum' + ); + + public static function getters() + { + return self::$getters; + } + + const ARRAY_ENUM[]_FISH = 'fish'; + const ARRAY_ENUM[]_CRAB = 'crab'; + const ARRAY_ARRAY_ENUM[][]_CAT = 'Cat'; + const ARRAY_ARRAY_ENUM[][]_DOG = 'Dog'; + + + + /** + * Gets allowable values of the enum + * @return string[] + */ + public function getArrayEnumAllowableValues() + { + return [ + self::ARRAY_ENUM[]_FISH, + self::ARRAY_ENUM[]_CRAB, + ]; + } + + /** + * Gets allowable values of the enum + * @return string[] + */ + public function getArrayArrayEnumAllowableValues() + { + return [ + self::ARRAY_ARRAY_ENUM[][]_CAT, + self::ARRAY_ARRAY_ENUM[][]_DOG, + ]; + } + + + /** + * Associative array for storing property values + * @var mixed[] + */ + protected $container = array(); + + /** + * Constructor + * @param mixed[] $data Associated array of property value initalizing the model + */ + public function __construct(array $data = null) + { + $this->container['array_enum'] = isset($data['array_enum']) ? $data['array_enum'] : null; + $this->container['array_array_enum'] = isset($data['array_array_enum']) ? $data['array_array_enum'] : null; + } + + /** + * show all the invalid properties with reasons. + * + * @return array invalid properties with reasons + */ + public function listInvalidProperties() + { + $invalid_properties = array(); + return $invalid_properties; + } + + /** + * validate all the properties in the model + * return true if all passed + * + * @return bool True if all properteis are valid + */ + public function valid() + { + return true; + } + + + /** + * Gets array_enum + * @return string[] + */ + public function getArrayEnum() + { + return $this->container['array_enum']; + } + + /** + * Sets array_enum + * @param string[] $array_enum + * @return $this + */ + public function setArrayEnum($array_enum) + { + $allowed_values = array('fish', 'crab'); + if (!in_array($array_enum, $allowed_values)) { + throw new \InvalidArgumentException("Invalid value for 'array_enum', must be one of 'fish', 'crab'"); + } + $this->container['array_enum'] = $array_enum; + + return $this; + } + + /** + * Gets array_array_enum + * @return string[][] + */ + public function getArrayArrayEnum() + { + return $this->container['array_array_enum']; + } + + /** + * Sets array_array_enum + * @param string[][] $array_array_enum + * @return $this + */ + public function setArrayArrayEnum($array_array_enum) + { + $allowed_values = array('Cat', 'Dog'); + if (!in_array($array_array_enum, $allowed_values)) { + throw new \InvalidArgumentException("Invalid value for 'array_array_enum', must be one of 'Cat', 'Dog'"); + } + $this->container['array_array_enum'] = $array_array_enum; + + return $this; + } + /** + * Returns true if offset exists. False otherwise. + * @param integer $offset Offset + * @return boolean + */ + public function offsetExists($offset) + { + return isset($this->container[$offset]); + } + + /** + * Gets offset. + * @param integer $offset Offset + * @return mixed + */ + public function offsetGet($offset) + { + return isset($this->container[$offset]) ? $this->container[$offset] : null; + } + + /** + * Sets value based on offset. + * @param integer $offset Offset + * @param mixed $value Value to be set + * @return void + */ + public function offsetSet($offset, $value) + { + if (is_null($offset)) { + $this->container[] = $value; + } else { + $this->container[$offset] = $value; + } + } + + /** + * Unsets offset. + * @param integer $offset Offset + * @return void + */ + public function offsetUnset($offset) + { + unset($this->container[$offset]); + } + + /** + * Gets the string presentation of the object + * @return string + */ + public function __toString() + { + if (defined('JSON_PRETTY_PRINT')) { // use JSON pretty print + return json_encode(\Swagger\Client\ObjectSerializer::sanitizeForSerialization($this), JSON_PRETTY_PRINT); + } + + return json_encode(\Swagger\Client\ObjectSerializer::sanitizeForSerialization($this)); + } +} + + diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumTest.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumTest.php index 49fc40c8574..656da77ac0a 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumTest.php @@ -196,14 +196,17 @@ class EnumTest implements ArrayAccess if (!in_array($this->container['enum_string'], $allowed_values)) { $invalid_properties[] = "invalid value for 'enum_string', must be one of #{allowed_values}."; } + $allowed_values = array("1", "-1"); if (!in_array($this->container['enum_integer'], $allowed_values)) { $invalid_properties[] = "invalid value for 'enum_integer', must be one of #{allowed_values}."; } + $allowed_values = array("1.1", "-1.2"); if (!in_array($this->container['enum_number'], $allowed_values)) { $invalid_properties[] = "invalid value for 'enum_number', must be one of #{allowed_values}."; } + return $invalid_properties; } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php index dff8b6cbb62..14c23883d66 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php @@ -117,6 +117,8 @@ class MapTest implements ArrayAccess return self::$getters; } + const map[string,string]_UPPER = 'UPPER'; + const map[string,string]_LOWER = 'lower'; @@ -127,7 +129,8 @@ class MapTest implements ArrayAccess public function getMapOfEnumStringAllowableValues() { return [ - + self::map[string,string]_UPPER, + self::map[string,string]_LOWER, ]; } @@ -156,10 +159,6 @@ class MapTest implements ArrayAccess public function listInvalidProperties() { $invalid_properties = array(); - $allowed_values = array(); - if (!in_array($this->container['map_of_enum_string'], $allowed_values)) { - $invalid_properties[] = "invalid value for 'map_of_enum_string', must be one of #{allowed_values}."; - } return $invalid_properties; } @@ -171,10 +170,6 @@ class MapTest implements ArrayAccess */ public function valid() { - $allowed_values = array(); - if (!in_array($this->container['map_of_enum_string'], $allowed_values)) { - return false; - } return true; } @@ -216,9 +211,9 @@ class MapTest implements ArrayAccess */ public function setMapOfEnumString($map_of_enum_string) { - $allowed_values = array(); + $allowed_values = array('UPPER', 'lower'); if (!in_array($map_of_enum_string, $allowed_values)) { - throw new \InvalidArgumentException("Invalid value for 'map_of_enum_string', must be one of "); + throw new \InvalidArgumentException("Invalid value for 'map_of_enum_string', must be one of 'UPPER', 'lower'"); } $this->container['map_of_enum_string'] = $map_of_enum_string; diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php index 47c29ec67b8..9b30e8de267 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Order.php @@ -185,6 +185,7 @@ class Order implements ArrayAccess if (!in_array($this->container['status'], $allowed_values)) { $invalid_properties[] = "invalid value for 'status', must be one of #{allowed_values}."; } + return $invalid_properties; } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php index 3e76cebbae0..d352976547d 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/Pet.php @@ -191,6 +191,7 @@ class Pet implements ArrayAccess if (!in_array($this->container['status'], $allowed_values)) { $invalid_properties[] = "invalid value for 'status', must be one of #{allowed_values}."; } + return $invalid_properties; } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php index ce77aa6c3b3..fe68a3877d6 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php @@ -264,7 +264,7 @@ class ObjectSerializer } else { return null; } - } elseif (in_array($class, array('void', 'bool', 'string', 'double', 'byte', 'mixed', 'integer', 'float', 'int', 'DateTime', 'number', 'boolean', 'object'))) { + } elseif (in_array($class, array('integer', 'int', 'void', 'number', 'object', 'double', 'float', 'byte', 'DateTime', 'string', 'mixed', 'boolean', 'bool'))) { settype($data, $class); return $data; } elseif ($class === '\SplFileObject') { diff --git a/samples/client/petstore/php/SwaggerClient-php/test/Model/EnumArraysTest.php b/samples/client/petstore/php/SwaggerClient-php/test/Model/EnumArraysTest.php new file mode 100644 index 00000000000..df5d48f2cbb --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient-php/test/Model/EnumArraysTest.php @@ -0,0 +1,114 @@ +