如何在FastAPI Swagger autodocs中为 API 方法设置自定义排序顺序?
这个问题展示了如何在 Java 中做到这一点。我之前的问题询问如何按“方法”排序,这是受支持的排序方法。我真的很想更进一步,以便我可以确定方法出现的顺序。现在DELETE显示在顶部,但我希望 API 方法的顺序为:GET, POST, PUT, DELETE。
我知道可以在 JavaScript 中实现自定义排序并将该函数提供给operationsSorter,但您不能从swagger_ui_parametersPython 绑定中可用的属性中包含它。有什么方法可以在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
Run Code Online (Sandbox Code Playgroud)