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

Schemas extracted to OpenAPI components #313

Merged
merged 5 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions tests/winter_openapi/test_api_request_and_response_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class Dataclass:
nested: NestedDataclass


@dataclass
class DataclassWithOptionalField:
"""DataclassWithOptionalField description"""
nested: Optional[NestedDataclass]


class StringValueEnum(Enum):
RED = 'red'
GREEN = 'green'
Expand Down Expand Up @@ -151,9 +157,8 @@ class RequestBodyWithUndefined:
{
'schema': {
'items': {
'nullable': False,
'description': 'Can be any value - string, number, boolean, array or object.',
'nullable': False
'nullable': False,
},
'type': 'array',
},
Expand All @@ -164,9 +169,8 @@ class RequestBodyWithUndefined:
{
'schema': {
'items': {
'nullable': False,
'description': 'Can be any value - string, number, boolean, array or object.',
'nullable': False
'nullable': False,
},
'type': 'array',
},
Expand All @@ -193,6 +197,31 @@ class RequestBodyWithUndefined:
},
},
),
(
DataclassWithOptionalField,
{
'schema': {
'description': 'DataclassWithOptionalField description',
'properties': {
'nested': {
'nullable': True,
'allOf': [
{
'properties': {'nested_number': {'format': 'int32', 'type': 'integer'}},
'required': ['nested_number'],
'title': 'NestedDataclass',
'type': 'object',
'description': 'NestedDataclass(nested_number: int)',
},
],
},
},
'required': ['nested'],
'title': 'DataclassWithOptionalField',
'type': 'object',
},
},
),
(
Page[NestedDataclass],
{
Expand Down
14 changes: 10 additions & 4 deletions winter_openapi/type_info_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ def convert_type_info_to_openapi_schema(value: TypeInfo, *, output: bool) -> Sch
if value.child is not None:
data['items'] = convert_type_info_to_openapi_schema(value.child, output=output)

if value.nullable:
data['nullable'] = True

if value.enum is not None:
data['enum'] = value.enum

Expand All @@ -53,4 +50,13 @@ def convert_type_info_to_openapi_schema(value: TypeInfo, *, output: bool) -> Sch
if required_properties:
data['required'] = required_properties

return Schema(**data)
schema = Schema(**data)

if value.nullable:
if value.type_.value == 'object':
# https://stackoverflow.com/questions/40920441/how-to-specify-a-property-can-be-null-or-a-reference-with-swagger
schema = Schema(nullable=True, allOf=[schema])
else:
schema.nullable = True

return schema
Loading