Skip to content

Commit 7e6faa1

Browse files
authored
Merge pull request #87 from thaichat04/add-example-content-value
Add support example/examples on content value body request
2 parents ca1dda7 + 2fb92eb commit 7e6faa1

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

src/openapi_parser/builders/content.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Type, Union
2+
from typing import Type, Union, Any
33

44
from . import SchemaFactory
55
from ..enumeration import ContentType
@@ -21,15 +21,19 @@ def __init__(self, schema_factory: SchemaFactory, strict_enum: bool = True) -> N
2121

2222
def build_list(self, data: dict) -> list[Content]:
2323
return [
24-
self._create_content(content_type, content_value['schema'])
24+
self._create_content(content_type, content_value.get('schema', {}),
25+
content_value.get('example', None),
26+
content_value.get('examples', {}))
2527
for content_type, content_value
2628
in data.items()
2729
]
2830

29-
def _create_content(self, content_type: str, content_value: dict) -> Content:
31+
def _create_content(self, content_type: str, schema: dict, example: Any, examples: dict) -> Content:
3032
logger.debug(f"Content building [type={content_type}]")
3133
ContentTypeCls: ContentTypeType = ContentType if self.strict_enum else LooseContentType
3234
return Content(
3335
type=ContentTypeCls(content_type),
34-
schema=self.schema_factory.create(content_value)
36+
schema=self.schema_factory.create(schema),
37+
example=example,
38+
examples=examples
3539
)

src/openapi_parser/specification.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ class Parameter:
168168
class Content:
169169
type: Union[ContentType, LooseContentType]
170170
schema: Schema
171-
# example: Optional[Any] # TODO
172-
# examples: list[Any] = field(default_factory=list) # TODO
171+
example: Optional[Any] = None
172+
examples: dict[str, Any] = field(default_factory=dict)
173173
# encoding: dict[str, Encoding] # TODO
174174

175175

tests/builders/test_operation_builder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _get_list_builder_mock(expected):
6363
description="Updated status of the pet")
6464
),
6565
],
66-
)
66+
),
6767
),
6868
]
6969
)

tests/builders/test_response_builder.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def _get_builder_mock(expected_value: Any) -> Union[ContentBuilder, HeaderBuilde
4444
"application/json": {
4545
"schema": {
4646
"type": "string",
47-
}
47+
},
48+
"example": "an example"
4849
}
4950
},
5051
"headers": {

tests/openapi_fixture.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def create_specification() -> Specification:
339339
request_body=RequestBody(
340340
description="New user model request",
341341
content=[
342-
Content(type=ContentType.JSON, schema=schema_user),
342+
Content(type=ContentType.JSON, schema=schema_user)
343343
]
344344
),
345345
responses=[

0 commit comments

Comments
 (0)