Skip to content

Commit

Permalink
Merge branch 'release/1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rius committed Sep 5, 2023
2 parents df30846 + 5892dd8 commit 4b6b278
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
12 changes: 9 additions & 3 deletions aiohttp_deps/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ 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)
return pydantic.TypeAdapter(var).json_schema(
ref_template=REF_TEMPLATE,
mode="validation",
)


def _add_route_def( # noqa: C901, WPS210, WPS211
Expand Down Expand Up @@ -139,7 +142,7 @@ def _insert_in_params(data: Dict[str, Any]) -> None:
):
input_schema = pydantic.TypeAdapter(
dependency.signature.annotation,
).json_schema(ref_template=REF_TEMPLATE)
).json_schema(ref_template=REF_TEMPLATE, mode="validation")
openapi_schema["components"]["schemas"].update(
input_schema.pop("$defs", {}),
)
Expand Down Expand Up @@ -365,7 +368,10 @@ def decorator(func: _T) -> _T:
if not status_response:
status_response["description"] = description
status_response["content"] = status_response.get("content", {})
response_schema = adapter.json_schema(ref_template=REF_TEMPLATE)
response_schema = adapter.json_schema(
ref_template=REF_TEMPLATE,
mode="serialization",
)
openapi_schemas.update(response_schema.pop("$defs", {}))
status_response["content"][content_type] = {"schema": response_schema}
responses[status] = status_response
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 = "1.0.1"
version = "1.0.2"
readme = "README.md"
license = "LICENSE"
classifiers = [
Expand Down
42 changes: 41 additions & 1 deletion tests/test_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import pytest
from aiohttp import web
from pydantic import BaseModel
from pydantic import BaseModel, WithJsonSchema
from typing_extensions import Annotated

from aiohttp_deps import (
Depends,
Expand Down Expand Up @@ -707,3 +708,42 @@ async def my_handler():
]["schema"]["properties"]["data"]["$ref"]
first_obj = follow_ref(first_ref, resp_json)
assert "name" in first_obj["properties"]


@pytest.mark.anyio
async def test_annotated(
my_app: web.Application,
aiohttp_client: ClientGenerator,
) -> None:
OPENAPI_URL = "/my_api_def.json"
my_app.on_startup.append(setup_swagger(schema_url=OPENAPI_URL))

validation_type = "int"
serialization_type = "float"

MyType = Annotated[
str,
WithJsonSchema({"type": validation_type}, mode="validation"),
WithJsonSchema({"type": serialization_type}, mode="serialization"),
]

class TestModel(BaseModel):
mt: MyType

@openapi_response(200, TestModel)
async def my_handler(param: TestModel = Depends(Json())) -> None:
"""Nothing."""

my_app.router.add_get("/a", my_handler)
client = await aiohttp_client(my_app)
response = await client.get(OPENAPI_URL)
resp_json = await response.json()
request_schema = resp_json["paths"]["/a"]["get"]
oapi_serialization_type = request_schema["responses"]["200"]["content"][
"application/json"
]["schema"]["properties"]["mt"]["type"]
assert oapi_serialization_type == serialization_type
oapi_validation_type = request_schema["requestBody"]["content"]["application/json"][
"schema"
]["properties"]["mt"]["type"]
assert oapi_validation_type == validation_type

0 comments on commit 4b6b278

Please sign in to comment.