<p dir="auto">I use <code class="notranslate">Depends()</code> to populate model from query parameters. Recently noticed, that dependency resolving somehow ignores pydantic model configuration. According to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="623996371" data-permission-text="Title is private" data-url="https://github.com/tiangolo/fastapi/issues/1474" data-hovercard-type="issue" data-hovercard-url="/tiangolo/fastapi/issues/1474/hovercard" href="https://github.com/tiangolo/fastapi/issues/1474">#1474</a>, it may be the same issue.</p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS: linux</li>
<li>FastAPI Version [e.g. 0.3.0]: 0.61</li>
<li>Python version: 3.8.2</li>
</ul>
<h3 dir="auto">Testcase</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from pydantic import BaseModel, Field
from fastapi import Depends, FastAPI

app = FastAPI()

class TestParams(BaseModel):
    class Config:
        allow_population_by_field_name = True

    param: bool = Field(False, alias='alias')


@app.get('/')
async def main(params: TestParams = Depends()):
    return params.dict()"><pre class="notranslate"><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">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">Depends</span>, <span class="pl-v">FastAPI</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">TestParams</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-k">class</span> <span class="pl-v">Config</span>:
        <span class="pl-s1">allow_population_by_field_name</span> <span class="pl-c1">=</span> <span class="pl-c1">True</span>

    <span class="pl-s1">param</span>: <span class="pl-s1">bool</span> <span class="pl-c1">=</span> <span class="pl-v">Field</span>(<span class="pl-c1">False</span>, <span class="pl-s1">alias</span><span class="pl-c1">=</span><span class="pl-s">'alias'</span>)


<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">'/'</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">main</span>(<span class="pl-s1">params</span>: <span class="pl-v">TestParams</span> <span class="pl-c1">=</span> <span class="pl-v">Depends</span>()):
    <span class="pl-k">return</span> <span class="pl-s1">params</span>.<span class="pl-en">dict</span>()</pre></div>
<h1 dir="auto">Expected results</h1>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$ curl &quot;http://127.0.0.1:8000/?param=true&quot;
{&quot;param&quot;:true}

$ curl &quot;http://127.0.0.1:8000/?alias=true&quot;
{&quot;param&quot;:true}"><pre class="notranslate">$ curl <span class="pl-s"><span class="pl-pds">"</span>http://127.0.0.1:8000/?param=true<span class="pl-pds">"</span></span>
{<span class="pl-s"><span class="pl-pds">"</span>param<span class="pl-pds">"</span></span>:true}

$ curl <span class="pl-s"><span class="pl-pds">"</span>http://127.0.0.1:8000/?alias=true<span class="pl-pds">"</span></span>
{<span class="pl-s"><span class="pl-pds">"</span>param<span class="pl-pds">"</span></span>:true}</pre></div>
<h1 dir="auto">Actual results</h1>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$ curl &quot;http://127.0.0.1:8000/?param=true&quot;
{&quot;param&quot;:false}

$ curl &quot;http://127.0.0.1:8000/?alias=true&quot;
{&quot;param&quot;:true}"><pre class="notranslate">$ curl <span class="pl-s"><span class="pl-pds">"</span>http://127.0.0.1:8000/?param=true<span class="pl-pds">"</span></span>
{<span class="pl-s"><span class="pl-pds">"</span>param<span class="pl-pds">"</span></span>:false}

$ curl <span class="pl-s"><span class="pl-pds">"</span>http://127.0.0.1:8000/?alias=true<span class="pl-pds">"</span></span>
{<span class="pl-s"><span class="pl-pds">"</span>param<span class="pl-pds">"</span></span>:true}</pre></div>
<h3 dir="auto">First check</h3>
<ul dir="auto">
<li>[ x] I added a very descriptive title to this issue.</li>
<li>[ x] I used the GitHub search to find a similar issue and didn't find it.</li>
<li>[ x] I searched the FastAPI documentation, with the integrated search.</li>
<li>[ x] I already searched in Google "How to X in FastAPI" and didn't find any information.</li>
<li>[ x] I already read and followed all the tutorial in the docs and didn't find an answer.</li>
<li>[ x] I already checked if it is not related to FastAPI but to <a href="https://github.com/samuelcolvin/pydantic">Pydantic</a>.</li>
<li>[ x] 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>[ x] I already checked if it is not related to FastAPI but to <a href="https://github.com/Redocly/redoc">ReDoc</a>.</li>
<li>[ x] After submitting this, I commit to:
<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>Or, 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">I'm sorry if this is a duplicate or I've missed how to do this. I have scoured the docs and github issues.</p>
<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="# URL request: localhost/api/weather/portland?state=OR&amp;country=US&amp;units=imperial

from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
import uvicorn

class Location(BaseModel):
    city: str
    country: Optional[str] = 'US'
    state: Optional[str] = None
    units: Optional[str] = 'metric'

app = FastAPI()

# What we have is:
@app.get('/api/weather/{city}')
async def weather(city: str,
        country: Optional[str] = None,
        state: Optional[str] = None,
        units: str = 'metric'):
    # work with city, country, etc.
    return Location(city=city, country=country, state=state, units=units)
    
# What I want:
# @app.get('/api/weather_binding/{city}')
# async def weather(loc: Location):
    # work with loc.city, loc.country, etc.
    # Where the binding inputs to the Pydantic model are basically:
    # data = {
    #       **request.query_params,
    #       **request.headers,
    #        **dict(await request.form()),  # only if the form dependencies are installed
    #        **request.path_params
    #    }
    # return loc

uvicorn.run(app)"><pre class="notranslate"><span class="pl-c"># URL request: localhost/api/weather/portland?state=OR&amp;country=US&amp;units=imperial</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-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">uvicorn</span>

<span class="pl-k">class</span> <span class="pl-v">Location</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">city</span>: <span class="pl-s1">str</span>
    <span class="pl-s1">country</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">str</span>] <span class="pl-c1">=</span> <span class="pl-s">'US'</span>
    <span class="pl-s1">state</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">str</span>] <span class="pl-c1">=</span> <span class="pl-c1">None</span>
    <span class="pl-s1">units</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">str</span>] <span class="pl-c1">=</span> <span class="pl-s">'metric'</span>

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

<span class="pl-c"># What we have is:</span>
<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">'/api/weather/{city}'</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">weather</span>(<span class="pl-s1">city</span>: <span class="pl-s1">str</span>,
        <span class="pl-s1">country</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">str</span>] <span class="pl-c1">=</span> <span class="pl-c1">None</span>,
        <span class="pl-s1">state</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">str</span>] <span class="pl-c1">=</span> <span class="pl-c1">None</span>,
        <span class="pl-s1">units</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-s">'metric'</span>):
    <span class="pl-c"># work with city, country, etc.</span>
    <span class="pl-k">return</span> <span class="pl-v">Location</span>(<span class="pl-s1">city</span><span class="pl-c1">=</span><span class="pl-s1">city</span>, <span class="pl-s1">country</span><span class="pl-c1">=</span><span class="pl-s1">country</span>, <span class="pl-s1">state</span><span class="pl-c1">=</span><span class="pl-s1">state</span>, <span class="pl-s1">units</span><span class="pl-c1">=</span><span class="pl-s1">units</span>)
    
<span class="pl-c"># What I want:</span>
<span class="pl-c"># @app.get('/api/weather_binding/{city}')</span>
<span class="pl-c"># async def weather(loc: Location):</span>
    <span class="pl-c"># work with loc.city, loc.country, etc.</span>
    <span class="pl-c"># Where the binding inputs to the Pydantic model are basically:</span>
    <span class="pl-c"># data = {</span>
    <span class="pl-c">#       **request.query_params,</span>
    <span class="pl-c">#       **request.headers,</span>
    <span class="pl-c">#        **dict(await request.form()),  # only if the form dependencies are installed</span>
    <span class="pl-c">#        **request.path_params</span>
    <span class="pl-c">#    }</span>
    <span class="pl-c"># return loc</span>

<span class="pl-s1">uvicorn</span>.<span class="pl-en">run</span>(<span class="pl-s1">app</span>)</pre></div>
<h3 dir="auto">Description</h3>
<ul dir="auto">
<li>Open the browser and call the endpoint <code class="notranslate">/api/weather/portland?state=OR&amp;country=US&amp;units=imperial</code>.</li>
<li>It returns a JSON:</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;city&quot;: &quot;portland&quot;,
  &quot;country&quot;: null,
  &quot;state&quot;: &quot;OR&quot;,
  &quot;units&quot;: &quot;metric&quot;
}"><pre class="notranslate"><code class="notranslate">{
  "city": "portland",
  "country": null,
  "state": "OR",
  "units": "metric"
}
</code></pre></div>
<ul dir="auto">
<li>I would like to use model binding just like we have for JSON POST of inbound Pydantic models but combining all these elements.</li>
</ul>
<h3 dir="auto">The solution you would like</h3>
<p dir="auto">Basically, I can do this already with this step:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="data = {
    **request.query_params,
    **request.headers,
    **dict(await request.form()),
    **request.path_params
}
loc = Location(**data)"><pre class="notranslate"><code class="notranslate">data = {
    **request.query_params,
    **request.headers,
    **dict(await request.form()),
    **request.path_params
}
loc = Location(**data)
</code></pre></div>
<p dir="auto">But that's a drag and having the framework do this, even with an opt in flag in the route (like bind_all=True) would be fantastic.</p>
<h3 dir="auto">Describe alternatives you've considered</h3>
<p dir="auto">I've consider just using the lines of code above more or less on method, but that doesn't seem to fit with the clean automatic nature of the platform.</p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS:  macOS</li>
<li>FastAPI Version: 0.61.1</li>
<li>Python version: 3.9</li>
</ul>
<h3 dir="auto">Additional context</h3>
<p dir="auto">None</p>