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 sort methods in FastAPI Swagger API?

How can I set a sort order for the API methods in the FastAPI Swagger autodocs?

This answer shows how to do it in Java. How can I do it in Python? Is there a pure Python solution?

For example, I would like all my methods grouped by type (GET, POST, PUT, DELETE). The blue GET methods should be at the top:

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

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def list_all_components():
    pass

@app.get("/{component_id}")
def get_component(component_id: int):
    pass

@app.post("/")
def create_component():
    pass

@app.put("/{component_id}")
def update_component(component_id: int):
    pass

@app.delete("/{component_id}")
def delete_component(component_id: int):
    pass

enter image description here

>Solution :

You can configure Swagger UI parameters through the FastAPI constructor.

app = FastAPI(swagger_ui_parameters={"operationsSorter": "method"})

The full list of parameters can be found in the swagger documentation.

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