mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 11:04:02 +03:00
Schemas: Handle default=false for boolean fields (#7165)
This commit is contained in:
parent
d21b8eb084
commit
160f912a60
|
@ -378,7 +378,7 @@ class AutoSchema(ViewInspector):
|
|||
schema['writeOnly'] = True
|
||||
if field.allow_null:
|
||||
schema['nullable'] = True
|
||||
if field.default and field.default != empty and not callable(field.default):
|
||||
if field.default is not None and field.default != empty and not callable(field.default):
|
||||
schema['default'] = field.default
|
||||
if field.help_text:
|
||||
schema['description'] = str(field.help_text)
|
||||
|
|
|
@ -70,6 +70,19 @@ class TestFieldMapping(TestCase):
|
|||
data = inspector._map_serializer(Serializer())
|
||||
assert isinstance(data['properties']['text']['description'], str), "description must be str"
|
||||
|
||||
def test_boolean_default_field(self):
|
||||
class Serializer(serializers.Serializer):
|
||||
default_true = serializers.BooleanField(default=True)
|
||||
default_false = serializers.BooleanField(default=False)
|
||||
without_default = serializers.BooleanField()
|
||||
|
||||
inspector = AutoSchema()
|
||||
|
||||
data = inspector._map_serializer(Serializer())
|
||||
assert data['properties']['default_true']['default'] is True, "default must be true"
|
||||
assert data['properties']['default_false']['default'] is False, "default must be false"
|
||||
assert 'default' not in data['properties']['without_default'], "default must not be defined"
|
||||
|
||||
|
||||
@pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.')
|
||||
class TestOperationIntrospection(TestCase):
|
||||
|
|
Loading…
Reference in New Issue
Block a user