Replies: 9 comments 5 replies
-
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Blog(BaseModel):
title: str
body: str
published: Optional[bool] # published: bool | None, should just do the same thing
@app.post("/test")
async def create_blog(request: Blog):
return request This code too. Optional is fail and the published field is required in Swagger UI docs. Or redocs Am i miss something? |
Beta Was this translation helpful? Give feedback.
-
@jvdavim , did you try with no
|
Beta Was this translation helpful? Give feedback.
-
After some try i can make it optional. It is happen if you assign None to field in Pydantic model ...
class Blog(BaseModel):
title: str
body: str
published: Optional[bool] = None # Or do with this, published: bool | None = None
... The key is when you assign None to Pydantic model to make it optional in Swagger or redocs class Foo(BaseModel):
a: Optional[str] # Required
b: str | None # Required
c: Optional[str] | None # Required
d: Optional[str] = None # Optional
e: str | None = None # Optional
f: str = None # Optional |
Beta Was this translation helpful? Give feedback.
-
yes, I agree this is an issue! |
Beta Was this translation helpful? Give feedback.
-
I have found that it's happening only with pydantic v2. But it seems to be a problema with SwaggerUI, not FastAPI. I'm gonna try to find some issue on their repository or open a new one. For some reason, the upload file button is not being rendered beacause SwaggerUI is not processing the |
Beta Was this translation helpful? Give feedback.
-
I have just stumbled on the same issue using fastapi 0.109.2 and pydantic 2.6.1 |
Beta Was this translation helpful? Give feedback.
-
Use @app.post("/example")
def read_item(upload_file: Annotated[UploadFile, File(description="Some description")] = None):
return Works for me |
Beta Was this translation helpful? Give feedback.
-
Any update? |
Beta Was this translation helpful? Give feedback.
-
Met same problem, any update? |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
For some reason, when using Python >= 3.10 optional type hints (SomeType | None
) in endpoints methods parameters, the upload button is not rendered in the Swagger UI.Edit: For some reason, when making an
UploadFile
parameter optional, the upload button is not rendered in the Swagger UI. The problem is just the same using the oldtyping.Optional
notation orUploadFile | None
.Update: It's happening only with pydantic v2. Downgrading to pydantic v1 seems to fix the bug.
An example of working swagger UI (but wrong typing) upload button:
An example of broken swagger UI (but right typing) upload button:
Inspecting the generated openapi.json I realized that there are some differences on using the optional (
SomeType | None
) type hint, as seem on the screenshot below:I'm not sure if this change is not expected, or if it's a Swagger UI related problem. I tried to find it in Swagger UI repository, but I was not successful. I'm not so experienced with openapi docs, so maybe I'm missing something. I appreciate your help.
Operating System
Linux, macOS
Operating System Details
No response
FastAPI Version
0.104.1
Pydantic Version
2.5.2
Python Version
3.11.6
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions