Path parameter became required if it defined via Pydantic model
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from pydantic import BaseModel, Field
from fastapi import Path, FastAPI, Depends
app = FastAPI()
class Foo(BaseModel):
bar: str = Field(Path('bar')) # default value is defined
@app.get('/foo') # always will return 422 because `bar` not represent in path
async def pathless_handler(params: Foo = Depends(Foo)):
pass
@app.get('/foo/{bar}')
async def path_aware_handler(params: Foo = Depends(Foo)):
pass
Description
If the path parameters are defined via Pydantic model, then the path parameters become always required.
curl -v localhost:8000/foo
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /foo HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.83.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 422 Unprocessable Entity
< date: Fri, 10 Jun 2022 11:56:49 GMT
< server: uvicorn
< content-length: 87
< content-type: application/json
<
* Connection #0 to host localhost left intact
{"detail":[{"loc":["path","bar"],"msg":"field required","type":"value_error.missing"}]}%
The root of evil is in this line (it makes a path parameter always required, even if a default value is specified 😢) https://github.com/tiangolo/fastapi/blob/1876ebc77949a9a254909ec61ea0c09365169ec2/fastapi/params.py#L85
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.78.0
Python Version
3.10.4
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
How to pass the path parameter to the Pydantic model?
Pydantic in FastAPI is used to define data model. You can do as follows: from typing import Optional from fastapi import FastAPI from...
Read more >Settings management - pydantic
If you create a model that inherits from BaseSettings , the model initialiser will attempt to determine the values of any fields not...
Read more >Flask and Pydantic - techtutorialsx
2 Defining a Pydantic class for query parameters ... that the usage of this library is not strictly necessary to combine Flask and...
Read more >Pydantic - The Blue Book
The primary means of defining objects in pydantic is via models (models are simply ... from the default value, and so a type...
Read more >FastAPI Python Tutorial - 6: Add users: Request body with ...
✓How to use Pydantic models for data validation? ... up the users' path /endpoint Part 4: Filter for specific users: Path parameter Part...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

As ad-hoc solution:
Ah, yeah @NickVeld , I was focusing on the original post. So, the thing is, I’m not sure I understand what would be the intention of the original question, what would be expected in that case. I’m not sure what would mean to have a default value for a path parameter. I think I’m missing the explanation of what was expected, and what was @gtors intending to achieve.
Now, if you have a specific use case that needs something related to this, please create a new issue, following the template, and please make sure you add what are you intending to solve and what behavior you would have expected.