[BUG] Union doesn't document properly on swagger
See original GitHub issueDescribe the bug
When using Union
on response_model
, it doesn’t doucment properly on swagger.
To Reproduce
- Create two models
from pydantic import BaseModel
class VerifyTokenResponseSchema(BaseModel):
status: bool
class ExceptionResponseSchema(BaseModel):
error: str
- Add it on response_model
from typing import Union
from fastapi import APIRouter
oauth_router = APIRouter()
@oauth_router.post('/verify', response_model=Union[VerifyTokenResponseSchema, ExceptionResponseSchema])
async def verify_token(token: str):
status = await VerifyTokenUsecase().execute(token=token)
return status.dict()
- Check it on /docs
Expected behavior
Example value tab throw no example available
.
But it can show on Schema
tab
Additional context
I want to check both response schema on Example value tab.
Is there anything wrong with my code?
(I know that I can use responses
but using Union is described on document(https://fastapi.tiangolo.com/tutorial/extra-models/#union-or-anyof)
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Unable to get Swagger UI working with Spring boot
Your problem lies in your SwaggerConfiguration file. You need to take out @EnableWebMvc , because this causes the default Spring Boot view ...
Read more >Best Practices in API Design - Swagger
The benefits of a well-designed API include: improved developer experience, faster documentation, and higher adoption for your API .
Read more >Swagger API: Discovery of API data and security flaws
We will present how we discovered those APIs, check for misconfigurations, present statistics, and give recommendations on how to properly ...
Read more >Fix Swagger Validator errors in Power Platform connectors
The Swagger Validator tool validates the connector files you submit in the GitHub open-source repository and the ISV portal.
Read more >latest PDF - Flask-RESTX Documentation
It provides a coherent collection of decorators and tools to describe your API and expose its documentation properly (using. Swagger). Flask- ...
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 FreeTop 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
Top GitHub Comments
Thanks for the help here @Dustyposa and @sm-Fifteen ! 👏 🍰
Yep, indeed that’s not a bug in FastAPI, but a feature request for Swagger UI.
Nevertheless, you can add a custom example that is shown in Swagger UI, check the docs: https://fastapi.tiangolo.com/tutorial/schema-extra-example/
@mcauto Please see comments above. It’s not supported by Swagger UI so you have to add a custom example. Also -
Please create an issue at https://github.com/swagger-api/swagger-ui if you want this behavior to be improved.