[英]How to indicate that the response body is a List in Swagger UI docs using FastAPI?
I describe the structure of the outgoing JSON in the model class, however I cannot make the output as a list.我在 model class 中描述了传出 JSON 的结构,但是我无法将 Z78E6221F6393D11CE5DZ8 设为 a1DB 列表。
My model class:我的 model class:
class versions_info(BaseModel):
""" List of versions """
version : str = Field(..., title="Version",example="2.1.1")
url : str = Field(..., title="Url",example="https://ocpi.wedwe.ww/ocpi/2.1.1/")
And in the documentation I see:在我看到的文档中:
However, I need it to be displayed as:但是,我需要将其显示为:
[
{
"version": "2.1.1",
"url": "https://www.server.com/ocpi/2.1.1/"
},
{
"version": "2.2",
"url": "https://www.server.com/ocpi/2.2/"
}
]
What am I doing wrong?我究竟做错了什么?
You can indicate that you're returning a List by wrapping the response_model
you've defined in List[<model>]
.您可以通过包装您在
List[<model>]
中定义的response_model
来表明您正在返回一个 List。
So in your case it'd be:所以在你的情况下是:
@app.get('/foo', response_model=List[versions_info])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.