Skip to content

Commit 86806e6

Browse files
nxexoxnxexox
and
nxexox
authored
Added check on None in to_representation methods: (#18)
Co-authored-by: nxexox <[email protected]>
1 parent e36cae4 commit 86806e6

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

docs/release-notes.md

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ Medium version numbers (0.x.0) may include API changes, in line with the [deprec
99
Major version numbers (x.0.0) are reserved for substantial project milestones.
1010

1111

12+
### 0.3.13
13+
14+
**Date:** [2nd April 2020]
15+
16+
* Added check on `None` in `to_representation` methods in:
17+
* ['CharField'][CharField]
18+
* ['IntegerField'][IntegerField]
19+
* ['FloatField'][FloatField]
20+
* ['DictField'][DictField]
21+
1222
### 0.3.12
1323

1424
**Date:** [21th January 2020]
@@ -201,6 +211,9 @@ class TwoSer(Ser):
201211
[ListField]: api-guid/fields.md#-listfield
202212
[JsonField]: api-guid/fields.md#-jsonfield
203213
[DictField]: api-guid/fields.md#-dictfield
214+
[IntegerField]: api-guid/fields.md#-integerfield
215+
[FloatField]: api-guid/fields.md#-floatfield
216+
[CharField]: api-guid/fields.md#-charfield
204217
[SerializerMethodField]: api-guid/fields.md#-serializermethodfield
205218
[InheritSerializers]: api-guid/serializers.md#serializer-inheritance
206219
[RegexValidator]: api-guid/validators.md#regexvalidator

rest_framework/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
__/ |
99
|___/
1010
"""
11-
VERSION = (0, 3, 12)
11+
VERSION = (0, 3, 13)
1212

1313
__title__ = 'Python-Rest-Framework'
1414
__author__ = 'Deys Timofey'

rest_framework/serializers/fields.py

+8
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ def to_representation(self, value):
490490
:rtype: str
491491
492492
"""
493+
if value is None:
494+
return None
493495
return six.text_type(value)
494496

495497

@@ -567,6 +569,8 @@ def to_representation(self, value):
567569
:rtype: int
568570
569571
"""
572+
if value is None:
573+
return value
570574
return int(value)
571575

572576

@@ -642,6 +646,8 @@ def to_representation(self, value):
642646
:rtype: float
643647
644648
"""
649+
if value is None:
650+
return value
645651
return float(value)
646652

647653

@@ -1305,6 +1311,8 @@ def to_representation(self, value):
13051311
:rtype: str
13061312
13071313
"""
1314+
if value is None:
1315+
return value
13081316
return {
13091317
six.text_type(key): self.child._to_representation(val) if val is not None else None
13101318
for key, val in six.iteritems(value)

tests/test_fields.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,8 @@ class CharFieldTest(BaseFieldTestCase):
499499
to_representation_cases = (
500500
{'data': {'value': '123'}, 'return': '123'},
501501
{'data': {'value': 123}, 'return': '123'},
502-
{'data': {'value': 'qwe'}, 'return': 'qwe'}
502+
{'data': {'value': 'qwe'}, 'return': 'qwe'},
503+
{'data': {'value': None}, 'return': None},
503504
) # Cases, to test the performance of `.to_representation()`.
504505
to_internal_value_cases = (
505506
{'data': {'data': '123'}, 'return': '123'},
@@ -560,6 +561,7 @@ class TestIntegerField(BaseFieldTestCase):
560561
{'data': {'value': 123}, 'return': 123},
561562
{'data': {'value': '123'}, 'return': 123},
562563
{'data': {'value': 'qwe'}, 'exceptions': (ValueError,)},
564+
{'data': {'value': None}, 'return': None},
563565
) # Cases, to test the performance of `.to_representation()`.
564566
to_internal_value_cases = (
565567
{'data': {'data': 123}, 'return': 123},
@@ -609,6 +611,7 @@ class TestFloatField(BaseFieldTestCase):
609611
{'data': {'value': 123}, 'return': 123.0},
610612
{'data': {'value': '123'}, 'return': 123.0},
611613
{'data': {'value': 'qwe'}, 'exceptions': (ValueError,)},
614+
{'data': {'value': None}, 'return': None},
612615
) # Cases, to test the performance of `.to_representation()`.
613616
to_internal_value_cases = (
614617
{'data': {'data': 123}, 'return': 123.0},
@@ -949,7 +952,8 @@ class TestJsonField(BaseFieldTestCase):
949952
{'data': {'value': {'123': 123}}, 'return': '{"123": 123}'},
950953
{'data': {'value': {'123': [123, '123']}}, 'return': '{"123": [123, "123"]}'},
951954
{'data': {'value': lambda: None}, 'exceptions': (ValidationError,)},
952-
{'data': {'value': {123: 123}}, 'return': '{"123": 123}'}
955+
{'data': {'value': {123: 123}}, 'return': '{"123": 123}'},
956+
{'data': {'value': None}, 'return': 'null'},
953957
) # Cases, to test the performance of `.to_representation()`.
954958
to_internal_value_cases = (
955959
{'data': {'data': {}}, 'return': {}},
@@ -998,6 +1002,7 @@ class TestDictField(BaseFieldTestCase):
9981002
{'data': {'value': 123}, 'exceptions': (AttributeError,)},
9991003
{'data': {'value': lambda: None}, 'exceptions': (AttributeError,)},
10001004
{'data': {'value': {123: 123}}, 'return': {'123': 123}},
1005+
{'data': {'value': None}, 'return': None},
10011006
{'data': {'value': {123: [123]}}, 'params': {'child': IntegerField()}, 'exceptions': (TypeError,)}
10021007
) # Cases, to test the performance of `.to_representation()`.
10031008
to_internal_value_cases = (

0 commit comments

Comments
 (0)