add enumName to store the enum prefix

This commit is contained in:
wing328 2016-08-02 23:25:46 +08:00
parent 0b8acb5b0c
commit 25fa3e86f9
14 changed files with 99 additions and 21 deletions

View File

@ -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;
}

View File

@ -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));

View File

@ -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 {

View File

@ -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}}
];
}

View File

@ -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);
}

View File

@ -1146,6 +1146,11 @@ definitions:
EnumArrays:
type: object
properties:
just_enum:
type: string
enum:
- bird
- eagle
array_enum:
type: array
items:

View File

@ -12,6 +12,8 @@ Name | Type | Description | Notes
## Enum: Map<String, InnerEnum>
Name | Value
---- | -----
UPPER | "UPPER"
LOWER | "lower"

View File

@ -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

View File

@ -173,8 +173,8 @@ public class ApiClient {
// Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();
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);
}

View File

@ -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;

View File

@ -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;

View File

@ -3,6 +3,7 @@
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**just_enum** | **string** | | [optional]
**array_enum** | **string[]** | | [optional]
**array_array_enum** | [**string[][]**](array.md) | | [optional]

View File

@ -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[]

View File

@ -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,
];
}