From edbe1c9389fa7da402c64f889b29d7745961f8aa Mon Sep 17 00:00:00 2001 From: George Sakkis Date: Sat, 12 Aug 2023 08:06:12 +0300 Subject: [PATCH] fix: set model response to NOT prefer alias (#2150) Fix: set model response to NOT prefer alias --- litestar/_openapi/path_item.py | 2 +- tests/unit/test_openapi/test_config.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/litestar/_openapi/path_item.py b/litestar/_openapi/path_item.py index f8c3a4d041..1d3edc08ec 100644 --- a/litestar/_openapi/path_item.py +++ b/litestar/_openapi/path_item.py @@ -87,7 +87,7 @@ def create_path_item( operation_ids: list[str] = [] request_schema_creator = SchemaCreator(create_examples, plugins, schemas, prefer_alias=True) - response_schema_creator = SchemaCreator(create_examples, plugins, schemas, prefer_alias=True) + response_schema_creator = SchemaCreator(create_examples, plugins, schemas, prefer_alias=False) for http_method, handler_tuple in route.route_handler_map.items(): route_handler, _ = handler_tuple diff --git a/tests/unit/test_openapi/test_config.py b/tests/unit/test_openapi/test_config.py index 8790c7ff3d..24c9e7e8a8 100644 --- a/tests/unit/test_openapi/test_config.py +++ b/tests/unit/test_openapi/test_config.py @@ -8,6 +8,7 @@ from litestar.exceptions import ImproperlyConfiguredException from litestar.openapi.config import OpenAPIConfig from litestar.openapi.spec import Components, Example, OpenAPIHeader, OpenAPIType, Schema +from litestar.testing import TestClient if TYPE_CHECKING: from litestar.handlers.http_handlers import HTTPRouteHandler @@ -62,19 +63,25 @@ def handler(data: RequestWithAlias) -> ResponseWithAlias: assert app.openapi_schema schemas = app.openapi_schema.to_schema()["components"]["schemas"] + request_key = "second" assert schemas["RequestWithAlias"] == { - "properties": {"second": {"type": "string"}}, + "properties": {request_key: {"type": "string"}}, "type": "object", - "required": ["second"], + "required": [request_key], "title": "RequestWithAlias", } + response_key = "first" assert schemas["ResponseWithAlias"] == { - "properties": {"second": {"type": "string"}}, + "properties": {response_key: {"type": "string"}}, "type": "object", - "required": ["second"], + "required": [response_key], "title": "ResponseWithAlias", } + with TestClient(app) as client: + response = client.post("/", json={request_key: "foo"}) + assert response.json() == {response_key: "foo"} + def test_allows_customization_of_operation_id_creator() -> None: def operation_id_creator(handler: "HTTPRouteHandler", _: Any, __: Any) -> str: