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] Union doesn't document properly on swagger

See original GitHub issue

Describe the bug

When using Union on response_model, it doesn’t doucment properly on swagger.

To Reproduce

  1. Create two models
from pydantic import BaseModel

class VerifyTokenResponseSchema(BaseModel):
    status: bool

class ExceptionResponseSchema(BaseModel):
    error: str
  1. 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()
  1. Check it on /docs

Expected behavior

스크린샷 2020-03-05 오후 7 59 47

Example value tab throw no example available.

스크린샷 2020-03-05 오후 7 59 57

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

github_iconTop GitHub Comments

2reactions
tiangolocommented, Apr 13, 2020

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/

1reaction
phy25commented, May 17, 2020

@mcauto Please see comments above. It’s not supported by Swagger UI so you have to add a custom example. Also -

What do you want to display in example value like Union type?

from fastapi import FastAPI, Body
from typing import Union
from pydantic import BaseModel


class User(BaseModel):
    name: str


class Item(BaseModel):
    size: int
    price: float


app = FastAPI()

process_things_body_example = {"name": "1", "size": 2, "price": 3} # whatever makes sense to you

@app.post("/multi/")
def process_things(body: Union[User, Item] = Body(..., example=process_things_body_example)):
    return body

Please create an issue at https://github.com/swagger-api/swagger-ui if you want this behavior to be improved.

Read more comments on GitHub >

github_iconTop 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 >

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