This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Path parameter became required if it defined via Pydantic model #5019
Comments
As ad-hoc solution: from typing import Any
from fastapi.params import Path as OrigPath, Undefined
class Path(OrigPath):
def __init__(self, default: Any = Undefined, *args, **kwargs):
super().__init__(*args, **kwargs)
if default is not Undefined:
self.default = default |
I also have found that the following blocks setting of Line 85 in 1876ebc
If change it to I was wondering does it really have not been found before. But I have found this issue in the issue list. Other related: |
@gtors @triangolo Now there is a following situation:
My suggestion:
I believe in both cases it is good to label the issue as a enchantment or a bug and not a question. Because nothing is asked but not intuitive behavior is shown. I can create a pull request for both solutions. |
As for the ad-hoc solution, in order to be consistent the code must redefine
It is cleaner to place the whole snippet as-is into a separate module; and then import it from there. |
Defining path parameters (or other parameters that don't come from the body) using Pydantic models is not really supported. There will be a way to do that in a future release, but not yet. So, for now, your have to take that path parameter out of the Pydantic model. |
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
@tiangolo , it can be not implemented but the behavior stays confusing anyway. I proposed two solutions how to deal with it but you commented none of them. |
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. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
If the path parameters are defined via Pydantic model, then the path parameters become always required.
The root of evil is in this line (it makes a path parameter always required, even if a default value is specified 😢)
fastapi/fastapi/params.py
Line 85 in 1876ebc
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.78.0
Python Version
3.10.4
Additional Context
No response
The text was updated successfully, but these errors were encountered: