如何在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)
如何按字母顺序对方法进行排序,例如DELETE,GET,POST,PUT。
我已经阅读了这篇文章,但是它是HTML格式的,但就我而言,我将Swagger集成到了Spring Boot中,因此在创建Docket时需要对其进行排序。
然后我operationOrdering()在Docket中注意到了这种方法,但是我仍然无法使它起作用。
我正在使用swagger版本3.0.2,我也遵循了这个答案,但是对方法顺序没有影响。
window.onload = function() {
const ui = SwaggerUIBundle({
.....
apisSorter: "alpha",
layout: "StandaloneLayout"
})
Run Code Online (Sandbox Code Playgroud)
谁能说出改变API方法顺序的最佳方法。
如何在 FastAPI Swagger 自动文档中设置 API 方法的排序顺序?我希望所有方法按类型分组(GET、POST、PUT、DELETE)。
这个答案展示了如何在 Java 中做到这一点。我怎样才能用Python做到这一点?
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
Run Code Online (Sandbox Code Playgroud)
swagger ×5
fastapi ×2
python ×2
swagger-ui ×2
java ×1
nestjs ×1
openapi ×1
spring-boot ×1
springfox ×1
swagger-2.0 ×1