<p dir="auto"><strong>Description</strong></p>
<p dir="auto">I'm currently migrating an API to FastAPI and I'm trying to prevent too many changes regarding how you call the API. To filter on the old API you'd pass a json serialized complex dict(with nested dicts and lists in it) as a query parameter and this would then be deserialized and parsed to create the filter. I can make this work in FastAPI as well, but this means I have to call <code class="notranslate">json.loads(filter_)</code> in every route that uses it, and it kind of feels like there is a way to declare a custom type that does this for me.</p>
<p dir="auto">On a similar note: the old API had comma separated lists. For example: <code class="notranslate">?columns=id,name,email</code>. If I define something as a list in FastAPI right now I have to pass it this: <code class="notranslate">?columns=id&amp;columns=name&amp;columns=email</code> which seems really verbose for my usecase(I can see it being useful if your data would potentially contain a "," though), so I'd like to write a custom parser for that as well(although iirc OpenAPI has support for this way of defining lists already somewhere).</p>
<p dir="auto">Could you help point me in the right direction on how to solve these issues?</p>
<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>
</ul>
<h3 dir="auto">Commit to Help</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I commit to help with one of those options <g-emoji class="g-emoji" alias="point_up_2" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f446.png">👆</g-emoji></li>
</ul>
<h3 dir="auto">Example Code</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import uvicorn
from fastapi import FastAPI, Depends
from pydantic import BaseModel, StrictInt

app = FastAPI()


class Model(BaseModel):
    foo: StrictInt
    bar: StrictInt


def dependency(request: Model):
    return request.foo + request.bar


@app.post(&quot;/&quot;)
def foo(request: Model, processed_data: int = Depends(dependency)):
    return &quot;hello&quot;


uvicorn.run(app, port=8000)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">uvicorn</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-v">Depends</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">StrictInt</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">Model</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">foo</span>: <span class="pl-v">StrictInt</span>
    <span class="pl-s1">bar</span>: <span class="pl-v">StrictInt</span>


<span class="pl-k">def</span> <span class="pl-en">dependency</span>(<span class="pl-s1">request</span>: <span class="pl-v">Model</span>):
    <span class="pl-k">return</span> <span class="pl-s1">request</span>.<span class="pl-s1">foo</span> <span class="pl-c1">+</span> <span class="pl-s1">request</span>.<span class="pl-s1">bar</span>


<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">post</span>(<span class="pl-s">"/"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">foo</span>(<span class="pl-s1">request</span>: <span class="pl-v">Model</span>, <span class="pl-s1">processed_data</span>: <span class="pl-s1">int</span> <span class="pl-c1">=</span> <span class="pl-v">Depends</span>(<span class="pl-s1">dependency</span>)):
    <span class="pl-k">return</span> <span class="pl-s">"hello"</span>


<span class="pl-s1">uvicorn</span>.<span class="pl-en">run</span>(<span class="pl-s1">app</span>, <span class="pl-s1">port</span><span class="pl-c1">=</span><span class="pl-c1">8000</span>)</pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">I'm trying to make some request data postprocessing using FastAPI dependency.<br>
It works fine, but seems like data from request is dumped into Pydantic model multiple times. It leads to some overhead and excessive validation errors if any.</p>
<p dir="auto">For example, following request:<br>
<code class="notranslate">$ curl --location --request POST 'http://localhost:8000' --header 'Content-Type: application/json' --data-raw '{"bar": 2}'</code><br>
Will produce following error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
    &quot;detail&quot;: [
        {
            &quot;loc&quot;: [
                &quot;body&quot;,
                &quot;foo&quot;
            ],
            &quot;msg&quot;: &quot;field required&quot;,
            &quot;type&quot;: &quot;value_error.missing&quot;
        },
        {
            &quot;loc&quot;: [
                &quot;body&quot;,
                &quot;foo&quot;
            ],
            &quot;msg&quot;: &quot;field required&quot;,
            &quot;type&quot;: &quot;value_error.missing&quot;
        }
    ]
}"><pre class="notranslate"><code class="notranslate">{
    "detail": [
        {
            "loc": [
                "body",
                "foo"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        },
        {
            "loc": [
                "body",
                "foo"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        }
    ]
}
</code></pre></div>
<p dir="auto">which looks weird.<br>
Is there a correct way of doing this stuff using Pydantic models in methods declaration?</p>
<h3 dir="auto">Operating System</h3>
<p dir="auto">Linux</p>
<h3 dir="auto">Operating System Details</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">FastAPI Version</h3>
<p dir="auto">0.68.1</p>
<h3 dir="auto">Python Version</h3>
<p dir="auto">3.9.5</p>
<h3 dir="auto">Additional Context</h3>
<p dir="auto"><em>No response</em></p>