<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="from typing import Optional

from fastapi import Depends, FastAPI
from pydantic import BaseModel

# Models as shown in https://fastapi.tiangolo.com/tutorial/extra-models/
class Command(BaseModel):
    name: str
    game: int = 0
    template: str
    cooldown: int


class UpdateCommand(BaseModel):
    name: str
    game: int = 0
    template: Optional[str]
    cooldown: Optional[int]


app = FastAPI()


async def get_cmd_from_db_dep(name: str, game: Optional[int] = 0) -&gt; Command:
    pass


async def get_cmd_from_db_by_update_cmd(update_cmd: UpdateCommand) -&gt; Command:
    return await get_cmd_from_db_dep(update_cmd.name, update_cmd.game)


# Expects query args name and game, perfect for a get call
@app.get(&quot;/command&quot;, response_model=Command)
async def get_command(cmd: Command = Depends(get_cmd_from_db_dep)):
    pass


# Expects query args name and game as well as a UpdateCommand in the body which will
# contain the name and game as well. Less desirable.
@app.post(&quot;/command&quot;, response_model=Command)
async def update_command(
    update_cmd: UpdateCommand, cmd: Command = Depends(get_cmd_from_db_dep)
):
    pass


# Expects a UpdateCommand in the body but requires an extra wrapper around
# `get_cmd_from_db_dep`
@app.post(&quot;/command_2&quot;, response_model=Command)
async def update_command_also(
    update_cmd: UpdateCommand, cmd2: Command = Depends(get_cmd_from_db_by_update_cmd)
):
    pass
"><pre class="notranslate"><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">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-k">from</span> <span class="pl-s1">pydantic</span> <span class="pl-k">import</span> <span class="pl-v">BaseModel</span>

<span class="pl-c"># Models as shown in https://fastapi.tiangolo.com/tutorial/extra-models/</span>
<span class="pl-k">class</span> <span class="pl-v">Command</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">name</span>: <span class="pl-s1">str</span>
    <span class="pl-s1">game</span>: <span class="pl-s1">int</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span>
    <span class="pl-s1">template</span>: <span class="pl-s1">str</span>
    <span class="pl-s1">cooldown</span>: <span class="pl-s1">int</span>


<span class="pl-k">class</span> <span class="pl-v">UpdateCommand</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">name</span>: <span class="pl-s1">str</span>
    <span class="pl-s1">game</span>: <span class="pl-s1">int</span> <span class="pl-c1">=</span> <span class="pl-c1">0</span>
    <span class="pl-s1">template</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">str</span>]
    <span class="pl-s1">cooldown</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">int</span>]


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


<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">get_cmd_from_db_dep</span>(<span class="pl-s1">name</span>: <span class="pl-s1">str</span>, <span class="pl-s1">game</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">int</span>] <span class="pl-c1">=</span> <span class="pl-c1">0</span>) <span class="pl-c1">-&gt;</span> <span class="pl-v">Command</span>:
    <span class="pl-k">pass</span>


<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">get_cmd_from_db_by_update_cmd</span>(<span class="pl-s1">update_cmd</span>: <span class="pl-v">UpdateCommand</span>) <span class="pl-c1">-&gt;</span> <span class="pl-v">Command</span>:
    <span class="pl-k">return</span> <span class="pl-k">await</span> <span class="pl-en">get_cmd_from_db_dep</span>(<span class="pl-s1">update_cmd</span>.<span class="pl-s1">name</span>, <span class="pl-s1">update_cmd</span>.<span class="pl-s1">game</span>)


<span class="pl-c"># Expects query args name and game, perfect for a get call</span>
<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">get</span>(<span class="pl-s">"/command"</span>, <span class="pl-s1">response_model</span><span class="pl-c1">=</span><span class="pl-v">Command</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">get_command</span>(<span class="pl-s1">cmd</span>: <span class="pl-v">Command</span> <span class="pl-c1">=</span> <span class="pl-v">Depends</span>(<span class="pl-s1">get_cmd_from_db_dep</span>)):
    <span class="pl-k">pass</span>


