<h3 dir="auto">First check</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I added a very descriptive title to this issue.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I used the GitHub search to find a similar issue and didn't find it.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I searched the FastAPI documentation, with the integrated search.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already searched in Google "How to X in FastAPI" and didn't find any information.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already read and followed all the tutorial in the docs and didn't find an answer.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/samuelcolvin/pydantic">Pydantic</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/swagger-api/swagger-ui">Swagger UI</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/Redocly/redoc">ReDoc</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> After submitting this, I commit to one of:
<ul dir="auto">
<li>Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.</li>
<li>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.</li>
<li>Implement a Pull Request for a confirmed bug.</li>
</ul>
</li>
</ul>
<h3 dir="auto">Example</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from fastapi import FastAPI
from pydantic import BaseModel, Field
from typing import Optional

app = FastAPI()


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

@app.get(&quot;/ambiguous&quot;, 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(&quot;/nullable&quot;, response_model=MyResponseNullable)
def nullable():
    # swagger shows ``a`` as type int, and also says it can be nullable.
    return MyResponseNullable(a=1)
"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">FastAPI</span>
<span class="pl-k">from</span> <span class="pl-s1">pydantic</span> <span class="pl-k">import</span> <span class="pl-v">BaseModel</span>, <span class="pl-v">Field</span>
<span class="pl-k">from</span> <span class="pl-s1">typing</span> <span class="pl-k">import</span> <span class="pl-v">Optional</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()


<span class="pl-k">class</span> <span class="pl-v">MyResponseAmbiguous</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">a</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">int</span>]

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">"/ambiguous"</span>, <span class="pl-s1">response_model</span><span class="pl-c1">=</span><span class="pl-v">MyResponseAmbiguous</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">ambiguous</span>():
    <span class="pl-c"># swagger shows ``a`` as type int, even though it can be None</span>
    <span class="pl-k">return</span> <span class="pl-v">MyResponseAmbiguous</span>(<span class="pl-s1">a</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)



<span class="pl-k">class</span> <span class="pl-v">MyResponseNullable</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">a</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">int</span>] <span class="pl-c1">=</span> <span class="pl-v">Field</span>(..., <span class="pl-s1">nullable</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">"/nullable"</span>, <span class="pl-s1">response_model</span><span class="pl-c1">=</span><span class="pl-v">MyResponseNullable</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">nullable</span>():
    <span class="pl-c"># swagger shows ``a`` as type int, and also says it can be nullable.</span>
    <span class="pl-k">return</span> <span class="pl-v">MyResponseNullable</span>(<span class="pl-s1">a</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)</pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">When generating an openapi schema, FastAPI does not mark <code class="notranslate">Optional</code> items as nullable - as in the <code class="notranslate">MyResponseAmbiguous</code> model above.<br>
To mark an item as nullable in openapi you need to manually pass <code class="notranslate">nullable=True</code> to the pydantic Field function - as in the <code class="notranslate">MyResponseNullable</code> model above.<br>
How can this be done automatically?</p>
<p dir="auto">Additionally, one of the models has <code class="notranslate">a</code> 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.</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/33386122/110582238-05721a80-81c0-11eb-9afb-6b9e62265a2e.png"><img width="401" alt="Screen Shot 2021-03-10 at 4 45 25 pm" src="https://user-images.githubusercontent.com/33386122/110582238-05721a80-81c0-11eb-9afb-6b9e62265a2e.png" style="max-width: 100%;"></a></p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS: Macos</li>
<li>FastAPI Version: 0.63.0</li>
<li>Python version: 3.7</li>
</ul>
<h3 dir="auto">First check</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I added a very descriptive title to this issue.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I used the GitHub search to find a similar issue and didn't find it.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I searched the FastAPI documentation, with the integrated search.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already searched in Google "How to X in FastAPI" and didn't find any information.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already read and followed all the tutorial in the docs and didn't find an answer.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/samuelcolvin/pydantic">Pydantic</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/swagger-api/swagger-ui">Swagger UI</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/Redocly/redoc">ReDoc</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> After submitting this, I commit to one of:
<ul dir="auto">
<li>Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.</li>
<li>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.</li>
<li>Implement a Pull Request for a confirmed bug.</li>
</ul>
</li>
</ul>

<h3 dir="auto">Example</h3>
<p dir="auto">Here's a self-contained, <a href="https://stackoverflow.com/help/minimal-reproducible-example" rel="nofollow">minimal, reproducible, example</a> with my use case:</p>

<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import enum

from typing import Optional

import pytest

from fastapi import FastAPI
from pydantic import BaseModel, Field


app = FastAPI()


class RHELVersions(enum.Enum):
    &quot;&quot;&quot;Available RHEL versions within the database.&quot;&quot;&quot;

    el7: str = &quot;RHEL 7&quot;
    el8: str = &quot;RHEL 8&quot;
    el9: str = &quot;RHEL 9&quot;


class DeviceEntry(BaseModel):
    &quot;&quot;&quot;A model which represents the single hardware device.&quot;&quot;&quot;

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


@app.get(&quot;/&quot;, response_model=DeviceEntry)
def read_root() -&gt; DeviceEntry:
    return DeviceEntry(simple_property=None, complex_property=None)


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


&quot;&quot;&quot;
  +  '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'},
