Skip to content

Commit

Permalink
Merge branch 'release/0.3.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rius committed Jul 27, 2023
2 parents a2f3edc + 8234b96 commit c5a5f0a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 5 deletions.
20 changes: 20 additions & 0 deletions aiohttp_deps/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ def dummy(_var: annotation.annotation) -> None: # type: ignore
return var == Optional[var]


def _get_param_schema(annotation: Optional[inspect.Parameter]) -> Dict[str, Any]:
if annotation is None or annotation.annotation == annotation.empty:
return {}

def dummy(_var: annotation.annotation) -> None: # type: ignore
"""Dummy function to use for type resolution."""

var = get_type_hints(dummy).get("_var")
return pydantic.TypeAdapter(var).json_schema(ref_template=REF_TEMPLATE)


def _add_route_def( # noqa: C901, WPS210, WPS211
openapi_schema: Dict[str, Any],
route: web.ResourceRoute,
Expand Down Expand Up @@ -140,32 +151,41 @@ def _insert_in_params(data: Dict[str, Any]) -> None:
"content": {content_type: {}},
}
elif isinstance(dependency.dependency, Query):
schema = _get_param_schema(dependency.signature)
openapi_schema["components"]["schemas"].update(schema.pop("$defs", {}))
_insert_in_params(
{
"name": dependency.dependency.alias or dependency.param_name,
"in": "query",
"description": dependency.dependency.description,
"required": not _is_optional(dependency.signature),
"schema": schema,
},
)
elif isinstance(dependency.dependency, Header):
name = dependency.dependency.alias or dependency.param_name
schema = _get_param_schema(dependency.signature)
openapi_schema["components"]["schemas"].update(schema.pop("$defs", {}))
_insert_in_params(
{
"name": name.capitalize(),
"in": "header",
"description": dependency.dependency.description,
"required": not _is_optional(dependency.signature),
"schema": schema,
},
)
elif isinstance(dependency.dependency, Path):
schema = _get_param_schema(dependency.signature)
openapi_schema["components"]["schemas"].update(schema.pop("$defs", {}))
_insert_in_params(
{
"name": dependency.dependency.alias or dependency.param_name,
"in": "path",
"description": dependency.dependency.description,
"required": not _is_optional(dependency.signature),
"allowEmptyValue": _is_optional(dependency.signature),
"schema": schema,
},
)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "aiohttp-deps"
description = "Dependency injection for AioHTTP"
authors = ["Taskiq team <[email protected]>"]
maintainers = ["Taskiq team <[email protected]>"]
version = "0.3.1"
version = "0.3.2"
readme = "README.md"
license = "LICENSE"
classifiers = [
Expand Down
99 changes: 95 additions & 4 deletions tests/test_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ async def my_handler(my_var: int = Depends(Query(description="desc"))):
"required": True,
"in": "query",
"description": "desc",
"schema": {"type": "integer"},
}


Expand Down Expand Up @@ -242,6 +243,7 @@ async def my_handler(my_var: Optional[int] = Depends(Query())):
"required": False,
"in": "query",
"description": "",
"schema": {"anyOf": [{"type": "integer"}, {"type": "null"}]},
}


Expand Down Expand Up @@ -269,6 +271,7 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
"required": True,
"in": "query",
"description": "",
"schema": {"type": "integer"},
}


Expand All @@ -278,7 +281,13 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
(
(
Query(),
{"name": "my_var", "required": True, "in": "query", "description": ""},
{
"name": "my_var",
"required": True,
"in": "query",
"description": "",
"schema": {"type": "integer"},
},
),
(
Query(description="my query"),
Expand All @@ -287,15 +296,28 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
"required": True,
"in": "query",
"description": "my query",
"schema": {"type": "integer"},
},
),
(
Query(alias="a"),
{"name": "a", "required": True, "in": "query", "description": ""},
{
"name": "a",
"required": True,
"in": "query",
"description": "",
"schema": {"type": "integer"},
},
),
(
Header(),
{"name": "My_var", "required": True, "in": "header", "description": ""},
{
"name": "My_var",
"required": True,
"in": "header",
"description": "",
"schema": {"type": "integer"},
},
),
(
Header(description="my header"),
Expand All @@ -304,11 +326,18 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
"required": True,
"in": "header",
"description": "my header",
"schema": {"type": "integer"},
},
),
(
Header(alias="a"),
{"name": "A", "required": True, "in": "header", "description": ""},
{
"name": "A",
"required": True,
"in": "header",
"description": "",
"schema": {"type": "integer"},
},
),
(
Path(),
Expand All @@ -318,6 +347,7 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
"in": "path",
"description": "",
"allowEmptyValue": False,
"schema": {"type": "integer"},
},
),
(
Expand All @@ -328,6 +358,7 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
"in": "path",
"description": "my path",
"allowEmptyValue": False,
"schema": {"type": "integer"},
},
),
(
Expand All @@ -338,6 +369,7 @@ async def my_handler(my_var: int = Depends(Query(alias="qqq"))):
"in": "path",
"description": "",
"allowEmptyValue": False,
"schema": {"type": "integer"},
},
),
),
Expand All @@ -364,6 +396,65 @@ async def my_handler(my_var: int = Depends(dependecy)):
assert handler_info["parameters"][0] == param_info


@pytest.mark.anyio
@pytest.mark.parametrize(
["dependecy", "param_info"],
(
(
Query(),
{
"name": "my_var",
"required": False,
"in": "query",
"description": "",
"schema": {},
},
),
(
Header(),
{
"name": "My_var",
"required": False,
"in": "header",
"description": "",
"schema": {},
},
),
(
Path(),
{
"name": "my_var",
"required": False,
"in": "path",
"description": "",
"allowEmptyValue": True,
"schema": {},
},
),
),
)
async def test_parameters_untyped(
my_app: web.Application,
aiohttp_client: ClientGenerator,
dependecy: Any,
param_info: Dict[str, Any],
):
OPENAPI_URL = "/my_api_def.json"
my_app.on_startup.append(setup_swagger(schema_url=OPENAPI_URL))

async def my_handler(my_var=Depends(dependecy)):
"""Nothing."""

my_app.router.add_get("/a", my_handler)

client = await aiohttp_client(my_app)
resp = await client.get(OPENAPI_URL)
assert resp.status == 200
resp_json = await resp.json()
handler_info = resp_json["paths"]["/a"]["get"]
assert handler_info["parameters"][0] == param_info


@pytest.mark.anyio
async def test_view_success(
my_app: web.Application,
Expand Down

0 comments on commit c5a5f0a

Please sign in to comment.