Skip to content

Commit c5a5f0a

Browse files
committed
Merge branch 'release/0.3.2'
2 parents a2f3edc + 8234b96 commit c5a5f0a

File tree

3 files changed

+116
-5
lines changed

3 files changed

+116
-5
lines changed

aiohttp_deps/swagger.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ def dummy(_var: annotation.annotation) -> None: # type: ignore
8585
return var == Optional[var]
8686

8787

88+
def _get_param_schema(annotation: Optional[inspect.Parameter]) -> Dict[str, Any]:
89+
if annotation is None or annotation.annotation == annotation.empty:
90+
return {}
91+
92+
def dummy(_var: annotation.annotation) -> None: # type: ignore
93+
"""Dummy function to use for type resolution."""
94+
95+
var = get_type_hints(dummy).get("_var")
96+
return pydantic.TypeAdapter(var).json_schema(ref_template=REF_TEMPLATE)
97+
98+
8899
def _add_route_def( # noqa: C901, WPS210, WPS211
89100
openapi_schema: Dict[str, Any],
90101
route: web.ResourceRoute,
@@ -140,32 +151,41 @@ def _insert_in_params(data: Dict[str, Any]) -> None:
140151
"content": {content_type: {}},
141152
}
142153
elif isinstance(dependency.dependency, Query):
154+
schema = _get_param_schema(dependency.signature)
155+
openapi_schema["components"]["schemas"].update(schema.pop("$defs", {}))
143156
_insert_in_params(
144157
{
145158
"name": dependency.dependency.alias or dependency.param_name,
146159
"in": "query",
147160
"description": dependency.dependency.description,
148161
"required": not _is_optional(dependency.signature),
162+
"schema": schema,
149163
},
150164
)
151165
elif isinstance(dependency.dependency, Header):
152166
name = dependency.dependency.alias or dependency.param_name
167+
schema = _get_param_schema(dependency.signature)
168+
openapi_schema["components"]["schemas"].update(schema.pop("$defs", {}))
153169
_insert_in_params(
154170
{
155171
"name": name.capitalize(),
156172
"in": "header",
157173
"description": dependency.dependency.description,
158174
"required": not _is_optional(dependency.signature),
175+
"schema": schema,
159176
},
160177
)
161178
elif isinstance(dependency.dependency, Path):
179+
schema = _get_param_schema(dependency.signature)
180+
openapi_schema["components"]["schemas"].update(schema.pop("$defs", {}))
162181
_insert_in_params(
163182
{
164183
"name": dependency.dependency.alias or dependency.param_name,
165184
"in": "path",
166185
"description": dependency.dependency.description,
167186
"required": not _is_optional(dependency.signature),
168187
"allowEmptyValue": _is_optional(dependency.signature),
188+
"schema": schema,
169189
},
170190
)
171191

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "aiohttp-deps"
33
description = "Dependency injection for AioHTTP"
44
authors = ["Taskiq team <taskiq@no-reply.com>"]
55
maintainers = ["Taskiq team <taskiq@no-reply.com>"]
6-
version = "0.3.1"
6+
version = "0.3.2"
77
readme = "README.md"
88
license = "LICENSE"
99
classifiers = [

tests/test_swagger.py

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ async def my_handler(my_var: int = Depends(Query(description="desc"))):
215215
"required": True,
216216
"in": "query",
217217
"description": "desc",
218+
"schema": {"type": "integer"},
218219
}
219220

220221

@@ -242,6 +243,7 @@ async def my_handler(my_var: Optional[int] = Depends(Query())):
242243
"required": False,
243244
"in": "query",
244245
"description": "",
246+
"schema": {"anyOf": [{"type": "integer"}, {"type": "null"}]},
245247
}
246248

247249

@@ -269,6 +271,7 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
269271
"required": True,
270272
"in": "query",
271273
"description": "",
274+
"schema": {"type": "integer"},
272275
}
273276

274277

