<p dir="auto"><strong>Description</strong></p>
<p dir="auto">Let's assume I have two routes:</p>
<ul dir="auto">
<li><code class="notranslate">/notes</code></li>
<li><code class="notranslate">/users/{user_id}/notes</code></li>
</ul>
<p dir="auto">Both will return the same format of the response but the route prefixed by <code class="notranslate">/users/{user_id}</code> will be additionally filtering notes by specific <code class="notranslate">user_id</code>.</p>
<p dir="auto">I would like to not duplicate the logic unnecessarily and have something like function below to parse parameters:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="async def params(user_id: Optional[str] = Path(None)) -&gt; Optional[str]:
    return user_id

@api.get(&quot;/notes&quot;)
async def root_get(user_id: Optional[str] = Depends(params)):
    pass

@api.get(&quot;/users/{user_id}/notes&quot;)
async def user_get(user_id: Optional[str] = Depends(params)):
    pass"><pre class="notranslate"><span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">params</span>(<span class="pl-s1">user_id</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">str</span>] <span class="pl-c1">=</span> <span class="pl-v">Path</span>(<span class="pl-c1">None</span>)) <span class="pl-c1">-&gt;</span> <span class="pl-v">Optional</span>[<span class="pl-s1">str</span>]:
    <span class="pl-k">return</span> <span class="pl-s1">user_id</span>

<span class="pl-en">@<span class="pl-s1">api</span>.<span class="pl-en">get</span>(<span class="pl-s">"/notes"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">root_get</span>(<span class="pl-s1">user_id</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">str</span>] <span class="pl-c1">=</span> <span class="pl-v">Depends</span>(<span class="pl-s1">params</span>)):
    <span class="pl-k">pass</span>

<span class="pl-en">@<span class="pl-s1">api</span>.<span class="pl-en">get</span>(<span class="pl-s">"/users/{user_id}/notes"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">user_get</span>(<span class="pl-s1">user_id</span>: <span class="pl-v">Optional</span>[<span class="pl-s1">str</span>] <span class="pl-c1">=</span> <span class="pl-v">Depends</span>(<span class="pl-s1">params</span>)):
    <span class="pl-k">pass</span></pre></div>
<p dir="auto">However <code class="notranslate">Path</code> only accepts defined <code class="notranslate">str</code> and doesn't allow <code class="notranslate">None</code>.</p>
<p dir="auto">Is there any way to make path parameters optional?</p>
<p dir="auto">I would like to exclude the ValidationError model from the OpenAPI Schema because I have completely overwritten it like this</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import ORJSONResponse
from starlette.exceptions import HTTPException

app = FastAPI(default_response_class=ORJSONResponse)

def exception_handler(req: Request, err: Exception) -&gt; ORJSONResponse:
    # ...

app.add_exception_handler(HTTPException, exception_handler)
app.add_exception_handler(RequestValidationError, exception_handler)
app.add_exception_handler(Exception, exception_handler)

# ..."><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-v">Request</span>
<span class="pl-k">from</span> <span class="pl-s1">fastapi</span>.<span class="pl-s1">exceptions</span> <span class="pl-k">import</span> <span class="pl-v">RequestValidationError</span>
<span class="pl-k">from</span> <span class="pl-s1">fastapi</span>.<span class="pl-s1">responses</span> <span class="pl-k">import</span> <span class="pl-v">ORJSONResponse</span>
<span class="pl-k">from</span> <span class="pl-s1">starlette</span>.<span class="pl-s1">exceptions</span> <span class="pl-k">import</span> <span class="pl-v">HTTPException</span>

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

<span class="pl-k">def</span> <span class="pl-en">exception_handler</span>(<span class="pl-s1">req</span>: <span class="pl-v">Request</span>, <span class="pl-s1">err</span>: <span class="pl-v">Exception</span>) <span class="pl-c1">-&gt;</span> <span class="pl-v">ORJSONResponse</span>:
    <span class="pl-c"># ...</span>

<span class="pl-s1">app</span>.<span class="pl-en">add_exception_handler</span>(<span class="pl-v">HTTPException</span>, <span class="pl-s1">exception_handler</span>)
<span class="pl-s1">app</span>.<span class="pl-en">add_exception_handler</span>(<span class="pl-v">RequestValidationError</span>, <span class="pl-s1">exception_handler</span>)
<span class="pl-s1">app</span>.<span class="pl-en">add_exception_handler</span>(<span class="pl-v">Exception</span>, <span class="pl-s1">exception_handler</span>)

<span class="pl-c"># ...</span></pre></div>
<p dir="auto">and I can't find any current training without directly updating the JSON schema to do so.</p>
<p dir="auto">Is there any other way to exclude it?</p>