Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Is it possible to generate openapi.json with "nullable" : "true" for non-required property? #833

Closed
dennismeissel opened this issue Jan 8, 2020 · 5 comments
Labels
question Question or problem question-migrate

Comments

@dennismeissel
Copy link

dennismeissel commented Jan 8, 2020

Description

Is it possible to generate openapi.json with "nullable" : "true" for non-required property?

Additional context

I'm using NSwag to generate C# code from FastAPI openapi.json. I need to set "nullable" as "true" to make it work correctly.

I have a FastAPI Model like this:

class Topic(BaseModel):
    first_id: str = None

I want to get a schema like this:

"Topic": {
  "title": "Topic",
  "type": "object",
    "properties": {
      "first_id": {
        "title": "First_Id",
        "type": "string",
        "nullable" : "true"
      }
  }
}

But I haven't found a way to set the field "nullable". And if it's not provided, it is "false" by default.

@dennismeissel dennismeissel added the question Question or problem label Jan 8, 2020
@nkhitrov
Copy link

nkhitrov commented Jan 21, 2020

You can use Field to set nullable=True by default.

Example:

from fastapi import FastAPI, Depends, Query, HTTPException
from typing import Optional
from pydantic import BaseModel, Field

app = FastAPI()


class Model(BaseModel):
    a: Optional[int]
    b: Optional[int] = None
    c: Optional[int] = Field(None, nullable=True)


@app.get("/test", response_model=Model)
def foo(m: Model):
    return m

Component in /openapi.json

...
Model: {
    title: "Model",
    required: [
		"c"
	],
    type: "object",
    properties: {
    a: {
    	title: "A",
    	type: "integer"
	},
    b: {
    	title: "B",
    	type: "integer"
	},
    c: {
    	title: "C",
    	type: "integer",
    	nullable: true
	}
}
...

@dennismeissel
Copy link
Author

@SlyFoxy Thanks for answer, it helped!

I found out, that Field is not working with FastAPI

But Schema did the job!

from pydantic import BaseModel, Schema

class Topic(BaseModel):
    first_id: str = Schema(None, nullable=True)

And /openapi.json

"Topic": {
    "title": "Topic",
    "type": "object",
    "properties": {
        "first_id": {
        "title": "First_Id",
        "type": "string",
        "nullable": true
        }
    }
}

@nkhitrov
Copy link

Fastapi works with 'Field' from 'pydantic' in latest version of 'fastapi' with 'pydantic=1.0'. Just update it :)

@dennismeissel
Copy link
Author

Ah, ok, understood!

Thank you :)

@tiangolo
Copy link
Owner

tiangolo commented Apr 7, 2020

Thanks for the help here @SlyFoxy ! 🍰 🙇‍♂️

Thanks @dennismeissel for reporting back and closing the issue 👍

@tiangolo tiangolo changed the title [QUESTION] Is it possible to generate openapi.json with "nullable" : "true" for non-required property? Is it possible to generate openapi.json with "nullable" : "true" for non-required property? Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7882 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

3 participants