@@ -278,7 +281,13 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
278281
(
279282
(
280283
Query(),
281-
{"name": "my_var", "required": True, "in": "query", "description": ""},
284+
{
285+
"name": "my_var",
286+
"required": True,
287+
"in": "query",
288+
"description": "",
289+
"schema": {"type": "integer"},
290+
},
282291
),
283292
(
284293
Query(description="my query"),
@@ -287,15 +296,28 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
287296
"required": True,
288297
"in": "query",
289298
"description": "my query",
299+
"schema": {"type": "integer"},
290300
},
291301
),
292302
(
293303
Query(alias="a"),
294-
{"name": "a", "required": True, "in": "query", "description": ""},
304+
{
305+
"name": "a",
306+
"required": True,
307+
"in": "query",
308+
"description": "",
309+
"schema": {"type": "integer"},
310+
},
295311
),
296312
(
297313
Header(),
298-
{"name": "My_var", "required": True, "in": "header", "description": ""},
314+
{
315+
"name": "My_var",
316+
"required": True,
317+
"in": "header",
318+
"description": "",
319+
"schema": {"type": "integer"},
320+
},
299321
),
300322
(
301323
Header(description="my header"),
@@ -304,11 +326,18 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
304326
"required": True,
305327
"in": "header",
306328
"description": "my header",
329+
"schema": {"type": "integer"},
307330
},
308331
),
309332
(
310333
Header(alias="a"),
311-
{"name": "A", "required": True, "in": "header", "description": ""},
334+
{
335+
"name": "A",
336+
"required": True,
337+
"in": "header",
338+
"description": "",
339+
"schema": {"type": "integer"},
340+
},
312341
),
313342
(
314343
Path(),
@@ -318,6 +347,7 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
318347
"in": "path",
319348
"description": "",
320349
"allowEmptyValue": False,
350+
"schema": {"type": "integer"},
321351
},
322352
),
323353
(
@@ -328,6 +358,7 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
328358
"in": "path",
329359
"description": "my path",
330360
"allowEmptyValue": False,
361+
"schema": {"type": "integer"},
331362
},
332363
),
333364
(
@@ -338,6 +369,7 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
338369
"in": "path",
339370
"description": "",
340371
"allowEmptyValue": False,
372+
"schema": {"type": "integer"},
341373
},
342374
),
343375
),
@@ -364,6 +396,65 @@ async def my_handler(my_var: int = Depends(dependecy)):
364396
assert handler_info["parameters"][0] == param_info
365397

366398

399+
@pytest.mark.anyio
400+
@pytest.mark.parametrize(
401+
["dependecy", "param_info"],
402+
(
403+
(
404+
Query(),
405+
{
406+
"name": "my_var",
407+
"required": False,
408+
"in": "query",
409+
"description": "",
410+
"schema": {},
411+
},
412+
),
413+
(
414+
Header(),
415+
{
416+
"name": "My_var",
417+
"required": False,
418+
"in": "header",
419+
"description": "",
420+
"schema": {},
421+
},
422+
),
423+
(
424+
Path(),
425+
{
426+
"name": "my_var",
427+
"required": False,
428+
"in": "path",
429+
"description": "",
430+
"allowEmptyValue": True,
431+
"schema": {},
432+
},
433+
),
434+
),
435+
)
436+
async def test_parameters_untyped(
437+
my_app: web.Application,
438+
aiohttp_client: ClientGenerator,
439+
dependecy: Any,
440+
param_info: Dict[str, Any],
441+
):
442+
OPENAPI_URL = "/my_api_def.json"
443+
my_app.on_startup.append(setup_swagger(schema_url=OPENAPI_URL))
444+
445+
async def my_handler(my_var=Depends(dependecy)):
446+
"""Nothing."""
447+
448+
my_app.router.add_get("/a", my_handler)
449+
450+
client = await aiohttp_client(my_app)
451+
resp = await client.get(OPENAPI_URL)
452+
assert resp.status == 200
453+
resp_json = await resp.json()
454+
handler_info = resp_json["paths"]["/a"]["get"]
455+
assert handler_info["parameters"][0] == param_info
456+
457+
367458
@pytest.mark.anyio
368459
async def test_view_success(
369460
my_app: web.Application,

0 commit comments

Comments
 (0)