Skip to content

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

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

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: list) -> 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]
examples: list[Any] = field(default_factory=list)
# encoding: dict[str, Encoding] # TODO


Expand Down
4 changes: 2 additions & 2 deletions tests/builders/test_content_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _get_schema_factory_mock(expected_value: Schema) -> SchemaFactory:
}
},
[
Content(type=ContentType.JSON, schema=string_schema)
Content(type=ContentType.JSON, schema=string_schema, example=None, examples=[])
],
_get_schema_factory_mock(string_schema)
),
Expand All @@ -40,7 +40,7 @@ def _get_schema_factory_mock(expected_value: Schema) -> SchemaFactory:
}
},
[
Content(type=ContentType.JSON_TEXT, schema=string_schema)
Content(type=ContentType.JSON_TEXT, schema=string_schema, example=None, examples=[])
],
_get_schema_factory_mock(string_schema)
),
Expand Down
6 changes: 4 additions & 2 deletions tests/builders/test_operation_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _get_list_builder_mock(expected):
response_schema = Response(
code=200,
description="Pet updated.",
content=[Content(type=ContentType.JSON, schema=Object(type=DataType.OBJECT))],
content=[Content(type=ContentType.JSON, schema=Object(type=DataType.OBJECT), example="an example", examples=[])],
is_default=False,
)

Expand Down Expand Up @@ -63,7 +63,9 @@ def _get_list_builder_mock(expected):
description="Updated status of the pet")
),
],
)
),
example="an example",
examples=[],
),
]
)
Expand Down
3 changes: 2 additions & 1 deletion tests/builders/test_path_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
from random import sample
from unittest.mock import MagicMock

import pytest
Expand Down Expand Up @@ -43,7 +44,7 @@ def _get_builder_list_mock(expected_value):
Response(
code=200,
description="pet response",
content=[Content(type=ContentType.JSON, schema=array_schema)],
content=[Content(type=ContentType.JSON, schema=array_schema, example="an example", examples=[])],
is_default=False,
)
],
Expand Down
12 changes: 9 additions & 3 deletions tests/builders/test_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def _get_content_builder_mock(expected_value: Any) -> ContentBuilder:
properties=[
Property(name="login", schema=String(type=DataType.STRING))
]
)
),
example="an example",
examples=[]
)
]

Expand All @@ -35,7 +37,9 @@ def _get_content_builder_mock(expected_value: Any) -> ContentBuilder:
properties=[
Property(name="login", schema=String(type=DataType.STRING))
]
)
),
example="an example",
examples=[]
),
Content(
type=ContentType.FORM,
Expand All @@ -44,7 +48,9 @@ def _get_content_builder_mock(expected_value: Any) -> ContentBuilder:
properties=[
Property(name="login", schema=String(type=DataType.STRING))
]
)
),
example="an example",
examples=[]
),
]

Expand Down
9 changes: 7 additions & 2 deletions tests/builders/test_response_builder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from tkinter.scrolledtext import example
from typing import Any, Union
from unittest.mock import MagicMock

Expand All @@ -24,7 +25,9 @@ def _get_builder_mock(expected_value: Any) -> Union[ContentBuilder, HeaderBuilde
properties=[
Property(name="login", schema=String(type=DataType.STRING))
]
)
),
example="an example",
examples=[]
)
]

Expand All @@ -44,7 +47,9 @@ def _get_builder_mock(expected_value: Any) -> Union[ContentBuilder, HeaderBuilde
"application/json": {
"schema": {
"type": "string",
}
},
"example": "an example",
"examples": ["example1", "example2"]
}
},
"headers": {
Expand Down
18 changes: 14 additions & 4 deletions tests/openapi_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
content=[
Content(
type=ContentType.JSON,
schema=user_list_schema
schema=user_list_schema,
example=None,
examples=[]
),
],
is_default=False,
Expand Down Expand Up @@ -103,7 +105,9 @@
)
),
],
)
),
example=None,
examples=[]
),
],
)
Expand Down Expand Up @@ -136,7 +140,9 @@
)
),
],
)
),
example=None,
examples=[]
),
],
)
Expand Down Expand Up @@ -339,7 +345,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, example=None, examples=[])
]
),
responses=[
Expand All @@ -360,6 +366,8 @@ def create_specification() -> Specification:
),
],
),
example=None,
examples=[]
),
]
),
Expand Down Expand Up @@ -398,6 +406,8 @@ def create_specification() -> Specification:
),
],
),
example=None,
examples=[]
),
]
),
Expand Down
Loading