Search code examples
pythonswaggerswagger-uifastapiopenapi

How to create a custom sort order for the API methods in FastAPI Swagger autodocs?


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

This question shows how to do it in Java. My previous question asked how to sort by "Method", which is a supported sorting method. I would really like to take this a step further, so that I can determine which order the methods appear. Right now DELETE appears at the top, but I want API methods to be in the order: GET, POST, PUT, DELETE.

I know it is possible to implement a custom sort in JavaScript and give that function to operationsSorter, but you can't include it from the swagger_ui_parameters property that is available in the Python bindings. Is there some way to accomplish this in Python?

from fastapi import FastAPI

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

@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 use tags to group your endpoints. To do that, pass the parameter tags with a list of str (commonly just one str) to your endpoints. Use the same tag name for endpoints that use the same HTTP method, so that you can group your endpoints that way. For example, use Get as the tag name for GET operations (Note: Get is only an example here, you can use any tag name you wish).

    Doing just the above would most likely define the endpoints in the order you wish (i.e., GET, POST, PUT, DELETE). However, to ensure this—or, even in case you would like to define a different order for your methods/tags—you could add metadata for the different tags used to group your endpoints. You could do that using the parameter openapi_tags, which takes a list containing one dictionary for each tag. Each dictionary should at least contain the name, which should be the same tag name used in the tags parameter. The order of each tag metadata dictionary also defines the order shown in the docs UI.

    Working Example

    from fastapi import FastAPI
    
    tags_metadata = [
        {"name": "Get"},
        {"name": "Post"},
        {"name": "Put"},
        {"name": "Delete"}
    ]
    
    app = FastAPI(openapi_tags=tags_metadata)
    
    @app.get("/", tags=["Get"])
    def list_all_components():
        pass
    
    @app.get("/{component_id}", tags=["Get"])
    def get_component(component_id: int):
        pass
    
    @app.post("/", tags=["Post"])
    def create_component():
        pass
    
    @app.put("/{component_id}", tags=["Put"])
    def update_component(component_id: int):
        pass
    
    @app.delete("/{component_id}", tags=["Delete"])
    def delete_component(component_id: int):
        pass