2

I'm using Flask and Flask-restplus to build an API with a swagger UI. Routes seem to be displayed in alphabetical order, but as for parameters of these routes, they seem to be displayed in a completely random order that changes every time I re-build my project.

I have searched thoroughly for a way to set the order in which parameters appear on the UI, but I have not found anything at all (I expected that many people would have had the same question).

Is there no way to set the ordering of parameters?

Fatalize
  • 3,513
  • 15
  • 25

1 Answers1

0

You need to use the operationsSorter and write a custom sort function. Here's an example:

// 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;
  }
})
Hasni Iheb
  • 282
  • 2
  • 12