Avoid outputting callable defaults to schema. (#7105)

This commit is contained in:
Noam 2020-01-03 15:49:46 +02:00 committed by Tom Christie
parent 07376f128c
commit ced37a56cb
3 changed files with 18 additions and 1 deletions

View File

@ -393,7 +393,7 @@ class AutoSchema(ViewInspector):
schema['writeOnly'] = True
if field.allow_null:
schema['nullable'] = True
if field.default and field.default != empty: # why don't they use None?!
if field.default and field.default != empty and not callable(field.default): # why don't they use None?!
schema['default'] = field.default
if field.help_text:
schema['description'] = str(field.help_text)

View File

@ -571,6 +571,22 @@ class TestOperationIntrospection(TestCase):
properties = response_schema['items']['properties']
assert properties['hstore']['type'] == 'object'
def test_serializer_callable_default(self):
path = '/'
method = 'GET'
view = create_view(
views.ExampleGenericAPIView,
method,
create_request(path),
)
inspector = AutoSchema()
inspector.view = view
responses = inspector._get_responses(path, method)
response_schema = responses['200']['content']['application/json']['schema']
properties = response_schema['items']['properties']
assert 'default' not in properties['uuid_field']
def test_serializer_validators(self):
path = '/'
method = 'GET'

View File

@ -58,6 +58,7 @@ class ExampleSerializer(serializers.Serializer):
date = serializers.DateField()
datetime = serializers.DateTimeField()
hstore = serializers.HStoreField()
uuid_field = serializers.UUIDField(default=uuid.uuid4)
class ExampleGenericAPIView(generics.GenericAPIView):