Skip to content

Commit

Permalink
warn user if operation id is not unique (#80, #70)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalik committed Apr 28, 2021
1 parent 60261dc commit 651de56
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
9 changes: 8 additions & 1 deletion ninja/openapi/schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple, Type

from pydantic import BaseModel
Expand Down Expand Up @@ -27,6 +28,7 @@ def __init__(self, api: "NinjaAPI", path_prefix: str) -> None:
self.path_prefix = path_prefix
self.schemas: DictStrAny = {}
self.securitySchemes: DictStrAny = {}
self.all_operation_ids: Set = set()
super().__init__(
[
("openapi", "3.0.2"),
Expand All @@ -52,7 +54,7 @@ def get_paths(self) -> DictStrAny:
full_path = normalize_path(full_path)
path_methods = self.methods(path_view.operations)
if path_methods:
result[full_path] = self.methods(path_view.operations)
result[full_path] = path_methods
return result

def methods(self, operations: list) -> DictStrAny:
Expand All @@ -66,6 +68,11 @@ def methods(self, operations: list) -> DictStrAny:

def operation_details(self, operation: Operation) -> DictStrAny:
op_id = operation.operation_id or self.api.get_openapi_operation_id(operation)
if op_id in self.all_operation_ids:
warnings.warn(
f'operation_id "{op_id}" is already used (func: {operation.view_func})'
)
self.all_operation_ids.add(op_id)
result = {
"operationId": op_id,
"summary": operation.summary,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def demo_operation(request):
("basic", BasicAuth()),
("bearer", BearerAuth()),
]:
api.get(f"/{path}", auth=auth)(demo_operation)
api.get(f"/{path}", auth=auth, operation_id=path)(demo_operation)


client = NinjaClient(api)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_inheritance_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ def global_op(request):


@first_router.get("/endpoint")
def router_op(request):
def router_op1(request):
return "first"


second_router_one = Router()


@second_router_one.get("endpoint_1")
def router_op(request):
def router_op2(request):
return "second 1"


second_router_two = Router()


@second_router_two.get("endpoint_2")
def router_op(request):
def router_op3(request):
return "second 2"


Expand Down
19 changes: 17 additions & 2 deletions tests/test_openapi_schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pytest
from typing import List
from unittest.mock import patch
from ninja import NinjaAPI, Schema
from django.test import Client, override_settings
from copy import copy


api = NinjaAPI()
Expand Down Expand Up @@ -122,3 +121,19 @@ def test_schema():
"type": "object",
},
}


def test_unique_operation_ids():

api = NinjaAPI()

@api.get("/1")
def same_name(request):
pass

@api.get("/2")
def same_name(request):
pass

with pytest.warns(UserWarning):
schema = api.get_openapi_schema()

0 comments on commit 651de56

Please sign in to comment.