|
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 | from aiohttp import web |
6 | | -from pydantic import BaseModel |
| 6 | +from pydantic import BaseModel, WithJsonSchema |
| 7 | +from typing_extensions import Annotated |
7 | 8 |
|
8 | 9 | from aiohttp_deps import ( |
9 | 10 | Depends, |
@@ -707,3 +708,42 @@ async def my_handler(): |
707 | 708 | ]["schema"]["properties"]["data"]["$ref"] |
708 | 709 | first_obj = follow_ref(first_ref, resp_json) |
709 | 710 | 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