Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ Medium version numbers (0.x.0) may include API changes, in line with the [deprec
Major version numbers (x.0.0) are reserved for substantial project milestones.


### 0.3.13

**Date:** [2nd April 2020]

* Added check on `None` in `to_representation` methods in:
* ['CharField'][CharField]
* ['IntegerField'][IntegerField]
* ['FloatField'][FloatField]
* ['DictField'][DictField]

### 0.3.12

**Date:** [21th January 2020]
Expand Down Expand Up @@ -201,6 +211,9 @@ class TwoSer(Ser):
[ListField]: api-guid/fields.md#-listfield
[JsonField]: api-guid/fields.md#-jsonfield
[DictField]: api-guid/fields.md#-dictfield
[IntegerField]: api-guid/fields.md#-integerfield
[FloatField]: api-guid/fields.md#-floatfield
[CharField]: api-guid/fields.md#-charfield
[SerializerMethodField]: api-guid/fields.md#-serializermethodfield
[InheritSerializers]: api-guid/serializers.md#serializer-inheritance
[RegexValidator]: api-guid/validators.md#regexvalidator
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__/ |
|___/
"""
VERSION = (0, 3, 12)
VERSION = (0, 3, 13)

__title__ = 'Python-Rest-Framework'
__author__ = 'Deys Timofey'
Expand Down
8 changes: 8 additions & 0 deletions rest_framework/serializers/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ def to_representation(self, value):
:rtype: str

"""
if value is None:
return None
return six.text_type(value)


Expand Down Expand Up @@ -567,6 +569,8 @@ def to_representation(self, value):
:rtype: int

"""
if value is None:
return value
return int(value)


Expand Down Expand Up @@ -642,6 +646,8 @@ def to_representation(self, value):
:rtype: float

"""
if value is None:
return value
return float(value)


Expand Down Expand Up @@ -1305,6 +1311,8 @@ def to_representation(self, value):
:rtype: str

"""
if value is None:
return value
return {
six.text_type(key): self.child._to_representation(val) if val is not None else None
for key, val in six.iteritems(value)
Expand Down
9 changes: 7 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ class CharFieldTest(BaseFieldTestCase):
to_representation_cases = (
{'data': {'value': '123'}, 'return': '123'},
{'data': {'value': 123}, 'return': '123'},
{'data': {'value': 'qwe'}, 'return': 'qwe'}
{'data': {'value': 'qwe'}, 'return': 'qwe'},
{'data': {'value': None}, 'return': None},
) # Cases, to test the performance of `.to_representation()`.
to_internal_value_cases = (
{'data': {'data': '123'}, 'return': '123'},
Expand Down Expand Up @@ -560,6 +561,7 @@ class TestIntegerField(BaseFieldTestCase):
{'data': {'value': 123}, 'return': 123},
{'data': {'value': '123'}, 'return': 123},
{'data': {'value': 'qwe'}, 'exceptions': (ValueError,)},
{'data': {'value': None}, 'return': None},
) # Cases, to test the performance of `.to_representation()`.
to_internal_value_cases = (
{'data': {'data': 123}, 'return': 123},
Expand Down Expand Up @@ -609,6 +611,7 @@ class TestFloatField(BaseFieldTestCase):
{'data': {'value': 123}, 'return': 123.0},
{'data': {'value': '123'}, 'return': 123.0},
{'data': {'value': 'qwe'}, 'exceptions': (ValueError,)},
{'data': {'value': None}, 'return': None},
) # Cases, to test the performance of `.to_representation()`.
to_internal_value_cases = (
{'data': {'data': 123}, 'return': 123.0},
Expand Down Expand Up @@ -949,7 +952,8 @@ class TestJsonField(BaseFieldTestCase):
{'data': {'value': {'123': 123}}, 'return': '{"123": 123}'},
{'data': {'value': {'123': [123, '123']}}, 'return': '{"123": [123, "123"]}'},
{'data': {'value': lambda: None}, 'exceptions': (ValidationError,)},
{'data': {'value': {123: 123}}, 'return': '{"123": 123}'}
{'data': {'value': {123: 123}}, 'return': '{"123": 123}'},
{'data': {'value': None}, 'return': 'null'},
) # Cases, to test the performance of `.to_representation()`.
to_internal_value_cases = (
{'data': {'data': {}}, 'return': {}},
Expand Down Expand Up @@ -998,6 +1002,7 @@ class TestDictField(BaseFieldTestCase):
{'data': {'value': 123}, 'exceptions': (AttributeError,)},
{'data': {'value': lambda: None}, 'exceptions': (AttributeError,)},
{'data': {'value': {123: 123}}, 'return': {'123': 123}},
{'data': {'value': None}, 'return': None},
{'data': {'value': {123: [123]}}, 'params': {'child': IntegerField()}, 'exceptions': (TypeError,)}
) # Cases, to test the performance of `.to_representation()`.
to_internal_value_cases = (
Expand Down