Skip to content

Commit

Permalink
fix: set model response to NOT prefer alias (#2150)
Browse files Browse the repository at this point in the history
Fix: set model response to NOT prefer alias
  • Loading branch information
gsakkis authored Aug 12, 2023
1 parent 426cab8 commit edbe1c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion litestar/_openapi/path_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 11 additions & 4 deletions tests/unit/test_openapi/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit edbe1c9

Please sign in to comment.