<span class="pl-c"># Expects query args name and game as well as a UpdateCommand in the body which will</span>
<span class="pl-c"># contain the name and game as well. Less desirable.</span>
<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">post</span>(<span class="pl-s">"/command"</span>, <span class="pl-s1">response_model</span><span class="pl-c1">=</span><span class="pl-v">Command</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">update_command</span>(
    <span class="pl-s1">update_cmd</span>: <span class="pl-v">UpdateCommand</span>, <span class="pl-s1">cmd</span>: <span class="pl-v">Command</span> <span class="pl-c1">=</span> <span class="pl-v">Depends</span>(<span class="pl-s1">get_cmd_from_db_dep</span>)
):
    <span class="pl-k">pass</span>


<span class="pl-c"># Expects a UpdateCommand in the body but requires an extra wrapper around</span>
<span class="pl-c"># `get_cmd_from_db_dep`</span>
<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">post</span>(<span class="pl-s">"/command_2"</span>, <span class="pl-s1">response_model</span><span class="pl-c1">=</span><span class="pl-v">Command</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">update_command_also</span>(
    <span class="pl-s1">update_cmd</span>: <span class="pl-v">UpdateCommand</span>, <span class="pl-s1">cmd2</span>: <span class="pl-v">Command</span> <span class="pl-c1">=</span> <span class="pl-v">Depends</span>(<span class="pl-s1">get_cmd_from_db_by_update_cmd</span>)
):
    <span class="pl-k">pass</span></pre></div>
<h3 dir="auto">Description</h3>

<p dir="auto">I am making a simple CRUD api, but I am hitting a weird pattern I feel like I am doing something wrong.</p>
<p dir="auto">The above example will make a get api call that wants <code class="notranslate">name</code> and <code class="notranslate">game</code> ( My two pieces of my primary key essentially) which is perfect.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/1725647/99155142-f26c9500-267a-11eb-9a05-51a0dab1d93d.png"><img src="https://user-images.githubusercontent.com/1725647/99155142-f26c9500-267a-11eb-9a05-51a0dab1d93d.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">However, the update post wants both the name and game defined as query args AND the body to contain a name and game as well as the fields that are changing.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/1725647/99155149-02847480-267b-11eb-8486-4d3fb3c7ec24.png"><img src="https://user-images.githubusercontent.com/1725647/99155149-02847480-267b-11eb-8486-4d3fb3c7ec24.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">The only way I had found to get around this was defining "wrapper" dependencies such as <code class="notranslate">get_cmd_from_db_by_update_cmd</code>, which looks how I would like:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/1725647/99155165-19c36200-267b-11eb-802b-a930f6d00795.png"><img src="https://user-images.githubusercontent.com/1725647/99155165-19c36200-267b-11eb-802b-a930f6d00795.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">Is there a different way to define the dependency or the arguments to make it so that if I get a model that contains the arguments it will pass them to the dependency instead of expecting them as separate query args?</p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS: [e.g. Linux / Windows / macOS]: Windows 10</li>
<li>FastAPI Version [e.g. 0.3.0]: 0.61.1</li>
<li>Python version: 3.7.9</li>
</ul>
<h3 dir="auto">Additional context</h3>
<p dir="auto">Obligatory, my model is actually a little more complicated than that, but the ideas conveyed are what I am after. Also, I LOVE the project, I just want to make sure I am doing things right with it!</p>
<p dir="auto">Hi there!<br>
I'm trying to send a post with a JSON that might contain a <code class="notranslate">\n</code> character in one of its fields. I tried what is explained here <a href="https://pydantic-docs.helpmanual.io/usage/exporting_models/" rel="nofollow">https://pydantic-docs.helpmanual.io/usage/exporting_models/</a> with no success. I can run the example there with no problems but when used in the app it crashes when it tries to parse the input json.</p>
<p dir="auto">The sample code:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import json
from fastapi import FastAPI
from pydantic import BaseModel

def new_json_loads(*args, **kwargs):
    kwargs.update(dict(strict=False))
    return json.loads(*args, **kwargs)

