-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom and collection type hint classes (#272)
- Loading branch information
Showing
4 changed files
with
77 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import uuid | ||
|
||
import pytest | ||
|
||
from drf_yasg import openapi | ||
from drf_yasg.inspectors.field import get_basic_type_info_from_hint | ||
|
||
try: | ||
import typing | ||
from typing import Dict, List, Union, Optional, Set | ||
except ImportError: | ||
typing = None | ||
|
||
|
||
if typing: | ||
@pytest.mark.parametrize('hint_class, expected_swagger_type_info', [ | ||
(int, {'type': openapi.TYPE_INTEGER, 'format': None}), | ||
(str, {'type': openapi.TYPE_STRING, 'format': None}), | ||
(bool, {'type': openapi.TYPE_BOOLEAN, 'format': None}), | ||
(dict, {'type': openapi.TYPE_OBJECT, 'format': None}), | ||
(Dict[int, int], {'type': openapi.TYPE_OBJECT, 'format': None}), | ||
(uuid.UUID, {'type': openapi.TYPE_STRING, 'format': openapi.FORMAT_UUID}), | ||
(List[int], {'type': openapi.TYPE_ARRAY, 'items': openapi.Items(openapi.TYPE_INTEGER)}), | ||
(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)}), | ||
# 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)}), | ||
]) | ||
def test_get_basic_type_info_from_hint(hint_class, expected_swagger_type_info): | ||
type_info = get_basic_type_info_from_hint(hint_class) | ||
assert type_info == expected_swagger_type_info |