FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Separated serialization and validation schemas. by s3rius · Pull Request #16 · taskiq-python/aiohttp-deps · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 9 additions & 3 deletions aiohttp_deps/swagger.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
42 changes: 41 additions & 1 deletion tests/test_swagger.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL