From fff759b79c2f6b8ee5b768dbb8c278d003e3c7e5 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 19 Jan 2020 15:25:20 +0800 Subject: [PATCH] [Python] fix numeric enum in python flask, aiohttp (#5019) * minor code format fix * fix numeric enum in python flask and aiohttp * add python-server-all.sh to ensure-up-to-date --- bin/utils/ensure-up-to-date | 1 + .../languages/PythonClientCodegen.java | 9 +-- .../resources/python-aiohttp/model.mustache | 3 +- .../resources/python-flask/model.mustache | 9 ++- .../openapi_server/models/order.py | 2 +- .../openapi_server/models/pet.py | 2 +- .../openapi_server/openapi/openapi.yaml | 62 +++++++++---------- .../openapi_server/openapi/openapi.yaml | 62 +++++++++---------- .../openapi_server/openapi/openapi.yaml | 62 +++++++++---------- 9 files changed, 109 insertions(+), 103 deletions(-) diff --git a/bin/utils/ensure-up-to-date b/bin/utils/ensure-up-to-date index 023ef246ad4..397b6975a1a 100755 --- a/bin/utils/ensure-up-to-date +++ b/bin/utils/ensure-up-to-date @@ -39,6 +39,7 @@ declare -a samples=( "${root}/bin/mysql-schema-petstore.sh" "${root}/bin/nim-client-petstore.sh" "${root}/bin/python-petstore-all.sh" +"${root}/bin/python-server-all.sh" "${root}/bin/openapi3/python-petstore.sh" "${root}/bin/php-petstore.sh" "${root}/bin/php-silex-petstore-server.sh" diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java index 52a29fe3dc0..fd90aabdd03 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java @@ -276,7 +276,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig // If the package name consists of dots(openapi.client), then we need to create the directory structure like openapi/client with __init__ files. String[] packageNameSplits = packageName.split("\\."); String currentPackagePath = ""; - for (int i = 0; i < packageNameSplits.length-1; i++) { + for (int i = 0; i < packageNameSplits.length - 1; i++) { if (i > 0) { currentPackagePath = currentPackagePath + File.separatorChar; } @@ -566,7 +566,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig name = name.replaceAll("-", "_"); // e.g. PhoneNumberApi.py => phone_number_api.py - return underscore(name+ "_" + apiNameSuffix); + return underscore(name + "_" + apiNameSuffix); } @Override @@ -584,7 +584,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig if (name.length() == 0) { return "default_api"; } - return underscore(name+ "_" + apiNameSuffix); + return underscore(name + "_" + apiNameSuffix); } @Override @@ -649,6 +649,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig /** * Return the default value of the property + * * @param p OpenAPI property object * @return string presentation of the default value of the property */ @@ -678,7 +679,7 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find()) return "'''" + p.getDefault() + "'''"; else - return "'" + ((String) p.getDefault()).replaceAll("'","\'") + "'"; + return "'" + ((String) p.getDefault()).replaceAll("'", "\'") + "'"; } } else if (ModelUtils.isArraySchema(p)) { if (p.getDefault() != null) { diff --git a/modules/openapi-generator/src/main/resources/python-aiohttp/model.mustache b/modules/openapi-generator/src/main/resources/python-aiohttp/model.mustache index 9ab013ea9ef..63b9b7361c1 100644 --- a/modules/openapi-generator/src/main/resources/python-aiohttp/model.mustache +++ b/modules/openapi-generator/src/main/resources/python-aiohttp/model.mustache @@ -89,8 +89,8 @@ class {{classname}}(Model): :type {{name}}: {{dataType}} """ {{#isEnum}} - allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] {{#isContainer}} + allowed_values = [{{#isNullable}}None,{{/isNullable}}{{#allowableValues}}{{#values}}{{#items.isString}}"{{/items.isString}}{{{this}}}{{#items.isString}}"{{/items.isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501 {{#isListContainer}} if not set({{{name}}}).issubset(set(allowed_values)): raise ValueError( @@ -109,6 +109,7 @@ class {{classname}}(Model): {{/isMapContainer}} {{/isContainer}} {{^isContainer}} + allowed_values = [{{#isNullable}}None,{{/isNullable}}{{#allowableValues}}{{#values}}{{#isString}}"{{/isString}}{{{this}}}{{#isString}}"{{/isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501 if {{{name}}} not in allowed_values: raise ValueError( "Invalid value for `{{{name}}}` ({0}), must be one of {1}" diff --git a/modules/openapi-generator/src/main/resources/python-flask/model.mustache b/modules/openapi-generator/src/main/resources/python-flask/model.mustache index 6b6743d5e66..0b2fd37cbc5 100644 --- a/modules/openapi-generator/src/main/resources/python-flask/model.mustache +++ b/modules/openapi-generator/src/main/resources/python-flask/model.mustache @@ -25,7 +25,8 @@ class {{classname}}(Model): """NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). Do not edit the class manually. - """{{#allowableValues}} + """ +{{#allowableValues}} """ allowed enum values @@ -33,7 +34,8 @@ class {{classname}}(Model): {{#enumVars}} {{name}} = {{{value}}}{{^-last}} {{/-last}} -{{/enumVars}}{{/allowableValues}} +{{/enumVars}} +{{/allowableValues}} def __init__(self{{#vars}}, {{name}}={{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{/vars}}): # noqa: E501 """{{classname}} - a model defined in OpenAPI @@ -96,8 +98,8 @@ class {{classname}}(Model): :type {{name}}: {{dataType}} """ {{#isEnum}} - allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501 {{#isContainer}} + allowed_values = [{{#isNullable}}None,{{/isNullable}}{{#allowableValues}}{{#values}}{{#items.isString}}"{{/items.isString}}{{{this}}}{{#items.isString}}"{{/items.isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501 {{#isListContainer}} if not set({{{name}}}).issubset(set(allowed_values)): raise ValueError( @@ -116,6 +118,7 @@ class {{classname}}(Model): {{/isMapContainer}} {{/isContainer}} {{^isContainer}} + allowed_values = [{{#isNullable}}None,{{/isNullable}}{{#allowableValues}}{{#values}}{{#isString}}"{{/isString}}{{{this}}}{{#isString}}"{{/isString}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}] # noqa: E501 if {{{name}}} not in allowed_values: raise ValueError( "Invalid value for `{{{name}}}` ({0}), must be one of {1}" diff --git a/samples/server/petstore/python-aiohttp/openapi_server/models/order.py b/samples/server/petstore/python-aiohttp/openapi_server/models/order.py index 71504aba20c..88df4eb7e8f 100644 --- a/samples/server/petstore/python-aiohttp/openapi_server/models/order.py +++ b/samples/server/petstore/python-aiohttp/openapi_server/models/order.py @@ -162,7 +162,7 @@ class Order(Model): :param status: The status of this Order. :type status: str """ - allowed_values = ["placed", "approved", "delivered"] + allowed_values = ["placed", "approved", "delivered"] # noqa: E501 if status not in allowed_values: raise ValueError( "Invalid value for `status` ({0}), must be one of {1}" diff --git a/samples/server/petstore/python-aiohttp/openapi_server/models/pet.py b/samples/server/petstore/python-aiohttp/openapi_server/models/pet.py index 4931fe6fc97..d3e68e105e3 100644 --- a/samples/server/petstore/python-aiohttp/openapi_server/models/pet.py +++ b/samples/server/petstore/python-aiohttp/openapi_server/models/pet.py @@ -189,7 +189,7 @@ class Pet(Model): :param status: The status of this Pet. :type status: str """ - allowed_values = ["available", "pending", "sold"] + allowed_values = ["available", "pending", "sold"] # noqa: E501 if status not in allowed_values: raise ValueError( "Invalid value for `status` ({0}), must be one of {1}" diff --git a/samples/server/petstore/python-aiohttp/openapi_server/openapi/openapi.yaml b/samples/server/petstore/python-aiohttp/openapi_server/openapi/openapi.yaml index d5215aa956a..030b84f7402 100644 --- a/samples/server/petstore/python-aiohttp/openapi_server/openapi/openapi.yaml +++ b/samples/server/petstore/python-aiohttp/openapi_server/openapi/openapi.yaml @@ -32,7 +32,7 @@ paths: required: true x-body-name: body responses: - 405: + "405": content: {} description: Invalid input security: @@ -58,13 +58,13 @@ paths: required: true x-body-name: body responses: - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Pet not found - 405: + "405": content: {} description: Validation exception security: @@ -97,7 +97,7 @@ paths: type: array style: form responses: - 200: + "200": content: application/xml: schema: @@ -110,7 +110,7 @@ paths: $ref: '#/components/schemas/Pet' type: array description: successful operation - 400: + "400": content: {} description: Invalid status value security: @@ -139,7 +139,7 @@ paths: type: array style: form responses: - 200: + "200": content: application/xml: schema: @@ -152,7 +152,7 @@ paths: $ref: '#/components/schemas/Pet' type: array description: successful operation - 400: + "400": content: {} description: Invalid tag value security: @@ -179,7 +179,7 @@ paths: format: int64 type: integer responses: - 400: + "400": content: {} description: Invalid pet value security: @@ -202,7 +202,7 @@ paths: format: int64 type: integer responses: - 200: + "200": content: application/xml: schema: @@ -211,10 +211,10 @@ paths: schema: $ref: '#/components/schemas/Pet' description: successful operation - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Pet not found security: @@ -246,7 +246,7 @@ paths: type: string x-body-name: body responses: - 405: + "405": content: {} description: Invalid input security: @@ -283,7 +283,7 @@ paths: type: string x-body-name: body responses: - 200: + "200": content: application/json: schema: @@ -303,7 +303,7 @@ paths: description: Returns a map of status codes to quantities operationId: get_inventory responses: - 200: + "200": content: application/json: schema: @@ -330,7 +330,7 @@ paths: required: true x-body-name: body responses: - 200: + "200": content: application/xml: schema: @@ -339,7 +339,7 @@ paths: schema: $ref: '#/components/schemas/Order' description: successful operation - 400: + "400": content: {} description: Invalid Order summary: Place an order for a pet @@ -360,10 +360,10 @@ paths: schema: type: string responses: - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Order not found summary: Delete purchase order by ID @@ -385,7 +385,7 @@ paths: minimum: 1 type: integer responses: - 200: + "200": content: application/xml: schema: @@ -394,10 +394,10 @@ paths: schema: $ref: '#/components/schemas/Order' description: successful operation - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Order not found summary: Find purchase order by ID @@ -486,7 +486,7 @@ paths: schema: type: string responses: - 200: + "200": content: application/xml: schema: @@ -506,7 +506,7 @@ paths: schema: format: date-time type: string - 400: + "400": content: {} description: Invalid username/password supplied summary: Logs user into the system @@ -536,10 +536,10 @@ paths: schema: type: string responses: - 400: + "400": content: {} description: Invalid username supplied - 404: + "404": content: {} description: User not found summary: Delete user @@ -556,7 +556,7 @@ paths: schema: type: string responses: - 200: + "200": content: application/xml: schema: @@ -565,10 +565,10 @@ paths: schema: $ref: '#/components/schemas/User' description: successful operation - 400: + "400": content: {} description: Invalid username supplied - 404: + "404": content: {} description: User not found summary: Get user by user name @@ -594,10 +594,10 @@ paths: required: true x-body-name: body responses: - 400: + "400": content: {} description: Invalid user supplied - 404: + "404": content: {} description: User not found summary: Updated user diff --git a/samples/server/petstore/python-flask-python2/openapi_server/openapi/openapi.yaml b/samples/server/petstore/python-flask-python2/openapi_server/openapi/openapi.yaml index 7d6c576e674..69c15a8f11a 100644 --- a/samples/server/petstore/python-flask-python2/openapi_server/openapi/openapi.yaml +++ b/samples/server/petstore/python-flask-python2/openapi_server/openapi/openapi.yaml @@ -31,7 +31,7 @@ paths: description: Pet object that needs to be added to the store required: true responses: - 405: + "405": content: {} description: Invalid input security: @@ -56,13 +56,13 @@ paths: description: Pet object that needs to be added to the store required: true responses: - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Pet not found - 405: + "405": content: {} description: Validation exception security: @@ -95,7 +95,7 @@ paths: type: array style: form responses: - 200: + "200": content: application/xml: schema: @@ -108,7 +108,7 @@ paths: $ref: '#/components/schemas/Pet' type: array description: successful operation - 400: + "400": content: {} description: Invalid status value security: @@ -137,7 +137,7 @@ paths: type: array style: form responses: - 200: + "200": content: application/xml: schema: @@ -150,7 +150,7 @@ paths: $ref: '#/components/schemas/Pet' type: array description: successful operation - 400: + "400": content: {} description: Invalid tag value security: @@ -177,7 +177,7 @@ paths: format: int64 type: integer responses: - 400: + "400": content: {} description: Invalid pet value security: @@ -200,7 +200,7 @@ paths: format: int64 type: integer responses: - 200: + "200": content: application/xml: schema: @@ -209,10 +209,10 @@ paths: schema: $ref: '#/components/schemas/Pet' description: successful operation - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Pet not found security: @@ -243,7 +243,7 @@ paths: description: Updated status of the pet type: string responses: - 405: + "405": content: {} description: Invalid input security: @@ -278,7 +278,7 @@ paths: format: binary type: string responses: - 200: + "200": content: application/json: schema: @@ -297,7 +297,7 @@ paths: description: Returns a map of status codes to quantities operationId: get_inventory responses: - 200: + "200": content: application/json: schema: @@ -323,7 +323,7 @@ paths: description: order placed for purchasing the pet required: true responses: - 200: + "200": content: application/xml: schema: @@ -332,7 +332,7 @@ paths: schema: $ref: '#/components/schemas/Order' description: successful operation - 400: + "400": content: {} description: Invalid Order summary: Place an order for a pet @@ -353,10 +353,10 @@ paths: schema: type: string responses: - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Order not found summary: Delete purchase order by ID @@ -378,7 +378,7 @@ paths: minimum: 1 type: integer responses: - 200: + "200": content: application/xml: schema: @@ -387,10 +387,10 @@ paths: schema: $ref: '#/components/schemas/Order' description: successful operation - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Order not found summary: Find purchase order by ID @@ -476,7 +476,7 @@ paths: schema: type: string responses: - 200: + "200": content: application/xml: schema: @@ -496,7 +496,7 @@ paths: schema: format: date-time type: string - 400: + "400": content: {} description: Invalid username/password supplied summary: Logs user into the system @@ -526,10 +526,10 @@ paths: schema: type: string responses: - 400: + "400": content: {} description: Invalid username supplied - 404: + "404": content: {} description: User not found summary: Delete user @@ -546,7 +546,7 @@ paths: schema: type: string responses: - 200: + "200": content: application/xml: schema: @@ -555,10 +555,10 @@ paths: schema: $ref: '#/components/schemas/User' description: successful operation - 400: + "400": content: {} description: Invalid username supplied - 404: + "404": content: {} description: User not found summary: Get user by user name @@ -583,10 +583,10 @@ paths: description: Updated user object required: true responses: - 400: + "400": content: {} description: Invalid user supplied - 404: + "404": content: {} description: User not found summary: Updated user diff --git a/samples/server/petstore/python-flask/openapi_server/openapi/openapi.yaml b/samples/server/petstore/python-flask/openapi_server/openapi/openapi.yaml index 7d6c576e674..69c15a8f11a 100644 --- a/samples/server/petstore/python-flask/openapi_server/openapi/openapi.yaml +++ b/samples/server/petstore/python-flask/openapi_server/openapi/openapi.yaml @@ -31,7 +31,7 @@ paths: description: Pet object that needs to be added to the store required: true responses: - 405: + "405": content: {} description: Invalid input security: @@ -56,13 +56,13 @@ paths: description: Pet object that needs to be added to the store required: true responses: - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Pet not found - 405: + "405": content: {} description: Validation exception security: @@ -95,7 +95,7 @@ paths: type: array style: form responses: - 200: + "200": content: application/xml: schema: @@ -108,7 +108,7 @@ paths: $ref: '#/components/schemas/Pet' type: array description: successful operation - 400: + "400": content: {} description: Invalid status value security: @@ -137,7 +137,7 @@ paths: type: array style: form responses: - 200: + "200": content: application/xml: schema: @@ -150,7 +150,7 @@ paths: $ref: '#/components/schemas/Pet' type: array description: successful operation - 400: + "400": content: {} description: Invalid tag value security: @@ -177,7 +177,7 @@ paths: format: int64 type: integer responses: - 400: + "400": content: {} description: Invalid pet value security: @@ -200,7 +200,7 @@ paths: format: int64 type: integer responses: - 200: + "200": content: application/xml: schema: @@ -209,10 +209,10 @@ paths: schema: $ref: '#/components/schemas/Pet' description: successful operation - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Pet not found security: @@ -243,7 +243,7 @@ paths: description: Updated status of the pet type: string responses: - 405: + "405": content: {} description: Invalid input security: @@ -278,7 +278,7 @@ paths: format: binary type: string responses: - 200: + "200": content: application/json: schema: @@ -297,7 +297,7 @@ paths: description: Returns a map of status codes to quantities operationId: get_inventory responses: - 200: + "200": content: application/json: schema: @@ -323,7 +323,7 @@ paths: description: order placed for purchasing the pet required: true responses: - 200: + "200": content: application/xml: schema: @@ -332,7 +332,7 @@ paths: schema: $ref: '#/components/schemas/Order' description: successful operation - 400: + "400": content: {} description: Invalid Order summary: Place an order for a pet @@ -353,10 +353,10 @@ paths: schema: type: string responses: - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Order not found summary: Delete purchase order by ID @@ -378,7 +378,7 @@ paths: minimum: 1 type: integer responses: - 200: + "200": content: application/xml: schema: @@ -387,10 +387,10 @@ paths: schema: $ref: '#/components/schemas/Order' description: successful operation - 400: + "400": content: {} description: Invalid ID supplied - 404: + "404": content: {} description: Order not found summary: Find purchase order by ID @@ -476,7 +476,7 @@ paths: schema: type: string responses: - 200: + "200": content: application/xml: schema: @@ -496,7 +496,7 @@ paths: schema: format: date-time type: string - 400: + "400": content: {} description: Invalid username/password supplied summary: Logs user into the system @@ -526,10 +526,10 @@ paths: schema: type: string responses: - 400: + "400": content: {} description: Invalid username supplied - 404: + "404": content: {} description: User not found summary: Delete user @@ -546,7 +546,7 @@ paths: schema: type: string responses: - 200: + "200": content: application/xml: schema: @@ -555,10 +555,10 @@ paths: schema: $ref: '#/components/schemas/User' description: successful operation - 400: + "400": content: {} description: Invalid username supplied - 404: + "404": content: {} description: User not found summary: Get user by user name @@ -583,10 +583,10 @@ paths: description: Updated user object required: true responses: - 400: + "400": content: {} description: Invalid user supplied - 404: + "404": content: {} description: User not found summary: Updated user