Search code examples
swagger-uifastapiopenapi

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.

My 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:

enter image description here

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?


Solution

  • You can indicate that you're returning a List by wrapping the response_model you've defined in List[<model>].

    So in your case it'd be:

    @app.get('/foo', response_model=List[versions_info])