question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional not generated for nullable strings in OpenAPI

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:19 (19 by maintainers)

github_iconTop GitHub Comments

3reactions
languitarcommented, Feb 9, 2021

Thank you again for being so quick on this one! This looks now exactly as expected!

3reactions
goodoldneoncommented, Feb 8, 2021

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found