python - Swagger-ui 中参数的排序

标签 python flask swagger-ui flask-restplus

我正在使用 Flask 和 Flask-restplus 构建一个带有 swagger UI 的 API。 路由似乎是按字母顺序显示的,但至于这些路由的参数,它们似乎是以完全随机的顺序显示的,每次我重新构建项目时都会改变。

我已经彻底搜索了一种设置参数在 UI 上出现的顺序的方法,但我没有找到任何东西(我预计很多人都会有同样的问题)。

有没有办法设置参数的顺序?

最佳答案

您需要使用操作排序器并编写自定义排序函数。这是一个例子:

// dist/index.html

const ui = SwaggerUIBundle({
url: "http://petstore.swagger.io/v2/swagger.json",
...

operationsSorter: (a, b) => {

        var methodsOrder = ["get", "post", "put", "delete", "patch", "options", "trace"];

        var result = methodsOrder.indexOf( a.get("method") ) - methodsOrder.indexOf( b.get("method") );
      // Or if you want to sort the methods alphabetically (delete, get, head, 
      options, 
      ...):
     // var result = a.get("method").localeCompare(b.get("method"));

      if (result === 0) {
       result = a.get("path").localeCompare(b.get("path"));
      }

   return result;
  }
})

关于python - Swagger-ui 中参数的排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55474949/

相关文章:

python - pymysql:TypeError:并非所有参数在字符串格式化期间都已转换

python - PythonAnywhere 中导入错误

json - 遵循 swagger 规范,如何将嵌套对象的 json 定义为 yaml?

node.js - Swagger jsdoc 不会在更改时立即更新 api 文档吗?

python - ssim 作为自动编码器中的自定义损失函数(keras 或/和tensorflow)

python - 防止方法重复

python - 为有效的 MySQL 列名过滤字符串

python - 如何使用 Flask WTF FileField 实际上传文件

python - 如何将请求流式传输到另一个网络服务器?

java - 如何 Swagger 下拉可能的值