Skip to content

Commit 4b6b278

Browse files
committed
Merge branch 'release/1.0.2'
2 parents df30846 + 5892dd8 commit 4b6b278

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

aiohttp_deps/swagger.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ def dummy(_var: annotation.annotation) -> None: # type: ignore
9393
"""Dummy function to use for type resolution."""
9494

9595
var = get_type_hints(dummy).get("_var")
96-
return pydantic.TypeAdapter(var).json_schema(ref_template=REF_TEMPLATE)
96+
return pydantic.TypeAdapter(var).json_schema(
97+
ref_template=REF_TEMPLATE,
98+
mode="validation",
99+
)
97100

98101

99102
def _add_route_def( # noqa: C901, WPS210, WPS211
@@ -139,7 +142,7 @@ def _insert_in_params(data: Dict[str, Any]) -> None:
139142
):
140143
input_schema = pydantic.TypeAdapter(
141144
dependency.signature.annotation,
142-
).json_schema(ref_template=REF_TEMPLATE)
145+
).json_schema(ref_template=REF_TEMPLATE, mode="validation")
143146
openapi_schema["components"]["schemas"].update(
144147
input_schema.pop("$defs", {}),
145148
)
@@ -365,7 +368,10 @@ def decorator(func: _T) -> _T:
365368
if not status_response:
366369
status_response["description"] = description
367370
status_response["content"] = status_response.get("content", {})
368-
response_schema = adapter.json_schema(ref_template=REF_TEMPLATE)
371+
response_schema = adapter.json_schema(
372+
ref_template=REF_TEMPLATE,
373+
mode="serialization",
374+
)
369375
openapi_schemas.update(response_schema.pop("$defs", {}))
370376
status_response["content"][content_type] = {"schema": response_schema}
371377
responses[status] = status_response

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 = "1.0.1"
6+
version = "1.0.2"
77
readme = "README.md"
88
license = "LICENSE"
99
classifiers = [

tests/test_swagger.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import pytest
55
from aiohttp import web
6-
from pydantic import BaseModel
6+
from pydantic import BaseModel, WithJsonSchema
7+
from typing_extensions import Annotated
78

89
from aiohttp_deps import (
910
Depends,
@@ -707,3 +708,42 @@ async def my_handler():
707708
]["schema"]["properties"]["data"]["$ref"]
708709
first_obj = follow_ref(first_ref, resp_json)
709710
assert "name" in first_obj["properties"]
711+
712+
713+
@pytest.mark.anyio
714+
async def test_annotated(
715+
my_app: web.Application,
716+
aiohttp_client: ClientGenerator,
717+
) -> None:
718+
OPENAPI_URL = "/my_api_def.json"
719+
my_app.on_startup.append(setup_swagger(schema_url=OPENAPI_URL))
720+
721+
validation_type = "int"
722+
serialization_type = "float"
723+
724+
MyType = Annotated[
725+
str,
726+
WithJsonSchema({"type": validation_type}, mode="validation"),
727+
WithJsonSchema({"type": serialization_type}, mode="serialization"),
728+
]
729+
730+
class TestModel(BaseModel):
731+
mt: MyType
732+
733+
@openapi_response(200, TestModel)
734+
async def my_handler(param: TestModel = Depends(Json())) -> None:
735+
"""Nothing."""
736+
737+
my_app.router.add_get("/a", my_handler)
738+
client = await aiohttp_client(my_app)
739+
response = await client.get(OPENAPI_URL)
740+
resp_json = await response.json()
741+
request_schema = resp_json["paths"]["/a"]["get"]
742+
oapi_serialization_type = request_schema["responses"]["200"]["content"][
743+
"application/json"
744+
]["schema"]["properties"]["mt"]["type"]
745+
assert oapi_serialization_type == serialization_type
746+
oapi_validation_type = request_schema["requestBody"]["content"]["application/json"][
747+
"schema"
748+
]["properties"]["mt"]["type"]
749+
assert oapi_validation_type == validation_type

0 commit comments

Comments
 (0)