Skip to content

Commit

Permalink
fix: add test and instance checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
robswc committed Mar 17, 2024
1 parent e6cf735 commit 707289c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
7 changes: 4 additions & 3 deletions litestar/_openapi/request_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ def create_request_body(

examples: dict[str, Example | Reference] | None = None
if isinstance(data_field.kwarg_definition, BodyKwarg) and data_field.kwarg_definition.examples:
examples = {
example.summary: example for example in data_field.kwarg_definition.examples if example.summary is not None
}
examples = {}
for example in data_field.kwarg_definition.examples:
if isinstance(example.summary, str) and isinstance(example.value, dict):
examples[example.summary] = example

return RequestBody(required=True, content={media_type: OpenAPIMediaType(schema=schema, examples=examples)})
32 changes: 18 additions & 14 deletions tests/unit/test_openapi/test_request_body.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Type
from unittest.mock import ANY, MagicMock
from typing import Callable, Dict, List, Type

import pytest
from typing_extensions import Annotated

from litestar import Controller, Litestar, post
from litestar._openapi.datastructures import OpenAPIContext
from litestar._openapi.request_body import create_request_body
from litestar.datastructures.upload_file import UploadFile
from litestar.dto import AbstractDTO
from litestar.enums import RequestEncodingType
from litestar.handlers import BaseRouteHandler
from litestar.openapi.config import OpenAPIConfig
from litestar.openapi.spec import RequestBody
from litestar.openapi.spec import Example, RequestBody
from litestar.params import Body
from litestar.typing import FieldDefinition

Expand Down Expand Up @@ -142,16 +141,21 @@ async def handle_form_upload(
}


def test_request_body_generation_with_dto(create_request: RequestBodyFactory) -> None:
mock_dto = MagicMock(spec=AbstractDTO)
def test_example_in_request_body_schema_generation() -> None:
@dataclass
class SampleClass:
name: str
age: int

@post(path="/form-upload", dto=mock_dto) # pyright: ignore
async def handler(data: Dict[str, Any]) -> None:
@post(path="/example")
async def handler(
data: Annotated[SampleClass, Body(examples=[Example(summary="example", value={"name": "John", "age": 30})])],
) -> None:
return None

Litestar(route_handlers=[handler])
field_definition = FieldDefinition.from_annotation(Dict[str, Any])
create_request(handler, field_definition)
mock_dto.create_openapi_schema.assert_called_once_with(
field_definition=field_definition, handler_id=handler.handler_id, schema_creator=ANY
)
app = Litestar([handler])
schema = app.openapi_schema.to_schema()

assert schema["paths"]["/example"]["post"]["requestBody"]["content"]["application/json"]["examples"] == {
"example": {"summary": "example", "value": {"name": "John", "age": 30}}
}

0 comments on commit 707289c

Please sign in to comment.