Issue 1:
First check

I added a very descriptive title to this issue.

I used the GitHub search to find a similar issue and didn't find it.

I searched the FastAPI documentation, with the integrated search.

I already searched in Google "How to X in FastAPI" and didn't find any information.

I already read and followed all the tutorial in the docs and didn't find an answer.

I already checked if it is not related to FastAPI but to Pydantic.

I already checked if it is not related to FastAPI but to Swagger UI.

I already checked if it is not related to FastAPI but to ReDoc.

After submitting this, I commit to one of:

Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

Implement a Pull Request for a confirmed bug.

Example

from fastapi import FastAPI
from pydantic import BaseModel, Field
from typing import Optional

app = FastAPI()

class MyResponseAmbiguous(BaseModel):
a: Optional[int]

@app.get("/ambiguous", response_model=MyResponseAmbiguous)
def ambiguous():
# swagger shows ``a`` as type int, even though it can be None
return MyResponseAmbiguous(a=1)

class MyResponseNullable(BaseModel):
a: Optional[int] = Field(..., nullable=True)

@app.get("/nullable", response_model=MyResponseNullable)
def nullable():
# swagger shows ``a`` as type int, and also says it can be nullable.
return MyResponseNullable(a=1)

Description

When generating an openapi schema, FastAPI does not mark Optional items as nullable - as in the MyResponseAmbiguous model above.

To mark an item as nullable in openapi you need to manually pass nullable=True to the pydantic Field function - as in the MyResponseNullable model above.

How can this be done automatically?

Additionally, one of the models has a marked as required (red asterisk) and the other does not - is this the intended behaviour? I think it should be marked as required in both models.

Environment

OS: Macos

FastAPI Version: 0.63.0

Python version: 3.7

Issue 2:
First check

I added a very descriptive title to this issue.

I used the GitHub search to find a similar issue and didn't find it.

I searched the FastAPI documentation, with the integrated search.

I already searched in Google "How to X in FastAPI" and didn't find any information.

I already read and followed all the tutorial in the docs and didn't find an answer.

I already checked if it is not related to FastAPI but to Pydantic.

I already checked if it is not related to FastAPI but to Swagger UI.

I already checked if it is not related to FastAPI but to ReDoc.

After submitting this, I commit to one of:

Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

Implement a Pull Request for a confirmed bug.

Example

Here's a self-contained, minimal, reproducible, example with my use case:

import enum

from typing import Optional

import pytest

from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()

class RHELVersions(enum.Enum):
"""Available RHEL versions within the database."""

el7: str = "RHEL 7"
el8: str = "RHEL 8"
el9: str = "RHEL 9"

class DeviceEntry(BaseModel):
"""A model which represents the single hardware device."""

simple_property: Optional[bool] = Field(None, nullable=True)
complex_property: Optional[RHELVersions] = Field(None, nullable=True)

@app.get("/", response_model=DeviceEntry)
def read_root() -> DeviceEntry:
return DeviceEntry(simple_property=None, complex_property=None)

def test_case():
spec = app.openapi()
assert spec["components"]["schemas"]["DeviceEntry"]["properties"][
"simple_property"
]["nullable"]
with pytest.raises(KeyError):
assert spec["components"]["schemas"]["DeviceEntry"]["properties"][
"complex_property"
]["nullable"]

"""
+ 'components': {'schemas': {'DeviceEntry': {'description': 'A model which '
+ 'represents the '
+ 'single hardware '
+ 'device.',
+ 'properties': {'complex_property': {'$ref': '#/components/schemas/RHELVersions'},
+ 'simple_property': {'nullable': True,
+ 'title': 'Simple '
+ 'Property',
+ 'type': 'boolean'}},
+ 'title': 'DeviceEntry',
+ 'type': 'object'},
"""

Description

Run pytest {path_tothe_module}.

Simple property has nullable in the apispec, but complex property doesn't.

Environment

OS: [e.g. Linux / Windows / macOS]: Linux

FastAPI Version [e.g. 0.3.0]: 0.62

To know the FastAPI version use:

python -c "import fastapi; print(fastapi.__version__)"

Python version: 3.9

To know the Python version use:

python --version

Additional context