question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Empty validation error when request parameters are of Union type

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
LKaycommented, Nov 27, 2019

This seems to be fixed, so closing to clean up backlog.

1reaction
nckswtcommented, Jun 28, 2019

Interesting! Yup, can confirm that

@app.post("/test")
def post(data: PostData = Body(...)):
    return "OK"

Does return successfully.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does typescript resolve a union type to possible empty ...
If I use "&&" operator to check if the object is not null, the whole expression is evaluated to another union type, which...
Read more >
Error handling - Apollo GraphQL Docs
A client sent the hash of a query string to execute via automatic persisted queries, but the server has disabled APQ. OPERATION_RESOLUTION_FAILURE. The...
Read more >
Take control of unexpected data at runtime with TypeScript
In this article, we'll explore how to use TypeScript type ... ValidationError: Expected string, but was undefined at new ValidationError (.
Read more >
TypeScript errors and how to fix them
error TS1337 : An index signature parameter type cannot be a union type. Consider using a mapped object type instead. Broken Code ❌....
Read more >
Build a simple object validation utility with TypeScript
The problem is, just because you're using TypeScript on the frontend and the backend (or syncing types between C# and TypeScript with a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found