Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading