Skip to content

Commit 9759c03

Browse files
committed
print in red if user makes duplicated name
1 parent f93b0a0 commit 9759c03

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

ninja/openapi/schema.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import itertools
22
import re
3-
import warnings
43
from http.client import responses
54
from typing import TYPE_CHECKING, Any, Dict, Generator, List, Optional, Set, Tuple
65

6+
from django.utils.termcolors import make_style
7+
78
from ninja.constants import NOT_SET
89
from ninja.operation import Operation
910
from ninja.params.models import TModel, TModels
@@ -28,6 +29,9 @@ def get_schema(api: "NinjaAPI", path_prefix: str = "") -> "OpenAPISchema":
2829
return openapi
2930

3031

32+
bold_red_style = make_style(opts=("bold",), fg="red")
33+
34+
3135
class OpenAPISchema(dict):
3236
def __init__(self, api: "NinjaAPI", path_prefix: str) -> None:
3337
self.api = api
@@ -103,9 +107,10 @@ def deep_dict_update(
103107
def operation_details(self, operation: Operation) -> DictStrAny:
104108
op_id = operation.operation_id or self.api.get_openapi_operation_id(operation)
105109
if op_id in self.all_operation_ids:
106-
warnings.warn(
107-
f'operation_id "{op_id}" is already used (func: {operation.view_func})',
108-
stacklevel=2,
110+
print(
111+
bold_red_style(
112+
f'Warning: operation_id "{op_id}" is already used (Try giving a different name to: {operation.view_func.__module__}.{operation.view_func.__name__})'
113+
)
109114
)
110115
self.all_operation_ids.add(op_id)
111116
result = {

tests/test_openapi_schema.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ def test_get_openapi_urls():
792792
get_openapi_urls(api)
793793

794794

795-
def test_unique_operation_ids():
795+
def test_unique_operation_ids(capsys):
796796
api = NinjaAPI()
797797

798798
@api.get("/1")
@@ -803,9 +803,9 @@ def same_name(request):
803803
def same_name(request): # noqa: F811
804804
pass
805805

806-
match = 'operation_id "test_openapi_schema_same_name" is already used'
807-
with pytest.warns(UserWarning, match=match):
808-
api.get_openapi_schema()
806+
api.get_openapi_schema()
807+
captured = capsys.readouterr()
808+
assert '"test_openapi_schema_same_name" is already used ' in captured.out
809809

810810

811811
def test_docs_decorator():

0 commit comments

Comments
 (0)