Replies: 1 comment 1 reply
-
You could solve your problem by using from typing import Annotated, Optional
import fastapi
from pydantic import BaseModel, Field
class Param(BaseModel):
param: str = Field(default=None)
PARAM = fastapi.Body(
description="param",
embed=True,
)
app = fastapi.FastAPI()
@app.post("/one")
async def post_one(one: Annotated[Optional[Param], PARAM] = None):
return one
@app.post("/two")
async def post_two(two: Annotated[Optional[Param], PARAM] = None):
return two NOTE: you can't have a default value be set in the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
I'm trying to upgrade from 0.99.1 to 0.111.0 which involves the pydantic v2 upgrade. This specific case seems to be breaking in my tests when I try to upgrade, I'm not positive it's related to pydantic, but the problem doesn't seem to be there if I'm not using pydantic request models.
This is correct in Swagger docs (note that if you have pydantic v1 installed, the docs are wrong as well):
![image](https://cdn.statically.io/img/private-user-images.githubusercontent.com/2197329/336629536-a50f52b5-8318-49b2-a570-42a3055d662b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA0NTA3NTYsIm5iZiI6MTcyMDQ1MDQ1NiwicGF0aCI6Ii8yMTk3MzI5LzMzNjYyOTUzNi1hNTBmNTJiNS04MzE4LTQ5YjItYTU3MC00MmEzMDU1ZDY2MmIucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDcwOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA3MDhUMTQ1NDE2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9N2Q4ZWMyMzdhYzJjOWE3Y2FjNmQ3ZWFhNzg0NmNhNDYyYTU0ZGViY2VjOWFiY2JlYTc3MTUyYTkzZTcwZjM0NSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.KzzJNXIZwgf04-A80rm5DUG3fvayVubPq28z6pShclY)
But a curl changes /two to require "one" instead "two":
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.111.0
Pydantic Version
2.7.3
Python Version
3.11.9
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions