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
Optional not generated for nullable strings in OpenAPI #327
Comments
Maybe this is a regression. #8 looks like this used to be implemented. |
The behavior around |
@languitar I think that If Also, I expect
How did you feel it? |
Interesting. OK, I write an example.
Did you expect the field from the schema? Your opinion is correct, But the change is a too huge an impact. What did you think about it? |
Anything that enables me to also provide |
Yes, that's what I'd expect. I think OpenAPI defaults A CLI argument sounds like a good choice! That'd make sure we don't introduce breaking changes for other people. |
That's right. Is there any change to get a quick fix for this? This is currently blocking one of our projects. |
Sorry, Nothing!! |
That's perfectly fine. Your fixes always extremely fast :) |
@languitar @goodoldneon command
openapi.yaml 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
pet:
type: object
properties:
name:
type: string
default: dog output# generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2021-02-05T19:57:32+00:00
from __future__ import annotations
from typing import Optional
from pydantic import BaseModel, Field
class Cursors(BaseModel):
prev: Optional[str] = Field(...)
class TopLevel(BaseModel):
cursors: Cursors
class Pet(BaseModel):
name: str = 'dog' @goodoldneon |
Thanks again for fast resolution! That version and CLI flag seems to work for the use case I have described. However, now I am also seeing other changes to the generated models that I do not expect. For instance, I am now seeing to following hunk: The respective OpenAPI type is: Problem:
type: object
properties:
type:
type: string
format: uri
description: An absolute URI that identifies the problem type. When dereferenced,
it SHOULD provide human-readable documentation for the problem type (e.g.,
using HTML).
default: about:blank
example: https://zalando.github.io/problem/constraint-violation
title:
type: string
description: 'A short, summary of the problem type. Written in english and
readable for engineers (usually not suited for non technical stakeholders
and not localized); example: Service Unavailable'
status:
type: integer
format: int32
description: The HTTP status code generated by the origin server for this
occurrence of the problem.
example: 503
detail:
type: string
description: A human readable explanation specific to this occurrence of
the problem.
example: Connection to database timed out
instance:
type: string
format: uri
description: An absolute URI that identifies the specific occurrence of
the problem. It may or may not yield further information if dereferenced. None of the fields are required (there is no |
Thank you for testing the new version.
Yes. But, I can discuss it with you. I imagined this matrix
You said the last row should be @goodoldneon |
For the last case of your table, OpenAPI explicitly allows an absent value. If there is no |
Thank you for your comment. I'm waiting for other feedback. |
Thank you so much for the quick fix, @koxudaxi! The new version fixes my problem when I use the |
@languitar |
Thank you again for being so quick on this one! This looks now exactly as expected! |
Should I close the issue? |
I have closed the issue. |
Describe the bug
When using
type: string
in combination withnullable: true
in an OpenAPI specification, the resulting type should beOptional[str]
but it is not.To Reproduce
Example schema:
Used commandline:
Expected behavior
The generated class should be
but currently it is:
Version:
The text was updated successfully, but these errors were encountered: