Skip to content

Commit 8130351

Browse files
committed
Fix deep_dict_update to merge openapi_extra list elements, rather than replace
1 parent 1b46ff1 commit 8130351

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

ninja/openapi/schema.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ def deep_dict_update(
9999
self.deep_dict_update(
100100
main_dict[key], update_dict[key]
101101
) # pragma: no cover
102+
elif (
103+
key in main_dict
104+
and isinstance(main_dict[key], list)
105+
and isinstance(update_dict[key], list)
106+
):
107+
main_dict[key].extend(update_dict[key])
102108
else:
103109
main_dict[key] = update_dict[key]
104110

tests/test_openapi_extra.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ninja import NinjaAPI
1+
from ninja import NinjaAPI, Router
22

33

44
def test_openapi_info_defined():
@@ -47,3 +47,43 @@ def test_openapi_extra():
4747
"url": "https://example.com",
4848
},
4949
}
50+
51+
52+
def test_router_openapi_extra_extends():
53+
"""
54+
Test for #1505.
55+
When adding an extra parameter to a route via openapi_extra, this should be combined with the route's own parameters.
56+
"""
57+
api = NinjaAPI()
58+
test_router = Router()
59+
api.add_router("", test_router)
60+
61+
extra_param = {
62+
"in": "header",
63+
"name": "X-HelloWorld",
64+
"required": False,
65+
"schema": {
66+
"type": "string",
67+
"format": "uuid",
68+
},
69+
}
70+
71+
@test_router.get("/path/{item_id}", openapi_extra={"parameters": [extra_param]})
72+
def get_path_item_id(request, item_id: int):
73+
pass
74+
75+
schema = api.get_openapi_schema()
76+
77+
assert len(schema["paths"]["/api/path/{item_id}"]["get"]["parameters"]) == 2
78+
assert schema["paths"]["/api/path/{item_id}"]["get"]["parameters"] == [
79+
{
80+
"in": "path",
81+
"name": "item_id",
82+
"required": True,
83+
"schema": {
84+
"title": "Item Id",
85+
"type": "integer",
86+
},
87+
},
88+
extra_param,
89+
]

0 commit comments

Comments
 (0)