codegen for nullable Enum throws AttributeError
See original GitHub issueHi,
we have encountered a problem when trying to generate code for nullable Enums. This bug is present since at least version 0.7.3.
Describe the bug
When generating code from an OpenAPI 3.0.3 spec with a nullable enum for my python project, the datamodel-codegen throws an AttributeError 'NoneType'" object has no attribute 'isIdentifier'
though it should be possible to model nullable enums:
https://swagger.io/docs/specification/data-models/enums/
To Reproduce
Example schema v1.yaml:
openapi: 3.0.3
info:
version: 0.1.1
title: testing-enums
paths:
/product/{product_id}/security-class:
get:
description: get the securityClass of a product
parameters:
- in: path
name: product_id
required: true
schema:
type: str
responses:
'200':
description: A Uuid that corresponds to a running task
content:
application/json:
schema:
$ref: '#/components/schemas/SecurityClass'
components:
schemas:
SecurityClass:
type: string
nullable: true
enum:
- RC1
- RC1N
- RC2
- RC2N
- RC3
- RC4
- null
Used commandline:
$ datamodel-codegen --disable-timestamp --use-default --strict-nullable --input ~/v1.yaml --output ~/my-project/models/scheduler_v1.py'
Expected behavior The models are generated correctly something like:
class SecurityClass:
__root__: Optional[SecurityClassEnum] = None
class SecurityClassEnum(Enum):
RC1 = "RC1"
RC1N = "RC1N"
RC2 = "RC2"
RC2N = "RC2N"
RC3 = "RC3"
RC4 = "RC4"
Version:
- OS: Ubuntu x64 20.04
- Python version: 3.9
- datamodel-code-generator version: 0.8.1
Additional context Add any other context about the problem here.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
enum-field-as-literal all` option is incompatible · Issue #424 ...
I expected my models to be generated looking like... class MyObject(BaseModel): my_nullable_enum: Optional[Literal['foo', 'bar']] = Field(...).
Read more >springdoc - How to achieve nullable enums? - Stack Overflow
If the query parameter is of the form color=null , null is recognized as a string, so it throws an IllegalArgumentException(No enum ......
Read more >Enums - Swagger
You can use the enum keyword to specify possible values of a request parameter or a model property. ... A nullable enum can...
Read more >Enum HOWTO — Python 3.11.1 documentation
An Enum is a set of symbolic names bound to unique values. ... For example, bool and range are not subclassable and will...
Read more >Openapi enum null values - SmartBear Community
I want to have enums that can have null values. ... CodeGenMojo.execute (CodeGenMojo.java:727) at org.apache.maven.plugin.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@mybr4inhurts I relased a new version as 0.8.2 The problem is fixed on the version.
@mybr4inhurts
Yes, I expect this behavior. I don’t want to break current behavior because this code-generator is used in a lot of projects.
I implemented nullable-enum only for nullable == true and type == string. The previous version of the generator couldn’t generate a valid Enum class with the pattern. (You said the problem on the head of this issue.) It means we didn’t break the behavior and fixed it.
But, I agree with your suggestion. The generator should tell about invalid OpenAPI Schema to users. What do you think to show a warning message when nullable is true when null is no in the enum?