-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
Union doesn't document properly on swagger #1083
Comments
What do you want to display in |
FastAPI does not generate body examples, that's just something SwaggerUI does, and Swagger apparently doesn't create an example value for type unions (probably because there's no single valid schema anymore). The union is technically properly documented (the schema tab is still correct, and that's the one that actually matters). I don't know if it's currently possible to specify a custom example structure using FastAPI, though. |
I'm not familiar with |
Swagger is just the documentation renderer FastAPI exposes at Normally, if Swagger can't figure out an example for your schema, your OpenAPI schema can specify one, and Pydantic supports it for individual BaseModel classes, but unions are trickier because they're not actually classes... |
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/ |
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
class Human(BaseModel):
age: int
name: str
class Man(Human):
class Woman(Human):
HumanCreateRequest = Union[Man, Woman]
# POST /human I want to set default schema Woman class in swagger. but swagger example is empty. |
@mcauto Please see comments above. It's not supported by Swagger UI so you have to add a custom example. Also -
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. |
Thank you for your attension. 👍👍 I solved it! class Human(BaseModel):
age: int
name: str
class Man(Human):
class Woman(Human):
class HumanCreateRequest(BaseModel):
target: Union[Woman, Man]
class Config:
schema_extra = {
"example": {
"target": Man(age=29, name="deo").dict()
}
}
app = FastAPI()
@app.post("/human")
def add_human(create_request: HumanCreateRequest):
pass
# POST /human |
Hi! @phy25 Have you ever been like this? from typing import Union
from fastapi.applications import FastAPI
from pydantic import BaseModel
class Human(BaseModel):
age: int
name: str
class Man(Human):
""" man """
something: str
class Woman(Human):
""" woman """
class ManCreateRequest(Man):
class Config:
schema_extra = {
"example": {
"target": Man(age=29, name="deo", something="something").dict()
}
}
class HumanCreateRequest(BaseModel):
target: Union[Woman, Man]
class Config:
schema_extra = Man.Config.schema_extra # default Schema Man
app = FastAPI()
@app.post("/human")
def add_human(create_request: HumanCreateRequest) -> None:
instance_type = create_request.target # always Woman
return Pydantic will always return the first one... I want to get real type of |
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. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Describe the bug
When using
Union
onresponse_model
, it doesn't doucment properly on swagger.To Reproduce
Expected behavior
Example value tab throw
no example available
.But it can show on
Schema
tabAdditional 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)The text was updated successfully, but these errors were encountered: