Skip to content

Commit

Permalink
handle default value for SerializerMethodField #422
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Sep 17, 2021
1 parent f43d7d3 commit b136568
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 4 additions & 3 deletions drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,10 @@ def _get_serializer_field_meta(self, field):
if field.allow_null:
meta['nullable'] = True
if field.default is not None and field.default != empty and not callable(field.default):
if isinstance(field, serializers.ModelField):
# Skip coercion for lack of a better solution. ModelField.to_representation() is special
# in that it requires a model instance (which we don't have) instead of a plain value.
if isinstance(field, (serializers.ModelField, serializers.SerializerMethodField)):
# Skip coercion for lack of a better solution. ModelField.to_representation()
# and SerializerMethodField.to_representation() are special in that they require
# a model instance or object (which we don't have) instead of a plain value.
default = field.default
else:
default = field.to_representation(field.default)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2240,7 +2240,7 @@ def view_func(request, format=None):
}


def test_serializer_modelfield_with_default_value(no_warnings):
def test_serializer_modelfield_and_methodfield_with_default_value(no_warnings):
class M8Model(models.Model):
field = models.IntegerField()

Expand All @@ -2249,6 +2249,10 @@ class XSerializer(serializers.ModelSerializer):
model_field=M8Model()._meta.get_field('field'),
default=3
)
field_smf = serializers.SerializerMethodField(default=4)

def get_field_smf(self, obj) -> int:
return 0 # pragma: no cover

class Meta:
model = M8Model
Expand All @@ -2262,6 +2266,9 @@ class XViewset(viewsets.ModelViewSet):
assert schema['components']['schemas']['X']['properties']['field'] == {
'type': 'integer', 'default': 3
}
assert schema['components']['schemas']['X']['properties']['field_smf'] == {
'type': 'integer', 'readOnly': True, 'default': 4
}


def test_literal_dot_in_regex_path(no_warnings):
Expand Down

0 comments on commit b136568

Please sign in to comment.