Skip to content

Commit

Permalink
Add x-nullable to Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
axnsan12 committed Jan 29, 2019
1 parent 3806d6e commit 69a1e62
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
20 changes: 13 additions & 7 deletions src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def get_origin_type(hint_class):
return getattr(hint_class, '__origin__', None) or hint_class


def is_origin_type_subclasses(hint_class, check_class):
def hint_class_issubclass(hint_class, check_class):
origin_type = get_origin_type(hint_class)
return inspect.isclass(origin_type) and issubclass(origin_type, check_class)

Expand All @@ -480,13 +480,13 @@ def is_origin_type_subclasses(hint_class, check_class):
def inspect_collection_hint_class(hint_class):
args = hint_class.__args__
child_class = args[0] if args else str
child_type_info = get_basic_type_info_from_hint(child_class)
if not child_type_info:
child_type_info = {'type': openapi.TYPE_STRING}
child_type_info = get_basic_type_info_from_hint(child_class) or {'type': openapi.TYPE_STRING}

return OrderedDict([
('type', openapi.TYPE_ARRAY),
('items', openapi.Items(**child_type_info)),
])

hinting_type_info.append(((typing.Sequence, typing.AbstractSet), inspect_collection_hint_class))


Expand All @@ -500,18 +500,24 @@ def get_basic_type_info_from_hint(hint_class):
:rtype: OrderedDict
"""
if typing and get_origin_type(hint_class) == typing.Union:
if len(hint_class.__args__) == 2 and hint_class.__args__[1] == type(None):
# Optional is implemented as Union[T, None]
if len(hint_class.__args__) == 2 and hint_class.__args__[1] == type(None): # noqa: E721
child_type = hint_class.__args__[0]
return get_basic_type_info_from_hint(child_type)
result = get_basic_type_info_from_hint(child_type)
result['x-nullable'] = True
return result

return None

for check_class, info in hinting_type_info:
if is_origin_type_subclasses(hint_class, check_class):
if hint_class_issubclass(hint_class, check_class):
if callable(info):
return info(hint_class)

swagger_type, format = info
if callable(swagger_type):
swagger_type = swagger_type()

return OrderedDict([
('type', swagger_type),
('format', format),
Expand Down
11 changes: 7 additions & 4 deletions tests/test_get_basic_type_info_from_hint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
except ImportError:
typing = None


if typing:
@pytest.mark.parametrize('hint_class, expected_swagger_type_info', [
(int, {'type': openapi.TYPE_INTEGER, 'format': None}),
Expand All @@ -24,9 +23,13 @@
(List[str], {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_STRING)}),
(List[bool], {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_BOOLEAN)}),
(Set[int], {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_INTEGER)}),
(Optional[bool], {'type': openapi.TYPE_BOOLEAN, 'format': None}),
(Optional[List[int]], {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_INTEGER)}),
(Union[List[int], type(None)], {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_INTEGER)}),
(Optional[bool], {'type': openapi.TYPE_BOOLEAN, 'format': None, 'x-nullable': True}),
(Optional[List[int]], {
'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_INTEGER), 'x-nullable': True
}),
(Union[List[int], type(None)], {
'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_INTEGER), 'x-nullable': True
}),
# Following cases are not 100% correct, but it should work somehow and not crash.
(Union[int, float], None),
(List, {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_STRING)}),
Expand Down

0 comments on commit 69a1e62

Please sign in to comment.