[BUG] Empty validation error when request parameters are of Union type
See original GitHub issueDescribe the bug
The Union
type works as expected when response model is defined as such according to docs: https://fastapi.tiangolo.com/tutorial/extra-models/#union-or-anyof However when parameters (body payload) are defined as Union
the code runs fine until executing method, and docs are picking up the types and generating schema correctly but the empty ValidationRequestError
is thrown when route method is called even if there are all parameters sent are valid.
To Reproduce
Sample code to reproduce:
class SimpleData(BaseModel):
foo: Optional[str] = None
class ExtendedData(SimpleData):
bar: str # Note that this is required
PostData = Union[ExtendedData, SimpleData]
@router.post("/test")
async def post(data: PostData):
return "OK"
Then the POST /test
route is called with a body payload:
{
"foo": "test1",
"bar": "test2"
}
As a result the empty ValidationRequestError
is thrown with value_error.missing
message but no actual field assigned to it.
Expected behavior
Parameters from the request are resolved and parsed against the types inside Union
.
Environment:
- FastAPI Version:
0.29.0
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (6 by maintainers)
Top GitHub Comments
This seems to be fixed, so closing to clean up backlog.
Interesting! Yup, can confirm that
Does return successfully.