admin管理员组文章数量:1023744
I have a fastapi endpoint for which I am trying to set the example value
:
The fastapi doc seems to indicate that you can add examples to request parameters but I cannot figure out how to set the response example.
My code:
from typing import Annotated
import structlog
from fastapi import (
APIRouter, Body,
)
from openai import BaseModel
from pydantic import Field
logger = structlog.get_logger(__name__)
router = APIRouter(prefix="/ham")
class Ham(BaseModel):
color: str = Field(..., description="What color?")
BetterHam = Annotated[Ham, Body(examples=[{"color": "pink"}])]
@router.get("/")
async def get_ham() -> BetterHam:
return Ham(color="green")
This is setting an example in the openapi.json, but I think that this is at the wrong level. According to the swagger-ui doc, the example should be a sibling to schema
not a child. Doc
"/ham/": {
"get": {
"summary": "Get Ham",
"operationId": "get_ham",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Ham",
"examples": [
{
"color": "pink"
}
]
}
}
}
}
}
}
},
I have a fastapi endpoint for which I am trying to set the example value
:
The fastapi doc seems to indicate that you can add examples to request parameters but I cannot figure out how to set the response example.
My code:
from typing import Annotated
import structlog
from fastapi import (
APIRouter, Body,
)
from openai import BaseModel
from pydantic import Field
logger = structlog.get_logger(__name__)
router = APIRouter(prefix="/ham")
class Ham(BaseModel):
color: str = Field(..., description="What color?")
BetterHam = Annotated[Ham, Body(examples=[{"color": "pink"}])]
@router.get("/")
async def get_ham() -> BetterHam:
return Ham(color="green")
This is setting an example in the openapi.json, but I think that this is at the wrong level. According to the swagger-ui doc, the example should be a sibling to schema
not a child. Doc
"/ham/": {
"get": {
"summary": "Get Ham",
"operationId": "get_ham",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Ham",
"examples": [
{
"color": "pink"
}
]
}
}
}
}
}
}
},
Share
Improve this question
asked Nov 18, 2024 at 20:17
sixtyfootersdudesixtyfootersdude
27.3k45 gold badges154 silver badges223 bronze badges
2 Answers
Reset to default 0This works but is very verbose:
@router.get("/", response_model=Ham, responses={
200: {
"content": {
"application/json": {
"example": {"color": "pink"}
}
}
}
})
async def get_ham() -> Ham:
return Ham(color="green")
This is a pretty good solution:
class Ham(BaseModel):
color: str = Field(..., description="What color?")
model_config = {
"json_schema_extra": {
"example": {
"color": "Red"
}
}
}
I have a fastapi endpoint for which I am trying to set the example value
:
The fastapi doc seems to indicate that you can add examples to request parameters but I cannot figure out how to set the response example.
My code:
from typing import Annotated
import structlog
from fastapi import (
APIRouter, Body,
)
from openai import BaseModel
from pydantic import Field
logger = structlog.get_logger(__name__)
router = APIRouter(prefix="/ham")
class Ham(BaseModel):
color: str = Field(..., description="What color?")
BetterHam = Annotated[Ham, Body(examples=[{"color": "pink"}])]
@router.get("/")
async def get_ham() -> BetterHam:
return Ham(color="green")
This is setting an example in the openapi.json, but I think that this is at the wrong level. According to the swagger-ui doc, the example should be a sibling to schema
not a child. Doc
"/ham/": {
"get": {
"summary": "Get Ham",
"operationId": "get_ham",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Ham",
"examples": [
{
"color": "pink"
}
]
}
}
}
}
}
}
},
I have a fastapi endpoint for which I am trying to set the example value
:
The fastapi doc seems to indicate that you can add examples to request parameters but I cannot figure out how to set the response example.
My code:
from typing import Annotated
import structlog
from fastapi import (
APIRouter, Body,
)
from openai import BaseModel
from pydantic import Field
logger = structlog.get_logger(__name__)
router = APIRouter(prefix="/ham")
class Ham(BaseModel):
color: str = Field(..., description="What color?")
BetterHam = Annotated[Ham, Body(examples=[{"color": "pink"}])]
@router.get("/")
async def get_ham() -> BetterHam:
return Ham(color="green")
This is setting an example in the openapi.json, but I think that this is at the wrong level. According to the swagger-ui doc, the example should be a sibling to schema
not a child. Doc
"/ham/": {
"get": {
"summary": "Get Ham",
"operationId": "get_ham",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Ham",
"examples": [
{
"color": "pink"
}
]
}
}
}
}
}
}
},
Share
Improve this question
asked Nov 18, 2024 at 20:17
sixtyfootersdudesixtyfootersdude
27.3k45 gold badges154 silver badges223 bronze badges
2 Answers
Reset to default 0This works but is very verbose:
@router.get("/", response_model=Ham, responses={
200: {
"content": {
"application/json": {
"example": {"color": "pink"}
}
}
}
})
async def get_ham() -> Ham:
return Ham(color="green")
This is a pretty good solution:
class Ham(BaseModel):
color: str = Field(..., description="What color?")
model_config = {
"json_schema_extra": {
"example": {
"color": "Red"
}
}
}
本文标签: fastapiHow to set response body exampleStack Overflow
版权声明:本文标题:fastapi - How to set response body example - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745596665a2158211.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论