diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java index 7462094acc2..b6a4b9d02da 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenProperty.java @@ -46,6 +46,8 @@ public class CodegenProperty implements Cloneable { public Boolean hasValidation; // true if pattern, maximum, etc are set (only used in the mustache template) public Boolean isInherited; public String nameInCamelCase; // property name in camel case + // enum name based on the property name, usually use as a prefix (e.g. VAR_NAME) for enum name (e.g. VAR_NAME_VALUE1) + public String enumName; @Override public String toString() { @@ -111,6 +113,7 @@ public class CodegenProperty implements Cloneable { result = prime * result + ((isListContainer == null) ? 0 : isListContainer.hashCode()); result = prime * result + Objects.hashCode(isInherited); result = prime * result + Objects.hashCode(nameInCamelCase); + result = prime * result + Objects.hashCode(enumName); return result; } @@ -271,6 +274,9 @@ public class CodegenProperty implements Cloneable { if (!Objects.equals(this.nameInCamelCase, other.nameInCamelCase)) { return false; } + if (!Objects.equals(this.enumName, other.enumName)) { + return false; + } return true; } 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 3e9be96f76f..fbf2a0f009f 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 @@ -1559,6 +1559,7 @@ public class DefaultCodegen { // this can cause issues for clients which don't support enums if (property.isEnum) { property.datatypeWithEnum = toEnumName(property); + property.enumName = toEnumName(property); } else { property.datatypeWithEnum = property.datatype; } @@ -1688,6 +1689,10 @@ public class DefaultCodegen { // set both datatype and datetypeWithEnum as only the inner type is enum property.datatypeWithEnum = property.datatypeWithEnum.replace(baseItem.baseType, toEnumName(baseItem)); + // naming the enum with respect to the language enum naming convention + // e.g. remove [], {} from array/map of enum + property.enumName = toEnumName(property); + // set default value for variable with inner enum if (property.defaultValue != null) { property.defaultValue = property.defaultValue.replace(property.items.baseType, toEnumName(property.items)); @@ -1707,6 +1712,10 @@ public class DefaultCodegen { // set both datatype and datetypeWithEnum as only the inner type is enum property.datatypeWithEnum = property.datatypeWithEnum.replace(", " + baseItem.baseType, ", " + toEnumName(baseItem)); + // naming the enum with respect to the language enum naming convention + // e.g. remove [], {} from array/map of enum + property.enumName = toEnumName(property); + // set default value for variable with inner enum if (property.defaultValue != null) { property.defaultValue = property.defaultValue.replace(", " + property.items.baseType, ", " + toEnumName(property.items)); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java index c907fec94f1..783af9c753a 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java @@ -645,6 +645,9 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { public String toEnumName(CodegenProperty property) { String enumName = underscore(toModelName(property.name)).toUpperCase(); + // remove [] for array or map of enum + enumName = enumName.replace("[]", ""); + if (enumName.matches("\\d.*")) { // starts with number return "_" + enumName; } else { 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 ea32a9a95ab..27f66e8683b 100644 --- a/modules/swagger-codegen/src/main/resources/php/model_generic.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model_generic.mustache @@ -62,7 +62,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple return {{#parentSchema}}parent::getters() + {{/parentSchema}}self::$getters; } - {{#vars}}{{#isEnum}}{{#allowableValues}}{{#enumVars}}const {{datatypeWithEnum}}_{{{name}}} = {{{value}}}; + {{#vars}}{{#isEnum}}{{#allowableValues}}{{#enumVars}}const {{enumName}}_{{{name}}} = {{{value}}}; {{/enumVars}}{{/allowableValues}}{{/isEnum}}{{/vars}} {{#vars}}{{#isEnum}} @@ -73,7 +73,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple public function {{getter}}AllowableValues() { return [ - {{#allowableValues}}{{#enumVars}}self::{{datatypeWithEnum}}_{{{name}}},{{^-last}} + {{#allowableValues}}{{#enumVars}}self::{{enumName}}_{{{name}}},{{^-last}} {{/-last}}{{/enumVars}}{{/allowableValues}} ]; } diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java index bd0fcc168cd..9842258fb92 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java @@ -111,9 +111,8 @@ public class CodegenTest { final CodegenParameter statusParam = op.queryParams.get(0); Assert.assertEquals(statusParam.datatypeWithEnum, "List"); Assert.assertEquals(statusParam.baseType, "String"); - // currently there's no way to tell if the inner type of a list is a enum - //Assert.assertTrue(statusParam.isEnum); - //Assert.assertEquals(statusParam._enum.size(), 3); + Assert.assertTrue(statusParam.isEnum); + Assert.assertEquals(((List)statusParam.allowableValues.get("values")).size(), 3); } 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 7ce7c2a1139..f951a745dcb 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 @@ -1146,6 +1146,11 @@ definitions: EnumArrays: type: object properties: + just_enum: + type: string + enum: + - bird + - eagle array_enum: type: array items: diff --git a/samples/client/petstore/java/okhttp-gson/docs/MapTest.md b/samples/client/petstore/java/okhttp-gson/docs/MapTest.md index c671e97ffbc..714a97a40d9 100644 --- a/samples/client/petstore/java/okhttp-gson/docs/MapTest.md +++ b/samples/client/petstore/java/okhttp-gson/docs/MapTest.md @@ -12,6 +12,8 @@ Name | Type | Description | Notes ## Enum: Map<String, InnerEnum> Name | Value ---- | ----- +UPPER | "UPPER" +LOWER | "lower" diff --git a/samples/client/petstore/java/okhttp-gson/docs/PetApi.md b/samples/client/petstore/java/okhttp-gson/docs/PetApi.md index e0314e20e51..3b5f84043e3 100644 --- a/samples/client/petstore/java/okhttp-gson/docs/PetApi.md +++ b/samples/client/petstore/java/okhttp-gson/docs/PetApi.md @@ -158,7 +158,7 @@ try { Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **status** | [**List<String>**](String.md)| Status values that need to be considered for filter | + **status** | [**List<String>**](String.md)| Status values that need to be considered for filter | [enum: available, pending, sold] ### Return type diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java index c0630ba7ebc..b7b04e3e4c5 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java @@ -173,8 +173,8 @@ public class ApiClient { // Setup authentications (key: authentication name, value: authentication). authentications = new HashMap(); - authentications.put("petstore_auth", new OAuth()); authentications.put("api_key", new ApiKeyAuth("header", "api_key")); + authentications.put("petstore_auth", new OAuth()); // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications); } diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/api/FakeApi.java b/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/api/FakeApi.java index 39e9bf37621..c6e71e5f1b6 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/api/FakeApi.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/api/FakeApi.java @@ -40,8 +40,8 @@ import java.io.IOException; import io.swagger.client.model.Client; import org.joda.time.LocalDate; -import java.math.BigDecimal; import org.joda.time.DateTime; +import java.math.BigDecimal; import java.lang.reflect.Type; import java.util.ArrayList; diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/api/PetApi.java b/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/api/PetApi.java index 890e1a216f2..2039a8842c1 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/api/PetApi.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/api/PetApi.java @@ -39,8 +39,8 @@ import com.google.gson.reflect.TypeToken; import java.io.IOException; import io.swagger.client.model.Pet; -import io.swagger.client.model.ModelApiResponse; import java.io.File; +import io.swagger.client.model.ModelApiResponse; import java.lang.reflect.Type; import java.util.ArrayList; diff --git a/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md b/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md index 20a130d1101..7b7fd741898 100644 --- a/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md +++ b/samples/client/petstore/php/SwaggerClient-php/docs/Model/EnumArrays.md @@ -3,6 +3,7 @@ ## Properties Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- +**just_enum** | **string** | | [optional] **array_enum** | **string[]** | | [optional] **array_array_enum** | [**string[][]**](array.md) | | [optional] diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php index aba320bf6ec..bfbc95128c5 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/EnumArrays.php @@ -66,6 +66,7 @@ class EnumArrays implements ArrayAccess * @var string[] */ protected static $swaggerTypes = array( + 'just_enum' => 'string', 'array_enum' => 'string[]', 'array_array_enum' => 'string[][]' ); @@ -80,6 +81,7 @@ class EnumArrays implements ArrayAccess * @var string[] */ protected static $attributeMap = array( + 'just_enum' => 'just_enum', 'array_enum' => 'array_enum', 'array_array_enum' => 'array_array_enum' ); @@ -94,6 +96,7 @@ class EnumArrays implements ArrayAccess * @var string[] */ protected static $setters = array( + 'just_enum' => 'setJustEnum', 'array_enum' => 'setArrayEnum', 'array_array_enum' => 'setArrayArrayEnum' ); @@ -108,6 +111,7 @@ class EnumArrays implements ArrayAccess * @var string[] */ protected static $getters = array( + 'just_enum' => 'getJustEnum', 'array_enum' => 'getArrayEnum', 'array_array_enum' => 'getArrayArrayEnum' ); @@ -117,13 +121,27 @@ class EnumArrays implements ArrayAccess 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'; + const JUST_ENUM_BIRD = 'bird'; + const JUST_ENUM_EAGLE = 'eagle'; + 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 getJustEnumAllowableValues() + { + return [ + self::JUST_ENUM_BIRD, + self::JUST_ENUM_EAGLE, + ]; + } + /** * Gets allowable values of the enum * @return string[] @@ -131,8 +149,8 @@ class EnumArrays implements ArrayAccess public function getArrayEnumAllowableValues() { return [ - self::ARRAY_ENUM[]_FISH, - self::ARRAY_ENUM[]_CRAB, + self::ARRAY_ENUM_FISH, + self::ARRAY_ENUM_CRAB, ]; } @@ -143,8 +161,8 @@ class EnumArrays implements ArrayAccess public function getArrayArrayEnumAllowableValues() { return [ - self::ARRAY_ARRAY_ENUM[][]_CAT, - self::ARRAY_ARRAY_ENUM[][]_DOG, + self::ARRAY_ARRAY_ENUM_CAT, + self::ARRAY_ARRAY_ENUM_DOG, ]; } @@ -161,6 +179,7 @@ class EnumArrays implements ArrayAccess */ public function __construct(array $data = null) { + $this->container['just_enum'] = isset($data['just_enum']) ? $data['just_enum'] : 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; } @@ -173,6 +192,11 @@ class EnumArrays implements ArrayAccess public function listInvalidProperties() { $invalid_properties = array(); + $allowed_values = array("bird", "eagle"); + if (!in_array($this->container['just_enum'], $allowed_values)) { + $invalid_properties[] = "invalid value for 'just_enum', must be one of #{allowed_values}."; + } + return $invalid_properties; } @@ -184,10 +208,39 @@ class EnumArrays implements ArrayAccess */ public function valid() { + $allowed_values = array("bird", "eagle"); + if (!in_array($this->container['just_enum'], $allowed_values)) { + return false; + } return true; } + /** + * Gets just_enum + * @return string + */ + public function getJustEnum() + { + return $this->container['just_enum']; + } + + /** + * Sets just_enum + * @param string $just_enum + * @return $this + */ + public function setJustEnum($just_enum) + { + $allowed_values = array('bird', 'eagle'); + if (!in_array($just_enum, $allowed_values)) { + throw new \InvalidArgumentException("Invalid value for 'just_enum', must be one of 'bird', 'eagle'"); + } + $this->container['just_enum'] = $just_enum; + + return $this; + } + /** * Gets array_enum * @return string[] 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 14c23883d66..0ff51e87971 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Model/MapTest.php @@ -117,8 +117,8 @@ class MapTest implements ArrayAccess return self::$getters; } - const map[string,string]_UPPER = 'UPPER'; - const map[string,string]_LOWER = 'lower'; + const MAP_OF_ENUM_STRING_UPPER = 'UPPER'; + const MAP_OF_ENUM_STRING_LOWER = 'lower'; @@ -129,8 +129,8 @@ class MapTest implements ArrayAccess public function getMapOfEnumStringAllowableValues() { return [ - self::map[string,string]_UPPER, - self::map[string,string]_LOWER, + self::MAP_OF_ENUM_STRING_UPPER, + self::MAP_OF_ENUM_STRING_LOWER, ]; }