Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sanic_ext/extensions/openapi/blueprint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
from copy import deepcopy

from functools import lru_cache, partial
from os.path import abspath, dirname, realpath
Expand Down Expand Up @@ -192,6 +193,10 @@ def build_spec(app):
):
operation.autodoc(docstring)

# Create an operation per method so defaults (like operationId)
# do not overwrite each other for multi-method routes.
operation = deepcopy(operation)

operation._default["operationId"] = (
f"{method.lower()}~{route_name}"
)
Expand Down
3 changes: 2 additions & 1 deletion sanic_ext/utils/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def get_all_routes(app, skip_prefix):
continue

method_handlers = [
(method, route.handler) for method in route.methods
(method, route.handler)
for method in sorted(route.methods)
]

_, name = route.name.split(".", 1)
Expand Down
15 changes: 15 additions & 0 deletions tests/extensions/openapi/test_operation_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from sanic import Sanic

from .utils import get_spec


def test_operation_id_is_unique_per_method_and_deterministic(app: Sanic):
@app.route("/ping", methods=["GET", "POST"])
async def ping(_):
return {"ping": "pong"}

spec = get_spec(app)
path = spec["paths"]["/ping"]

assert path["get"]["operationId"] == "get~ping"
assert path["post"]["operationId"] == "post~ping"