<p dir="auto"><strong>Description</strong></p>
<p dir="auto">Up until database interaction is introduced, the tutorial for FastAPI uses pydantic models for everything, such as this example in <a href="https://fastapi.tiangolo.com/tutorial/extra-models/" rel="nofollow">the sextion on Extra Models</a> :</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: str = None


class UserInDB(BaseModel):
    username: str
    hashed_password: str
    email: EmailStr
    full_name: str = None"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">UserOut</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">username</span>: <span class="pl-s1">str</span>
    <span class="pl-s1">email</span>: <span class="pl-v">EmailStr</span>
    <span class="pl-s1">full_name</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-c1">None</span>


<span class="pl-k">class</span> <span class="pl-v">UserInDB</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">username</span>: <span class="pl-s1">str</span>
    <span class="pl-s1">hashed_password</span>: <span class="pl-s1">str</span>
    <span class="pl-s1">email</span>: <span class="pl-v">EmailStr</span>
    <span class="pl-s1">full_name</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-c1">None</span></pre></div>
<p dir="auto">This alows for the "database model" to have private data which will not be exposed through the API. Later, in <a href="https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/" rel="nofollow">the section on security</a>, a similar trick is used, but this time using inheritance to stack the two models (which I find makes the return-casting used by some functions better encoded in the type system).</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class User(BaseModel):
    username: str
    email: str = None
    full_name: str = None
    disabled: bool = None


class UserInDB(User):
    hashed_password: str"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">User</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">username</span>: <span class="pl-s1">str</span>
    <span class="pl-s1">email</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-c1">None</span>
    <span class="pl-s1">full_name</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-c1">None</span>
    <span class="pl-s1">disabled</span>: <span class="pl-s1">bool</span> <span class="pl-c1">=</span> <span class="pl-c1">None</span>


<span class="pl-k">class</span> <span class="pl-v">UserInDB</span>(<span class="pl-v">User</span>):
    <span class="pl-s1">hashed_password</span>: <span class="pl-s1">str</span></pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="def get_db_user() -&gt; UserInDB:
    return UserInDB(
        username=&quot;johndoe&quot;, full_name=&quot;John Doe&quot;,
        email=&quot;johndoe@example.com&quot;,
        hashed_password=&quot;fakehashedsecret&quot;,
        disabled=False
    )

def get_user() -&gt; User:
    return get_db_user()"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-en">get_db_user</span>() <span class="pl-c1">-&gt;</span> <span class="pl-v">UserInDB</span>:
    <span class="pl-k">return</span> <span class="pl-v">UserInDB</span>(
        <span class="pl-s1">username</span><span class="pl-c1">=</span><span class="pl-s">"johndoe"</span>, <span class="pl-s1">full_name</span><span class="pl-c1">=</span><span class="pl-s">"John Doe"</span>,
        <span class="pl-s1">email</span><span class="pl-c1">=</span><span class="pl-s">"johndoe@example.com"</span>,
        <span class="pl-s1">hashed_password</span><span class="pl-c1">=</span><span class="pl-s">"fakehashedsecret"</span>,
        <span class="pl-s1">disabled</span><span class="pl-c1">=</span><span class="pl-c1">False</span>
    )

<span class="pl-k">def</span> <span class="pl-en">get_user</span>() <span class="pl-c1">-&gt;</span> <span class="pl-v">User</span>:
    <span class="pl-k">return</span> <span class="pl-en">get_db_user</span>()</pre></div>
