forked from loafle/openapi-generator-original
[python-nextgen] better enum naming (#14869)
* better handling of enum naming in python nextgen * remove unused import * update samples * update samples * map dot
This commit is contained in:
parent
e535066a85
commit
39e27a804d
@ -38,6 +38,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import static org.openapitools.codegen.utils.StringUtils.escape;
|
||||||
import static org.openapitools.codegen.utils.StringUtils.underscore;
|
import static org.openapitools.codegen.utils.StringUtils.underscore;
|
||||||
|
|
||||||
public class PythonNextgenClientCodegen extends AbstractPythonCodegen implements CodegenConfig {
|
public class PythonNextgenClientCodegen extends AbstractPythonCodegen implements CodegenConfig {
|
||||||
@ -208,6 +209,9 @@ public class PythonNextgenClientCodegen extends AbstractPythonCodegen implements
|
|||||||
|
|
||||||
super.processOpts();
|
super.processOpts();
|
||||||
|
|
||||||
|
// map to Dot instead of Period
|
||||||
|
specialCharReplacements.put(".", "Dot");
|
||||||
|
|
||||||
if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) {
|
if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) {
|
||||||
LOGGER.info("Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE=\"/usr/local/bin/yapf -i\"' (Linux/Mac)");
|
LOGGER.info("Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE=\"/usr/local/bin/yapf -i\"' (Linux/Mac)");
|
||||||
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
|
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
|
||||||
@ -1331,38 +1335,42 @@ public class PythonNextgenClientCodegen extends AbstractPythonCodegen implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toEnumVariableName(String name, String datatype) {
|
public String toEnumVariableName(String name, String datatype) {
|
||||||
|
if ("int".equals(datatype)) {
|
||||||
|
return "NUMBER_" + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove quote e.g. 'abc' => abc
|
||||||
|
name = name.substring(1, name.length() - 1);
|
||||||
|
|
||||||
if (name.length() == 0) {
|
if (name.length() == 0) {
|
||||||
return "EMPTY";
|
return "EMPTY";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.trim().length() == 0) {
|
if (" ".equals(name)) {
|
||||||
return "SPACE_" + name.length();
|
return "SPACE";
|
||||||
}
|
}
|
||||||
|
|
||||||
// for symbol, e.g. $, #
|
if ("_".equals(name)) {
|
||||||
if (getSymbolName(name) != null) {
|
return "UNDERSCORE";
|
||||||
return (getSymbolName(name)).toUpperCase(Locale.ROOT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// number
|
if (reservedWords.contains(name)) {
|
||||||
if ("int".equals(datatype) || "float".equals(datatype)) {
|
name = name.toUpperCase(Locale.ROOT);
|
||||||
String varName = name;
|
} else if (((CharSequence) name).chars().anyMatch(character -> specialCharReplacements.keySet().contains(String.valueOf((char) character)))) {
|
||||||
varName = varName.replaceAll("-", "MINUS_");
|
name = underscore(escape(name, specialCharReplacements, Collections.singletonList("_"), "_")).toUpperCase(Locale.ROOT);
|
||||||
varName = varName.replaceAll("\\+", "PLUS_");
|
|
||||||
varName = varName.replaceAll("\\.", "_DOT_");
|
|
||||||
return "NUMBER_" + varName;
|
|
||||||
}
|
|
||||||
|
|
||||||
// string
|
|
||||||
String enumName = sanitizeName(underscore(name).toUpperCase(Locale.ROOT));
|
|
||||||
enumName = enumName.replaceFirst("^_", "");
|
|
||||||
enumName = enumName.replaceFirst("_$", "");
|
|
||||||
|
|
||||||
if (isReservedWord(enumName) || enumName.matches("\\d.*")) { // reserved word or starts with number
|
|
||||||
return escapeReservedWord(enumName);
|
|
||||||
} else {
|
} else {
|
||||||
return enumName;
|
name = name.toUpperCase(Locale.ROOT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name = name.replace(" ", "_");
|
||||||
|
name = name.replaceFirst("^_", "");
|
||||||
|
name = name.replaceFirst("_$", "");
|
||||||
|
|
||||||
|
if (name.matches("\\d.*")) {
|
||||||
|
name = "ENUM_" + name.toUpperCase(Locale.ROOT);
|
||||||
|
}
|
||||||
|
|
||||||
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1836,6 +1836,19 @@ components:
|
|||||||
- 1
|
- 1
|
||||||
- 2
|
- 2
|
||||||
default: 0
|
default: 0
|
||||||
|
SpecialCharacterEnum:
|
||||||
|
type: string
|
||||||
|
enum:
|
||||||
|
- "456"
|
||||||
|
- "123abc"
|
||||||
|
- "_"
|
||||||
|
- " "
|
||||||
|
- "&"
|
||||||
|
- "$"
|
||||||
|
- ">="
|
||||||
|
- "this_is_!"
|
||||||
|
- "import" # reserved word
|
||||||
|
- " hello world "
|
||||||
OuterComposite:
|
OuterComposite:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
|
@ -61,6 +61,7 @@ docs/Pig.md
|
|||||||
docs/ReadOnlyFirst.md
|
docs/ReadOnlyFirst.md
|
||||||
docs/SelfReferenceModel.md
|
docs/SelfReferenceModel.md
|
||||||
docs/SingleRefType.md
|
docs/SingleRefType.md
|
||||||
|
docs/SpecialCharacterEnum.md
|
||||||
docs/SpecialModelName.md
|
docs/SpecialModelName.md
|
||||||
docs/SpecialName.md
|
docs/SpecialName.md
|
||||||
docs/StoreApi.md
|
docs/StoreApi.md
|
||||||
@ -135,6 +136,7 @@ petstore_api/models/pig.py
|
|||||||
petstore_api/models/read_only_first.py
|
petstore_api/models/read_only_first.py
|
||||||
petstore_api/models/self_reference_model.py
|
petstore_api/models/self_reference_model.py
|
||||||
petstore_api/models/single_ref_type.py
|
petstore_api/models/single_ref_type.py
|
||||||
|
petstore_api/models/special_character_enum.py
|
||||||
petstore_api/models/special_model_name.py
|
petstore_api/models/special_model_name.py
|
||||||
petstore_api/models/special_name.py
|
petstore_api/models/special_name.py
|
||||||
petstore_api/models/tag.py
|
petstore_api/models/tag.py
|
||||||
|
@ -180,6 +180,7 @@ Class | Method | HTTP request | Description
|
|||||||
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
|
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
|
||||||
- [SelfReferenceModel](docs/SelfReferenceModel.md)
|
- [SelfReferenceModel](docs/SelfReferenceModel.md)
|
||||||
- [SingleRefType](docs/SingleRefType.md)
|
- [SingleRefType](docs/SingleRefType.md)
|
||||||
|
- [SpecialCharacterEnum](docs/SpecialCharacterEnum.md)
|
||||||
- [SpecialModelName](docs/SpecialModelName.md)
|
- [SpecialModelName](docs/SpecialModelName.md)
|
||||||
- [SpecialName](docs/SpecialName.md)
|
- [SpecialName](docs/SpecialName.md)
|
||||||
- [Tag](docs/Tag.md)
|
- [Tag](docs/Tag.md)
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
# SpecialCharacterEnum
|
||||||
|
|
||||||
|
|
||||||
|
## Properties
|
||||||
|
Name | Type | Description | Notes
|
||||||
|
------------ | ------------- | ------------- | -------------
|
||||||
|
|
||||||
|
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
|
||||||
|
|
||||||
|
|
@ -88,6 +88,7 @@ from petstore_api.models.pig import Pig
|
|||||||
from petstore_api.models.read_only_first import ReadOnlyFirst
|
from petstore_api.models.read_only_first import ReadOnlyFirst
|
||||||
from petstore_api.models.self_reference_model import SelfReferenceModel
|
from petstore_api.models.self_reference_model import SelfReferenceModel
|
||||||
from petstore_api.models.single_ref_type import SingleRefType
|
from petstore_api.models.single_ref_type import SingleRefType
|
||||||
|
from petstore_api.models.special_character_enum import SpecialCharacterEnum
|
||||||
from petstore_api.models.special_model_name import SpecialModelName
|
from petstore_api.models.special_model_name import SpecialModelName
|
||||||
from petstore_api.models.special_name import SpecialName
|
from petstore_api.models.special_name import SpecialName
|
||||||
from petstore_api.models.tag import Tag
|
from petstore_api.models.tag import Tag
|
||||||
|
@ -67,6 +67,7 @@ from petstore_api.models.pig import Pig
|
|||||||
from petstore_api.models.read_only_first import ReadOnlyFirst
|
from petstore_api.models.read_only_first import ReadOnlyFirst
|
||||||
from petstore_api.models.self_reference_model import SelfReferenceModel
|
from petstore_api.models.self_reference_model import SelfReferenceModel
|
||||||
from petstore_api.models.single_ref_type import SingleRefType
|
from petstore_api.models.single_ref_type import SingleRefType
|
||||||
|
from petstore_api.models.special_character_enum import SpecialCharacterEnum
|
||||||
from petstore_api.models.special_model_name import SpecialModelName
|
from petstore_api.models.special_model_name import SpecialModelName
|
||||||
from petstore_api.models.special_name import SpecialName
|
from petstore_api.models.special_name import SpecialName
|
||||||
from petstore_api.models.tag import Tag
|
from petstore_api.models.tag import Tag
|
||||||
|
@ -31,6 +31,6 @@ class EnumClass(str, Enum):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
ABC = '_abc'
|
ABC = '_abc'
|
||||||
EFG = '-efg'
|
MINUS_EFG = '-efg'
|
||||||
XYZ = '(xyz)'
|
LEFT_PARENTHESIS_XYZ_RIGHT_PARENTHESIS = '(xyz)'
|
||||||
|
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
"""
|
||||||
|
OpenAPI Petstore
|
||||||
|
|
||||||
|
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501
|
||||||
|
|
||||||
|
The version of the OpenAPI document: 1.0.0
|
||||||
|
Generated by: https://openapi-generator.tech
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
from inspect import getfullargspec
|
||||||
|
import pprint
|
||||||
|
import re # noqa: F401
|
||||||
|
from aenum import Enum, no_arg
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class SpecialCharacterEnum(str, Enum):
|
||||||
|
"""NOTE: This class is auto generated by OpenAPI Generator.
|
||||||
|
Ref: https://openapi-generator.tech
|
||||||
|
|
||||||
|
Do not edit the class manually.
|
||||||
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
allowed enum values
|
||||||
|
"""
|
||||||
|
|
||||||
|
ENUM_456 = '456'
|
||||||
|
ENUM_123ABC = '123abc'
|
||||||
|
UNDERSCORE = '_'
|
||||||
|
SPACE = ' '
|
||||||
|
AMPERSAND = '&'
|
||||||
|
DOLLAR = '$'
|
||||||
|
GREATER_THAN_EQUAL = '>='
|
||||||
|
THIS_IS_EXCLAMATION = 'this_is_!'
|
||||||
|
IMPORT = 'import'
|
||||||
|
HELLO_WORLD = ' hello world '
|
||||||
|
|
@ -0,0 +1,36 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
"""
|
||||||
|
OpenAPI Petstore
|
||||||
|
|
||||||
|
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501
|
||||||
|
|
||||||
|
The version of the OpenAPI document: 1.0.0
|
||||||
|
Generated by: https://openapi-generator.tech
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
import petstore_api
|
||||||
|
from petstore_api.models.special_character_enum import SpecialCharacterEnum # noqa: E501
|
||||||
|
from petstore_api.rest import ApiException
|
||||||
|
|
||||||
|
class TestSpecialCharacterEnum(unittest.TestCase):
|
||||||
|
"""SpecialCharacterEnum unit test stubs"""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def testSpecialCharacterEnum(self):
|
||||||
|
"""Test SpecialCharacterEnum"""
|
||||||
|
# inst = SpecialCharacterEnum()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
@ -61,6 +61,7 @@ docs/Pig.md
|
|||||||
docs/ReadOnlyFirst.md
|
docs/ReadOnlyFirst.md
|
||||||
docs/SelfReferenceModel.md
|
docs/SelfReferenceModel.md
|
||||||
docs/SingleRefType.md
|
docs/SingleRefType.md
|
||||||
|
docs/SpecialCharacterEnum.md
|
||||||
docs/SpecialModelName.md
|
docs/SpecialModelName.md
|
||||||
docs/SpecialName.md
|
docs/SpecialName.md
|
||||||
docs/StoreApi.md
|
docs/StoreApi.md
|
||||||
@ -135,6 +136,7 @@ petstore_api/models/pig.py
|
|||||||
petstore_api/models/read_only_first.py
|
petstore_api/models/read_only_first.py
|
||||||
petstore_api/models/self_reference_model.py
|
petstore_api/models/self_reference_model.py
|
||||||
petstore_api/models/single_ref_type.py
|
petstore_api/models/single_ref_type.py
|
||||||
|
petstore_api/models/special_character_enum.py
|
||||||
petstore_api/models/special_model_name.py
|
petstore_api/models/special_model_name.py
|
||||||
petstore_api/models/special_name.py
|
petstore_api/models/special_name.py
|
||||||
petstore_api/models/tag.py
|
petstore_api/models/tag.py
|
||||||
|
@ -180,6 +180,7 @@ Class | Method | HTTP request | Description
|
|||||||
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
|
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
|
||||||
- [SelfReferenceModel](docs/SelfReferenceModel.md)
|
- [SelfReferenceModel](docs/SelfReferenceModel.md)
|
||||||
- [SingleRefType](docs/SingleRefType.md)
|
- [SingleRefType](docs/SingleRefType.md)
|
||||||
|
- [SpecialCharacterEnum](docs/SpecialCharacterEnum.md)
|
||||||
- [SpecialModelName](docs/SpecialModelName.md)
|
- [SpecialModelName](docs/SpecialModelName.md)
|
||||||
- [SpecialName](docs/SpecialName.md)
|
- [SpecialName](docs/SpecialName.md)
|
||||||
- [Tag](docs/Tag.md)
|
- [Tag](docs/Tag.md)
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
# SpecialCharacterEnum
|
||||||
|
|
||||||
|
|
||||||
|
## Properties
|
||||||
|
Name | Type | Description | Notes
|
||||||
|
------------ | ------------- | ------------- | -------------
|
||||||
|
|
||||||
|
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
|
||||||
|
|
||||||
|
|
@ -88,6 +88,7 @@ from petstore_api.models.pig import Pig
|
|||||||
from petstore_api.models.read_only_first import ReadOnlyFirst
|
from petstore_api.models.read_only_first import ReadOnlyFirst
|
||||||
from petstore_api.models.self_reference_model import SelfReferenceModel
|
from petstore_api.models.self_reference_model import SelfReferenceModel
|
||||||
from petstore_api.models.single_ref_type import SingleRefType
|
from petstore_api.models.single_ref_type import SingleRefType
|
||||||
|
from petstore_api.models.special_character_enum import SpecialCharacterEnum
|
||||||
from petstore_api.models.special_model_name import SpecialModelName
|
from petstore_api.models.special_model_name import SpecialModelName
|
||||||
from petstore_api.models.special_name import SpecialName
|
from petstore_api.models.special_name import SpecialName
|
||||||
from petstore_api.models.tag import Tag
|
from petstore_api.models.tag import Tag
|
||||||
|
@ -67,6 +67,7 @@ from petstore_api.models.pig import Pig
|
|||||||
from petstore_api.models.read_only_first import ReadOnlyFirst
|
from petstore_api.models.read_only_first import ReadOnlyFirst
|
||||||
from petstore_api.models.self_reference_model import SelfReferenceModel
|
from petstore_api.models.self_reference_model import SelfReferenceModel
|
||||||
from petstore_api.models.single_ref_type import SingleRefType
|
from petstore_api.models.single_ref_type import SingleRefType
|
||||||
|
from petstore_api.models.special_character_enum import SpecialCharacterEnum
|
||||||
from petstore_api.models.special_model_name import SpecialModelName
|
from petstore_api.models.special_model_name import SpecialModelName
|
||||||
from petstore_api.models.special_name import SpecialName
|
from petstore_api.models.special_name import SpecialName
|
||||||
from petstore_api.models.tag import Tag
|
from petstore_api.models.tag import Tag
|
||||||
|
@ -31,6 +31,6 @@ class EnumClass(str, Enum):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
ABC = '_abc'
|
ABC = '_abc'
|
||||||
EFG = '-efg'
|
MINUS_EFG = '-efg'
|
||||||
XYZ = '(xyz)'
|
LEFT_PARENTHESIS_XYZ_RIGHT_PARENTHESIS = '(xyz)'
|
||||||
|
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
"""
|
||||||
|
OpenAPI Petstore
|
||||||
|
|
||||||
|
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501
|
||||||
|
|
||||||
|
The version of the OpenAPI document: 1.0.0
|
||||||
|
Generated by: https://openapi-generator.tech
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
from inspect import getfullargspec
|
||||||
|
import pprint
|
||||||
|
import re # noqa: F401
|
||||||
|
from aenum import Enum, no_arg
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class SpecialCharacterEnum(str, Enum):
|
||||||
|
"""NOTE: This class is auto generated by OpenAPI Generator.
|
||||||
|
Ref: https://openapi-generator.tech
|
||||||
|
|
||||||
|
Do not edit the class manually.
|
||||||
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
allowed enum values
|
||||||
|
"""
|
||||||
|
|
||||||
|
ENUM_456 = '456'
|
||||||
|
ENUM_123ABC = '123abc'
|
||||||
|
UNDERSCORE = '_'
|
||||||
|
SPACE = ' '
|
||||||
|
AMPERSAND = '&'
|
||||||
|
DOLLAR = '$'
|
||||||
|
GREATER_THAN_EQUAL = '>='
|
||||||
|
THIS_IS_EXCLAMATION = 'this_is_!'
|
||||||
|
IMPORT = 'import'
|
||||||
|
HELLO_WORLD = ' hello world '
|
||||||
|
|
@ -0,0 +1,36 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
"""
|
||||||
|
OpenAPI Petstore
|
||||||
|
|
||||||
|
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501
|
||||||
|
|
||||||
|
The version of the OpenAPI document: 1.0.0
|
||||||
|
Generated by: https://openapi-generator.tech
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
import petstore_api
|
||||||
|
from petstore_api.models.special_character_enum import SpecialCharacterEnum # noqa: E501
|
||||||
|
from petstore_api.rest import ApiException
|
||||||
|
|
||||||
|
class TestSpecialCharacterEnum(unittest.TestCase):
|
||||||
|
"""SpecialCharacterEnum unit test stubs"""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def testSpecialCharacterEnum(self):
|
||||||
|
"""Test SpecialCharacterEnum"""
|
||||||
|
# inst = SpecialCharacterEnum()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user