-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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 the post processing of enums #18566
[Python] Fix the post processing of enums #18566
Conversation
It turns out there are still some complications with this PR, as moving the E.g.: class OuterEnum(str, Enum):
"""
allowed enum values
"""
'placed' = 'placed'
'approved' = 'approved'
'delivered' = 'delivered' Expected: class OuterEnum(str, Enum):
"""
allowed enum values
"""
PLACED = 'placed'
APPROVED = 'approved'
DELIVERED = 'delivered' |
Since the desired enum post processor is run before all other transformations in the Python generator, a working approach is to prevent the enum transformation for int enums to run if the |
thanks for the PR. can you please add a test spec to modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml ? |
@wing328 I have added the definitions below to Is there anything else I need to do, like defining the expected results somewhere? enum_number_vendor_ext:
type: integer
format: int32
enum:
- 42
- 18
- 56
x-enum-descriptions:
- 'Description for 42'
- 'Description for 18'
- 'Description for 56'
x-enum-varnames:
- FortyTwo
- Eigtheen
- FiftySix
enum_string_vendor_ext:
type: string
enum:
- FOO
- Bar
- baz
x-enum-descriptions:
- 'Description for FOO'
- 'Description for Bar'
- 'Description for baz'
x-enum-varnames:
- FOOVar
- BarVar
- bazVar |
…t uses the proper variable namesfrom x-enum-varnames Remove sample
thanks for the PR, which has been merged. have a nice weekend |
…t uses the proper variable namesfrom x-enum-varnames (OpenAPITools#18566) Remove sample
description
This fixes the issue described in #16560 of a problem with the Python family of converters that when processing enums, none of them substitutes the enum var extensions.
When processing the following schema:
The generator would yield:
The variables have the default
NUMBER_x
substitutions.problem
The problem is caused by the fact that the enum var extension substitutions done by
postProcessModelsEnum
at the beginning ofpostProcessModelsMap
are overwritten at a later point in that same method.openapi-generator/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java
Line 868 in 296a6ac
The overwriting of the variable names occurs here:
https://github.com/OpenAPITools/openapi-generator/blob/296a6ac5163b226015511b4bea42b78f0d2966f3/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java#L986C10-L998C14
Note: the proposed solution mentioned in #16560 of calling
postProcessModelsEnum
as part ofpostProcessModels
seems ineffective, as the overridepostProcessModels
is never called by the generator.proposed solution
Add a condition to test if
x-enum-varnames
exist before performing the defaultNUMBER_x
substition.A previous attempt to fix the issue by moving
postProcessModelsEnum
to the end of the method lead to a complication with string enum types, ending up with variable names ending up enclosed in double quotes.With the proposed modification the generated is as expected:
checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master
(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)