From 94e5d05caa285180356fa36e946433b5c93745b1 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Wed, 30 Aug 2017 14:06:43 -0400 Subject: [PATCH] Add failing test for #5371 --- tests/test_serializer.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_serializer.py b/tests/test_serializer.py index 91430a193..3044d7c6b 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -411,6 +411,19 @@ class TestDefaultOutput: serializer = self.Serializer(instance) assert serializer.data == {'has_default': 'def', 'has_default_callable': 'ghi', 'no_default': 'abc'} + def test_default_for_source_source(self): + """ + 'default="something"' should be used when a traversed attribute is missing from input. + """ + class Serializer(serializers.Serializer): + traversed = serializers.CharField(default='x', source='traversed.attr') + + assert Serializer({}).data == {'traversed': 'x'} + assert Serializer({'traversed': {}}).data == {'traversed': 'x'} + assert Serializer({'traversed': None}).data == {'traversed': 'x'} + + assert Serializer({'traversed': {'attr': 'abc'}}).data == {'traversed': 'abc'} + class TestCacheSerializerData: def test_cache_serializer_data(self):