Skip to content

Commit

Permalink
fix: use get_formatted_examples to format examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
robswc committed Mar 17, 2024
1 parent 707289c commit 1e75753
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
14 changes: 6 additions & 8 deletions litestar/_openapi/request_body.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Mapping

from litestar._openapi.schema_generation import SchemaCreator
from litestar._openapi.schema_generation.utils import get_formatted_examples
from litestar.enums import RequestEncodingType
from litestar.openapi.spec.media_type import OpenAPIMediaType
from litestar.openapi.spec.request_body import RequestBody
Expand All @@ -13,7 +14,7 @@
if TYPE_CHECKING:
from litestar._openapi.datastructures import OpenAPIContext
from litestar.dto import AbstractDTO
from litestar.openapi.spec import Example, Reference
from litestar.openapi.spec import Example
from litestar.typing import FieldDefinition


Expand Down Expand Up @@ -48,11 +49,8 @@ def create_request_body(
else:
schema = schema_creator.for_field_definition(data_field)

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

return RequestBody(required=True, content={media_type: OpenAPIMediaType(schema=schema, examples=examples)})
4 changes: 2 additions & 2 deletions litestar/openapi/spec/media_type.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Mapping

from litestar.openapi.spec.base import BaseSchemaObject

Expand Down Expand Up @@ -32,7 +32,7 @@ class OpenAPIMediaType(BaseSchemaObject):
example provided by the schema.
"""

examples: dict[str, Example | Reference] | None = None
examples: dict[str, Example | Reference] | Mapping[str, Example] | None = None
"""Examples of the media type.
Each example object SHOULD match the media type and specified schema if present.
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_openapi/test_request_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,5 @@ async def 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}}
"data-example-1": {"summary": "example", "value": {"name": "John", "age": 30}}
}

0 comments on commit 1e75753

Please sign in to comment.