diff --git a/src/openapi_parser/builders/content.py b/src/openapi_parser/builders/content.py index a24bb82..41e8c06 100644 --- a/src/openapi_parser/builders/content.py +++ b/src/openapi_parser/builders/content.py @@ -1,5 +1,5 @@ import logging -from typing import Type, Union +from typing import Type, Union, Any from . import SchemaFactory from ..enumeration import ContentType @@ -21,15 +21,19 @@ def __init__(self, schema_factory: SchemaFactory, strict_enum: bool = True) -> N def build_list(self, data: dict) -> list[Content]: return [ - self._create_content(content_type, content_value['schema']) + self._create_content(content_type, content_value.get('schema', {}), + content_value.get('example', None), + content_value.get('examples', {})) for content_type, content_value in data.items() ] - def _create_content(self, content_type: str, content_value: dict) -> Content: + def _create_content(self, content_type: str, schema: dict, example: Any, examples: dict) -> Content: logger.debug(f"Content building [type={content_type}]") ContentTypeCls: ContentTypeType = ContentType if self.strict_enum else LooseContentType return Content( type=ContentTypeCls(content_type), - schema=self.schema_factory.create(content_value) + schema=self.schema_factory.create(schema), + example=example, + examples=examples ) diff --git a/src/openapi_parser/specification.py b/src/openapi_parser/specification.py index a53a6a3..b79ca5b 100644 --- a/src/openapi_parser/specification.py +++ b/src/openapi_parser/specification.py @@ -168,8 +168,8 @@ class Parameter: class Content: type: Union[ContentType, LooseContentType] schema: Schema - # example: Optional[Any] # TODO - # examples: list[Any] = field(default_factory=list) # TODO + example: Optional[Any] = None + examples: dict[str, Any] = field(default_factory=dict) # encoding: dict[str, Encoding] # TODO diff --git a/tests/builders/test_operation_builder.py b/tests/builders/test_operation_builder.py index f06a466..7e2f6b2 100644 --- a/tests/builders/test_operation_builder.py +++ b/tests/builders/test_operation_builder.py @@ -63,7 +63,7 @@ def _get_list_builder_mock(expected): description="Updated status of the pet") ), ], - ) + ), ), ] ) diff --git a/tests/builders/test_response_builder.py b/tests/builders/test_response_builder.py index 4cc0dfe..669b6f9 100644 --- a/tests/builders/test_response_builder.py +++ b/tests/builders/test_response_builder.py @@ -44,7 +44,8 @@ def _get_builder_mock(expected_value: Any) -> Union[ContentBuilder, HeaderBuilde "application/json": { "schema": { "type": "string", - } + }, + "example": "an example" } }, "headers": { diff --git a/tests/openapi_fixture.py b/tests/openapi_fixture.py index 4ebdc6d..b02047c 100644 --- a/tests/openapi_fixture.py +++ b/tests/openapi_fixture.py @@ -339,7 +339,7 @@ def create_specification() -> Specification: request_body=RequestBody( description="New user model request", content=[ - Content(type=ContentType.JSON, schema=schema_user), + Content(type=ContentType.JSON, schema=schema_user) ] ), responses=[