forked from axnsan12/drf-yasg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for SerializerMethodField (axnsan12#137)
- Loading branch information
1 parent
16b6ed7
commit fcc65aa
Showing
11 changed files
with
493 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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,70 @@ | ||
import datetime | ||
import decimal | ||
import typing | ||
import uuid | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class Unknown(object): | ||
pass | ||
|
||
|
||
class MethodFieldExampleSerializer(serializers.Serializer): | ||
""" | ||
Implementation of SerializerMethodField using type hinting for Python >= 3.5 | ||
""" | ||
|
||
hinted_bool = serializers.SerializerMethodField( | ||
help_text="the type hint on the method should determine this to be a bool") | ||
|
||
def get_hinted_bool(self, obj) -> bool: | ||
return True | ||
|
||
hinted_int = serializers.SerializerMethodField( | ||
help_text="the type hint on the method should determine this to be an integer") | ||
|
||
def get_hinted_int(self, obj) -> int: | ||
return 1 | ||
|
||
hinted_float = serializers.SerializerMethodField( | ||
help_text="the type hint on the method should determine this to be a number") | ||
|
||
def get_hinted_float(self, obj) -> float: | ||
return 1.0 | ||
|
||
hinted_decimal = serializers.SerializerMethodField( | ||
help_text="the type hint on the method should determine this to be a decimal") | ||
|
||
def get_hinted_decimal(self, obj) -> decimal.Decimal: | ||
return decimal.Decimal(1) | ||
|
||
hinted_datetime = serializers.SerializerMethodField( | ||
help_text="the type hint on the method should determine this to be a datetime") | ||
|
||
def get_hinted_datetime(self, obj) -> datetime.datetime: | ||
return datetime.datetime.now() | ||
|
||
hinted_date = serializers.SerializerMethodField( | ||
help_text="the type hint on the method should determine this to be a date") | ||
|
||
def get_hinted_date(self, obj) -> datetime.date: | ||
return datetime.date.today() | ||
|
||
hinted_uuid = serializers.SerializerMethodField( | ||
help_text="the type hint on the method should determine this to be a uuid") | ||
|
||
def get_hinted_uuid(self, obj) -> uuid.UUID: | ||
return uuid.uuid4() | ||
|
||
hinted_unknown = serializers.SerializerMethodField( | ||
help_text="type hint is unknown, so is expected to fallback to string") | ||
|
||
def get_hinted_unknown(self, obj) -> Unknown: | ||
return Unknown() | ||
|
||
non_hinted_number = serializers.SerializerMethodField( | ||
help_text="No hint on the method, so this is expected to fallback to string") | ||
|
||
def get_non_hinted_number(self, obj): | ||
return 1.0 |
Oops, something went wrong.