Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support example/examples on content value body request #87

Merged
merged 12 commits into from
Sep 5, 2024
12 changes: 8 additions & 4 deletions src/openapi_parser/builders/content.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Type, Union
from typing import Type, Union, Any

from . import SchemaFactory
from ..enumeration import ContentType
Expand All @@ -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
)
4 changes: 2 additions & 2 deletions src/openapi_parser/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion tests/builders/test_operation_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _get_list_builder_mock(expected):
description="Updated status of the pet")
),
],
)
),
),
]
)
Expand Down
3 changes: 2 additions & 1 deletion tests/builders/test_response_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def _get_builder_mock(expected_value: Any) -> Union[ContentBuilder, HeaderBuilde
"application/json": {
"schema": {
"type": "string",
}
},
"example": "an example"
}
},
"headers": {
Expand Down
2 changes: 1 addition & 1 deletion tests/openapi_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down
Loading