Optional not generated for nullable strings in OpenAPI
See original GitHub issueDescribe the bug
When using type: string
in combination with nullable: true
in an OpenAPI specification, the resulting type should be Optional[str]
but it is not.
To Reproduce
Example schema:
openapi: 3.0.3
info:
version: 1.0.0
title: testapi
license:
name: proprietary
servers: []
paths: {}
components:
schemas:
TopLevel:
type: object
properties:
cursors:
type: object
properties:
prev:
type: string
nullable: true
required:
- prev
required:
- cursors
Used commandline:
$ env/bin/datamodel-codegen --input spec.yaml --output test.py
Expected behavior
The generated class should be
class Cursors(BaseModel):
prev: Optional[str]
but currently it is:
class Cursors(BaseModel):
prev: str
Version:
- OS: Linux
- Python version: 3.9
- datamodel-code-generator version: 0.7.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:19 (19 by maintainers)
Top Results From Across the Web
How can I make optional OpenAPI parameters nullable using ...
One solution is to create a springdoc-openapi OpenApiCustomiser Spring bean which sets all properties to nullable unless they are in the ...
Read more >Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations in each parameter of your API requests made from a SDK....
Read more >How to manage nullable properties - Jane - Read the Docs
Most of the time, the schema you get from vendor have issues about nullability of their properties. Here Jane has an option called...
Read more >ASP.NET Core nullable route params in Swagger
I recently had an issue with optional route parameters in ASP.NET Core Web API not showing as such in the Open API specification...
Read more >Enums - Swagger
Note that null must be explicitly included in the list of enum values. Using nullable: true alone is not enough here. Reusable enums....
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
Thank you again for being so quick on this one! This looks now exactly as expected!
Thank you so much for the quick fix, @koxudaxi! The new version fixes my problem when I use the
--strict-nullable
flag. I really appreciate your responsiveness, and thanks for maintaining such a great tool!