-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
fix bug of generating enum class in python-fastapi generator #14242
Conversation
@@ -18,6 +25,21 @@ from pydantic import AnyUrl, BaseModel, EmailStr, Field, validator # noqa: F401 | |||
|
|||
{{#models}} | |||
{{#model}} | |||
{{#isEnum}} | |||
class {{classname}}(str, Enum): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the enum is for schema integers, numbers, or boolean, or includes null?
This forces them all to inherit str
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't enums in OpenApi not always string based?
https://swagger.io/docs/specification/data-models/enums/
The only way I found to specify integer like enums would be something like:
# Source: https://stackoverflow.com/questions/66465888/how-to-define-enum-mapping-in-openapi
Severity:
type: integer
oneOf:
- title: HIGH
const: 2
description: An urgent problem
- title: MEDIUM
const: 1
- title: LOW
const: 0
description: Can wait forever
But I think that would need anyways a different handling, as the example is not even typed as enum.
Even this being maybe not perfect, it is already a big improvement to no generation at all IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enums can have any type. If type is unset enum values can be any json schema type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide an example from openapi?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, here is an enum that contains a string, int, float, and boolean
EnumOfAnyType:
enum:
- "a"
- 1
- 3.14
- true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this what we're looking for?
ReportingType:
type: integer
enum:
- 1
- 7
Great initiative! @liulu1998 are you working on the tests or do you need support? |
@Linus-Boehm I'm working on those tests. |
I've tried to generate a python-fastapi server now and get empty enum classes like this:
So this is I guess will be handled by this issue. Are you also aware that in endpoints enum type is not present, so the generated code is invalid:
Note the |
please give if the implementation looks good, we can plot it to |
|
are you using openapi-generator v6.3.0? |
i'm using openapi-generator-cli as NPM package: |
pls run |
|
Any updates on this? |
Would be great if we had this fix in. |
What would it mean to 'plot' it to |
Hi all, I've filed #16321 to improve pydantic support in python-fastapi generator (based on pydantic implementation in python client generator). Can you please test the branch My preliminary test result looks good but I would like more python-fastapi users to test it as I'm no expert in python-fastapi. Thank you. |
To test the branch, simply run the following:
|
Would it be possible to merge this onto master as a quickfix untill #16321 is ready? The current state of #16321 doesn't work with our OpenAPI documents sadly. The generated code is invalid Python. With this PR merged onto master to get the changes from #14470 for inversion of control, the generated FastAPI code works as expected for our OpenAPI documents. |
Hi all. I did some tests. The model classes look ok. The problem with the enums still persists in the router classes. List enums work, but single enums do not. It creates empty types:
It creates something like that. (Note the type missing after 'status:'):
|
You can try #16321. Some people have reported better results with that branch. |
Any update on this regarding a merge? |
please help test #17369 instead which uses pydantic v2 |
Fix #10127
Previous
python-fastapi
generator generated enum classes with empty body. In this PR, I try to generate Python's standard enum class, mapping values tostr
.PR checklist
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*
.For Windows users, please run the script in Git BASH.
master
(6.3.0) (minor release - breaking changes with fallbacks),7.0.x
(breaking changes without fallbacks)