Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide a way to order the endpoints #168

Open
nioncode opened this issue Aug 7, 2024 · 4 comments
Open

provide a way to order the endpoints #168

nioncode opened this issue Aug 7, 2024 · 4 comments
Labels
bug Something isn't working Stale

Comments

@nioncode
Copy link

nioncode commented Aug 7, 2024

Is there a way to modify the order of the endpoints? Currently it seems they are grouped by tags and then sorted alphabetically by their path, which results in non-intuitive ordering (at least in our use case).

Assume we have the endpoints:

  • /start
  • /finish

Naturally, we'd like to see /start before /finish in the documentation, but currently these are reversed.

It would be great if we could provide a custom name used for sorting, e.g. something like:

@bp.post("/start", sorting_name="1_start")
def start():
    pass

@bp.post("/finish", sorting_name="2_finish")
def finish():
    pass

It would also be possible by using a simple integer for the sort order, but I think having a string and then keep using the alphabetical sorting order allows more customization and is less brittle when adding a new function, since we could also create some kind of subgroups between requests to order some requests relative to each other without impacting global order, e.g.:

@bp.post("/something/start", sorting_name="something_1_start")
def start():
    pass

@bp.post("/something/finish", sorting_name="something_2_finish")
def finish():
    pass

@bp.post("/other/start", sorting_name="other_1_start")
def start2():
    pass

@bp.post("/other/finish", sorting_name="other_2_finish")
def finish2():
    pass

Does something like this maybe already exist?

@nioncode nioncode added the bug Something isn't working label Aug 7, 2024
@luolingchun
Copy link
Owner

luolingchun commented Aug 11, 2024

@nioncode If you want to change the endpoint order of the swagger, you can change the default behavior by setting the SWAGGER_CONFIG.

from flask_openapi3 import OpenAPI

app = OpenAPI(__name__)

app.config["SWAGGER_CONFIG"] = {
    "operationsSorter":"method"
}

operationsSorter | Unavailable | Function=(a => a). Apply a sort to the operation list of each API. It can be 'alpha' (sort by paths alphanumerically), 'method' (sort by HTTP method) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.

@github-actions github-actions bot added the Stale label Nov 1, 2024
@nioncode
Copy link
Author

Sorry for my late response. Is there a similar flag to control the redoc API?

I found out that the order of the endpoints is actually how they are in our python code in the generated api.json when generating it via flask openapi -o api.json, so I guess it is not the responsibility of flask-openapi3 to sort the endpoints, but of the tool that generates the redoc API afterwards.

@luolingchun
Copy link
Owner

I've found that some configurations can be used:

usage:

app.config["REDOC_CONFIG"] = {
    "sortOperationsAlphabetically": True
}

@nioncode
Copy link
Author

The problem is that we don't want to sort alphabetically, but semantically. E.g. a start endpoint should come before a finish endpoint, but through alphabetical order it would be

  • finish
  • start

@github-actions github-actions bot removed the Stale label Dec 1, 2024
@github-actions github-actions bot added the Stale label Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale
Projects
None yet
Development

No branches or pull requests

2 participants