Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] fix numeric enum in python flask, aiohttp #5019

Merged
merged 3 commits into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/utils/ensure-up-to-date
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public void processOpts() {
// 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;
}
Expand Down Expand Up @@ -566,7 +566,7 @@ public String toApiFilename(String name) {
name = name.replaceAll("-", "_");

// e.g. PhoneNumberApi.py => phone_number_api.py
return underscore(name+ "_" + apiNameSuffix);
return underscore(name + "_" + apiNameSuffix);
}

@Override
Expand All @@ -584,7 +584,7 @@ public String toApiVarName(String name) {
if (name.length() == 0) {
return "default_api";
}
return underscore(name+ "_" + apiNameSuffix);
return underscore(name + "_" + apiNameSuffix);
}

@Override
Expand Down Expand Up @@ -649,6 +649,7 @@ public String generatePackageName(String packageName) {

/**
* Return the default value of the property
*
* @param p OpenAPI property object
* @return string presentation of the default value of the property
*/
Expand Down Expand Up @@ -678,7 +679,7 @@ public String toDefaultValue(Schema p) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are tying to minimize the usage of flake8 E501 and other flake8 opt out lines.
Can you spread this to a multiline statement so we won't need # noqa: E501?

{{#isListContainer}}
if not set({{{name}}}).issubset(set(allowed_values)):
raise ValueError(
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are tying to minimize the usage of flake8 E501 and other flake8 opt out lines.
Can you spread this to a multiline statement so we won't need # noqa: E501?

if {{{name}}} not in allowed_values:
raise ValueError(
"Invalid value for `{{{name}}}` ({0}), must be one of {1}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ 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
"""
{{#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
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are tying to minimize the usage of flake8 E501 and other flake8 opt out lines.
Can you spread this to a multiline statement so we won't need # noqa: E501?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this in a separate PR instead. I'll open an issue to track it to see if anyone has the time to improve it.

(this PR simply plots the fix from the python client template)

Copy link
Contributor

@spacether spacether Jan 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you suggesting that we remove the E501s in a separate PR? That sounds good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #5030 for tracking.

{{#isListContainer}}
if not set({{{name}}}).issubset(set(allowed_values)):
raise ValueError(
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are tying to minimize the usage of flake8 E501 and other flake8 opt out lines.
Can you spread this to a multiline statement so we won't need # noqa: E501?

if {{{name}}} not in allowed_values:
raise ValueError(
"Invalid value for `{{{name}}}` ({0}), must be one of {1}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def status(self, status):
: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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def status(self, status):
: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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ paths:
required: true
x-body-name: body
responses:
405:
"405":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These spec updates are unexpected in this pull review because we did not edit the spec file.
Can you please add the below sample generation files to https://github.com/OpenAPITools/openapi-generator/blob/master/bin/utils/ensure-up-to-date?

  • python-aiohttp
  • python-flask-python2
  • python-flask

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to ensure-up-to-date.

These scripts are using petstore.yaml (original) but not the fake petstore spec (which has numeric enums)

Copy link
Contributor

@spacether spacether Jan 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a model with numeric enums in petstore.yaml?
That lets us verify this fix in the repo and this PR. Or a java test like I mention below also verifies the fix.

Copy link
Member Author

@wing328 wing328 Jan 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can update the script to use the fake petstore spec instead (petstore.yaml shouldn't be updated)

When a generator gets mature, we will use fake petstore spec instead of petstore.yaml/json as the generator should be able to handle those edge cases in fake petstore spec.

I'm not sure if these generators can handle the edge cases in fake petstore spec.

content: {}
description: Invalid input
security:
Expand All @@ -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:
Expand Down Expand Up @@ -97,7 +97,7 @@ paths:
type: array
style: form
responses:
200:
"200":
content:
application/xml:
schema:
Expand All @@ -110,7 +110,7 @@ paths:
$ref: '#/components/schemas/Pet'
type: array
description: successful operation
400:
"400":
content: {}
description: Invalid status value
security:
Expand Down Expand Up @@ -139,7 +139,7 @@ paths:
type: array
style: form
responses:
200:
"200":
content:
application/xml:
schema:
Expand All @@ -152,7 +152,7 @@ paths:
$ref: '#/components/schemas/Pet'
type: array
description: successful operation
400:
"400":
content: {}
description: Invalid tag value
security:
Expand All @@ -179,7 +179,7 @@ paths:
format: int64
type: integer
responses:
400:
"400":
content: {}
description: Invalid pet value
security:
Expand All @@ -202,7 +202,7 @@ paths:
format: int64
type: integer
responses:
200:
"200":
content:
application/xml:
schema:
Expand All @@ -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:
Expand Down Expand Up @@ -246,7 +246,7 @@ paths:
type: string
x-body-name: body
responses:
405:
"405":
content: {}
description: Invalid input
security:
Expand Down Expand Up @@ -283,7 +283,7 @@ paths:
type: string
x-body-name: body
responses:
200:
"200":
content:
application/json:
schema:
Expand All @@ -303,7 +303,7 @@ paths:
description: Returns a map of status codes to quantities
operationId: get_inventory
responses:
200:
"200":
content:
application/json:
schema:
Expand All @@ -330,7 +330,7 @@ paths:
required: true
x-body-name: body
responses:
200:
"200":
content:
application/xml:
schema:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -385,7 +385,7 @@ paths:
minimum: 1
type: integer
responses:
200:
"200":
content:
application/xml:
schema:
Expand All @@ -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
Expand Down Expand Up @@ -486,7 +486,7 @@ paths:
schema:
type: string
responses:
200:
"200":
content:
application/xml:
schema:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -556,7 +556,7 @@ paths:
schema:
type: string
responses:
200:
"200":
content:
application/xml:
schema:
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading