简体   繁体   English

如何使用 FastAPI 指示响应正文是 Swagger UI 文档中的列表?

[英]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.

相关问题 在 Python 中的 FastAPI Swagger UI 文档网页中添加自定义 javascript - Add a custom javascript to the FastAPI Swagger UI docs webpage in Python 如何使用 FastAPI 将图像添加到 Swagger UI 自动文档? - How to add image to Swagger UI autodocs using FastAPI? 如何使用 FastAPI 通过 Swagger UI 发送授权标头? - How to send Authorization Header through Swagger UI using FastAPI? 如何使用 FastAPI 在 Swagger UI 中显示来自 HTMLResponse 的实际 HTML 页面? - How to display the actual HTML page from HTMLResponse in Swagger UI using FastAPI? Swagger UI 在响应正文中返回“无内容”,响应代码为 0 - Swagger UI returns "no content" in the Response Body, and Response Code 0 如何为 swagger-ui 指示对象所需的属性 - How to indicate required properties for an object for swagger-ui 如何通过 Swagger UI 将 FileUpload 响应体视为上传按钮 - How to view a FileUpload response body as an upload button when seen through Swagger UI swagger-ui-express未在浏览器中呈现,但在Postma中具有响应正文 - swagger-ui-express is not rendering in browser but has response body in Postma 如何在swagger-ui生成的文档中显示@PostMapping参数? - How to show @PostMapping parameters in swagger-ui generated docs? 如何使用Springdoc删除或隐藏Swagger UI中的服务器列表? - How to remove or hide the server list in Swagger UI using Springdoc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM