From ced37a56cbc78fdd0679c76b4d26d6cd8d215536 Mon Sep 17 00:00:00 2001 From: Noam Date: Fri, 3 Jan 2020 15:49:46 +0200 Subject: [PATCH] Avoid outputting callable defaults to schema. (#7105) --- rest_framework/schemas/openapi.py | 2 +- tests/schemas/test_openapi.py | 16 ++++++++++++++++ tests/schemas/views.py | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 134df5043..fe688facc 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -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) diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 622f78cdd..03eb9de7a 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -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' diff --git a/tests/schemas/views.py b/tests/schemas/views.py index f8d143e71..e8307ccbd 100644 --- a/tests/schemas/views.py +++ b/tests/schemas/views.py @@ -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):