Skip to content

Commit ced37a5

Browse files
noamkushlovelydinosaur
authored andcommitted
Avoid outputting callable defaults to schema. (#7105)
1 parent 07376f1 commit ced37a5

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

rest_framework/schemas/openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ def _map_serializer(self, serializer):
393393
schema['writeOnly'] = True
394394
if field.allow_null:
395395
schema['nullable'] = True
396-
if field.default and field.default != empty: # why don't they use None?!
396+
if field.default and field.default != empty and not callable(field.default): # why don't they use None?!
397397
schema['default'] = field.default
398398
if field.help_text:
399399
schema['description'] = str(field.help_text)

tests/schemas/test_openapi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,22 @@ def test_serializer_hstorefield(self):
571571
properties = response_schema['items']['properties']
572572
assert properties['hstore']['type'] == 'object'
573573

574+
def test_serializer_callable_default(self):
575+
path = '/'
576+
method = 'GET'
577+
view = create_view(
578+
views.ExampleGenericAPIView,
579+
method,
580+
create_request(path),
581+
)
582+
inspector = AutoSchema()
583+
inspector.view = view
584+
585+
responses = inspector._get_responses(path, method)
586+
response_schema = responses['200']['content']['application/json']['schema']
587+
properties = response_schema['items']['properties']
588+
assert 'default' not in properties['uuid_field']
589+
574590
def test_serializer_validators(self):
575591
path = '/'
576592
method = 'GET'

tests/schemas/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class ExampleSerializer(serializers.Serializer):
5858
date = serializers.DateField()
5959
datetime = serializers.DateTimeField()
6060
hstore = serializers.HStoreField()
61+
uuid_field = serializers.UUIDField(default=uuid.uuid4)
6162

6263

6364
class ExampleGenericAPIView(generics.GenericAPIView):

0 commit comments

Comments
 (0)