class Request(BaseModel):
    name = &quot;John Doe&quot;
    class Config:
        json_loads = new_json_loads

r = Request.parse_raw('{\n  &quot;name&quot;: &quot;john \n doe&quot;\n}')
# It runs!
print(r)

app = FastAPI()

# It does not run here when a request arrives
@app.post(&quot;/test&quot;)
def read_root(request: Request):
    return request"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">json</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">def</span> <span class="pl-en">new_json_loads</span>(<span class="pl-c1">*</span><span class="pl-s1">args</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>):
    <span class="pl-s1">kwargs</span>.<span class="pl-en">update</span>(<span class="pl-en">dict</span>(<span class="pl-s1">strict</span><span class="pl-c1">=</span><span class="pl-c1">False</span>))
    <span class="pl-k">return</span> <span class="pl-s1">json</span>.<span class="pl-en">loads</span>(<span class="pl-c1">*</span><span class="pl-s1">args</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)

<span class="pl-k">class</span> <span class="pl-v">Request</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">name</span> <span class="pl-c1">=</span> <span class="pl-s">"John Doe"</span>
    <span class="pl-k">class</span> <span class="pl-v">Config</span>:
        <span class="pl-s1">json_loads</span> <span class="pl-c1">=</span> <span class="pl-s1">new_json_loads</span>

<span class="pl-s1">r</span> <span class="pl-c1">=</span> <span class="pl-v">Request</span>.<span class="pl-en">parse_raw</span>(<span class="pl-s">'{<span class="pl-cce">\n</span>  "name": "john <span class="pl-cce">\n</span> doe"<span class="pl-cce">\n</span>}'</span>)
<span class="pl-c"># It runs!</span>
<span class="pl-en">print</span>(<span class="pl-s1">r</span>)

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

<span class="pl-c"># It does not run here when a request arrives</span>
<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">post</span>(<span class="pl-s">"/test"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">read_root</span>(<span class="pl-s1">request</span>: <span class="pl-v">Request</span>):
    <span class="pl-k">return</span> <span class="pl-s1">request</span></pre></div>
<h3 dir="auto">Description</h3>
<ul dir="auto">
<li>Open the browser and call the endpoint <a href="http://0.0.0.0:8000/docs#/default/read_root_test_post" rel="nofollow">http://0.0.0.0:8000/docs#/default/read_root_test_post</a>.</li>
<li><code class="notranslate">Try it out</code> with: <code class="notranslate">{ "name": "john  doe" }</code>. Don't forget to enter a new line after the word <code class="notranslate">John</code></li>
<li>The 422 error returns</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;detail&quot;: [
    {
      &quot;loc&quot;: [
        &quot;body&quot;,
        18
      ],
      &quot;msg&quot;: &quot;Invalid control character at: line 2 column 17 (char 18)&quot;,
      &quot;type&quot;: &quot;value_error.jsondecode&quot;,
      &quot;ctx&quot;: {
        &quot;msg&quot;: &quot;Invalid control character at&quot;,
        &quot;doc&quot;: &quot;{\n  \&quot;name\&quot;: \&quot;john \ndoe\&quot;\n}&quot;,
        &quot;pos&quot;: 18,
        &quot;lineno&quot;: 2,
        &quot;colno&quot;: 17
      }
    }
  ]
}"><pre class="notranslate"><code class="notranslate">{
  "detail": [
    {
      "loc": [
        "body",
        18
      ],
      "msg": "Invalid control character at: line 2 column 17 (char 18)",
      "type": "value_error.jsondecode",
      "ctx": {
        "msg": "Invalid control character at",
        "doc": "{\n  \"name\": \"john \ndoe\"\n}",
        "pos": 18,
        "lineno": 2,
        "colno": 17
      }
    }
  ]
}
</code></pre></div>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS: [e.g. Linux / Windows / macOS]: Linux 20.04</li>
<li>FastAPI Version [e.g. 0.3.0]: 0.61.1</li>
<li>Python version: 3.7.8</li>
</ul>
<p dir="auto">Thanks for your help!</p>