<p dir="auto">However, when <a href="https://fastapi.tiangolo.com/tutorial/sql-databases/" rel="nofollow">proper databases are introduced</a>, those Pydantic models are dropped in favor of a single SQLAlchemy ORM model, with no effort to bridge the two parts. And while one could see this as the SQLAlchemy models completely superseding the Pydantic models, <a href="https://github.com/tiangolo/full-stack-fastapi-postgresql">the fullstack demo app</a> appears to <a href="https://github.com/tiangolo/full-stack-fastapi-postgresql/blob/ecd634e49715bb57fdfeb35b9dba21a6e94cf012/%7B%7Bcookiecutter.project_slug%7D%7D/backend/app/app/api/api_v1/endpoints/login.py#L13-L16">actually use both</a>, so there appears to be value in attempting to use them together, something which the documentation doesn't seem to address.</p>
<p dir="auto">So can/should Pydantic and SQLAlchemy models be used together? If they are, how is one meant to connect the two together and can this still be done while maintaining some kind of type hierarchy?</p>
<p dir="auto"><strong>Describe the bug</strong><br>
Getting duplicate headers on API methods with subdependencies requiring the same dependency with a Header argument.</p>
<p dir="auto"><strong>To Reproduce</strong><br>
I've created an example repo to reproduce <a href="https://github.com/henriklindgren/fastapi-header-bug">https://github.com/henriklindgren/fastapi-header-bug</a><br>
Steps to reproduce the behavior:</p>
<ol dir="auto">
<li>Create an API-method which uses the same dependency, with a Header argument, in each of 2 paths of the dependency tree. Like this <a href="https://github.com/henriklindgren/fastapi-header-bug/blob/master/app/main.py">https://github.com/henriklindgren/fastapi-header-bug/blob/master/app/main.py</a></li>
<li>Visit generated OpenApi json and see duplicate header requirements for the method.</li>
</ol>
<div class="highlight highlight-source-json notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&quot;parameters&quot;: [
  {
    &quot;required&quot;: true,
    &quot;schema&quot;: {
      &quot;title&quot;: &quot;Someheader&quot;,
      &quot;type&quot;: &quot;string&quot;
    },
    &quot;name&quot;: &quot;someheader&quot;,
    &quot;in&quot;: &quot;header&quot;
  },
  {
    &quot;required&quot;: true,
    &quot;schema&quot;: {
      &quot;title&quot;: &quot;Someheader&quot;,
      &quot;type&quot;: &quot;string&quot;
    },
    &quot;name&quot;: &quot;someheader&quot;,
    &quot;in&quot;: &quot;header&quot;
  }
]"><pre class="notranslate"><span class="pl-ent">"parameters"</span>: [
  {
    <span class="pl-ent">"required"</span>: <span class="pl-c1">true</span>,
    <span class="pl-ent">"schema"</span>: {
      <span class="pl-ent">"title"</span>: <span class="pl-s"><span class="pl-pds">"</span>Someheader<span class="pl-pds">"</span></span>,
      <span class="pl-ent">"type"</span>: <span class="pl-s"><span class="pl-pds">"</span>string<span class="pl-pds">"</span></span>
    },
    <span class="pl-ent">"name"</span>: <span class="pl-s"><span class="pl-pds">"</span>someheader<span class="pl-pds">"</span></span>,
    <span class="pl-ent">"in"</span>: <span class="pl-s"><span class="pl-pds">"</span>header<span class="pl-pds">"</span></span>
  },
  {
    <span class="pl-ent">"required"</span>: <span class="pl-c1">true</span>,
    <span class="pl-ent">"schema"</span>: {
      <span class="pl-ent">"title"</span>: <span class="pl-s"><span class="pl-pds">"</span>Someheader<span class="pl-pds">"</span></span>,
      <span class="pl-ent">"type"</span>: <span class="pl-s"><span class="pl-pds">"</span>string<span class="pl-pds">"</span></span>
    },
    <span class="pl-ent">"name"</span>: <span class="pl-s"><span class="pl-pds">"</span>someheader<span class="pl-pds">"</span></span>,
    <span class="pl-ent">"in"</span>: <span class="pl-s"><span class="pl-pds">"</span>header<span class="pl-pds">"</span></span>
  }
]</pre></div>
<p dir="auto"><strong>Expected behavior</strong><br>
A single header requirement shown in the OpenApi definition.</p>
<p dir="auto"><strong>Environment:</strong></p>
<ul dir="auto">
<li>OS: Linux</li>
<li>FastAPI Version: 0.33.0</li>
<li>Python Version: 3.7.3</li>
</ul>