4

Is there any way to sort the Namespace entries after they've been added to the Api?

I'm following the documentation, and the flow (AFAIK) appears to be:

a) Create the API:

api = Api(version="1.0",
      title="MahApi",
      description="Mah Nice Little API",
      doc="/swagger-ui",
      strict_slashes=False)

b) Define namespaces:

ns = Namespace("sample_namespace",
           description="sample module api",
           path='/sample_one')
...
@ns.route('/test1', endpoint='sample_ns_test1')
class TestApi(Resource):

    def get(self):
        df = mah_service.get_some_data()
        return jsonify(json.loads(df.to_json(orient='records')))

c) Add namespaces to the API:

api.add_namespace(mah_sample_ns)

Where I'm facing an issue (albeit it's a cosmetic yet annoying one) is that the namespaces are not being sorted in any way. It seems that I will need to sort the namespaces myself, manually, in code. Is there a more clever, pythonic way to get swagger to sort the namespaces?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ojingo
  • 202
  • 2
  • 9
  • Possibly a duplicate of [Sort API methods in Swagger-UI](https://stackoverflow.com/questions/24951268/sort-api-methods-in-swagger-ui). – dmulter Aug 02 '18 at 17:18
  • @dmulter: taht link is for java. Flask restplus is python, which has its own way df dong things... – Ojingo Aug 14 '18 at 15:13
  • Actually it's for JavaScript, not Java. My point was that you can sort the namespaces on the frontend and accomplish what you are looking for without coding it on the backend. It would be much more complicated to sort them in Python in the backend. – dmulter Aug 14 '18 at 15:16
  • @dmulter: I'm asking if there is a `flask-restplus` solution. I guess if there is nothing out-of-the-box in the pythonic world, I will look at doing it in a JS way. But I'd much rather not, since the whole purpose of flask-restplus was to abstract all of this away from the codebase. – Ojingo Aug 14 '18 at 15:36
  • You should also note that even if you sort the namespaces in the JSON on the backend, that is still no guarantee that the frontend will display them in that JSON order. If you want to guarantee a frontend display in sorted order, there really is no other way than addressing it in the frontend. – dmulter Aug 14 '18 at 15:40
  • @dmulter: got it. Thanks for the helpful info! – Ojingo Aug 15 '18 at 13:37

1 Answers1

0

I am probably way too late but I have been wondering the same thing but with flask_restx so posting this to hopefully help others. But it seems to go by the order in which your namespaces are imported.

For example:

from rest.endpoints.Episodes import ns as episodes_namespace
from rest.endpoints.Plex import ns as plex_namespace
from rest.endpoints.Server import ns as server_namespace

Will be ordered in swagger as:

  1. Episodes
  2. Plex
  3. Server

While:

from rest.endpoints.Server import ns as server_namespace
from rest.endpoints.Episodes import ns as episodes_namespace
from rest.endpoints.Plex import ns as plex_namespace

Will be ordered in swagger as:

  1. Server
  2. Episodes
  3. Plex
wedgess
  • 68
  • 1
  • 8