&quot;&quot;&quot;
"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">enum</span>

<span class="pl-k">from</span> <span class="pl-s1">typing</span> <span class="pl-k">import</span> <span class="pl-v">Optional</span>

<span class="pl-k">import</span> <span class="pl-s1">pytest</span>

<span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">FastAPI</span>
<span class="pl-k">from</span> <span class="pl-s1">pydantic</span> <span class="pl-k">import</span> <span class="pl-v">BaseModel</span>, <span class="pl-v">Field</span>


<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()


<span class="pl-k">class</span> <span class="pl-v">RHELVersions</span>(<span class="pl-s1">enum</span>.<span class="pl-v">Enum</span>):
    <span class="pl-s">"""Available RHEL versions within the database."""</span>

    <span class="pl-s1">el7</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-s">"RHEL 7"</span>
    <span class="pl-s1">el8</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-s">"RHEL 8"</span>
    <span class="pl-s1">el9</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-s">"RHEL 9"</span>


<span class="pl-k">class</span> <span class="pl-v">DeviceEntry</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s">"""A model which represents the single hardware device."""</span>

    <span class="pl-s1">simple_property</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">bool</span>] <span class="pl-c1">=</span> <span class="pl-v">Field</span>(<span class="pl-c1">None</span>, <span class="pl-s1">nullable</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
    <span class="pl-s1">complex_property</span>: <span class="pl-v">Optional</span>[<span class="pl-v">RHELVersions</span>] <span class="pl-c1">=</span> <span class="pl-v">Field</span>(<span class="pl-c1">None</span>, <span class="pl-s1">nullable</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)


<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">"/"</span>, <span class="pl-s1">response_model</span><span class="pl-c1">=</span><span class="pl-v">DeviceEntry</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">read_root</span>() <span class="pl-c1">-&gt;</span> <span class="pl-v">DeviceEntry</span>:
    <span class="pl-k">return</span> <span class="pl-v">DeviceEntry</span>(<span class="pl-s1">simple_property</span><span class="pl-c1">=</span><span class="pl-c1">None</span>, <span class="pl-s1">complex_property</span><span class="pl-c1">=</span><span class="pl-c1">None</span>)


<span class="pl-k">def</span> <span class="pl-en">test_case</span>():
    <span class="pl-s1">spec</span> <span class="pl-c1">=</span> <span class="pl-s1">app</span>.<span class="pl-en">openapi</span>()
    <span class="pl-k">assert</span> <span class="pl-s1">spec</span>[<span class="pl-s">"components"</span>][<span class="pl-s">"schemas"</span>][<span class="pl-s">"DeviceEntry"</span>][<span class="pl-s">"properties"</span>][
        <span class="pl-s">"simple_property"</span>
    ][<span class="pl-s">"nullable"</span>]
    <span class="pl-k">with</span> <span class="pl-s1">pytest</span>.<span class="pl-en">raises</span>(<span class="pl-v">KeyError</span>):
        <span class="pl-k">assert</span> <span class="pl-s1">spec</span>[<span class="pl-s">"components"</span>][<span class="pl-s">"schemas"</span>][<span class="pl-s">"DeviceEntry"</span>][<span class="pl-s">"properties"</span>][
            <span class="pl-s">"complex_property"</span>
        ][<span class="pl-s">"nullable"</span>]


<span class="pl-s">"""</span>
<span class="pl-s">  +  'components': {'schemas': {'DeviceEntry': {'description': 'A model which '</span>
<span class="pl-s">  +                                                            'represents the '</span>
<span class="pl-s">  +                                                            'single hardware '</span>
<span class="pl-s">  +                                                            'device.',</span>
<span class="pl-s">  +                                             'properties': {'complex_property': {'$ref': '#/components/schemas/RHELVersions'},</span>
<span class="pl-s">  +                                                            'simple_property': {'nullable': True,</span>
<span class="pl-s">  +                                                                                'title': 'Simple '</span>
<span class="pl-s">  +                                                                                         'Property',</span>
<span class="pl-s">  +                                                                                'type': 'boolean'}},</span>
<span class="pl-s">  +                                             'title': 'DeviceEntry',</span>
<span class="pl-s">  +                                             'type': 'object'},</span>
<span class="pl-s">"""</span></pre></div>
<h3 dir="auto">Description</h3>

<p dir="auto">Run pytest {path_tothe_module}.<br>
Simple property has nullable in the apispec, but complex property doesn't.</p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS: [e.g. Linux / Windows / macOS]: Linux</li>
<li>FastAPI Version [e.g. 0.3.0]: 0.62</li>
</ul>
<p dir="auto">To know the FastAPI version use:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="python -c &quot;import fastapi; print(fastapi.__version__)&quot;"><pre class="notranslate">python -c <span class="pl-s"><span class="pl-pds">"</span>import fastapi; print(fastapi.__version__)<span class="pl-pds">"</span></span></pre></div>
<ul dir="auto">
<li>Python version: 3.9</li>
</ul>
<p dir="auto">To know the Python version use:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="python --version"><pre class="notranslate">python --version</pre></div>
<h3 dir="auto">Additional context</h3>