Skip to content

Commit

Permalink
fix FilePathField required argument (#8805)
Browse files Browse the repository at this point in the history
  • Loading branch information
radekwlsk authored Jan 4, 2023
1 parent 069c701 commit 89d6ce7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,7 @@ def __init__(self, path, match=None, recursive=False, allow_files=True,
allow_folders=allow_folders, required=required
)
kwargs['choices'] = field.choices
kwargs['required'] = required
super().__init__(**kwargs)


Expand Down
18 changes: 17 additions & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import rest_framework
from rest_framework import exceptions, serializers
from rest_framework.fields import (
BuiltinSignatureError, DjangoImageField, is_simple_callable
BuiltinSignatureError, DjangoImageField, SkipField, empty,
is_simple_callable
)
from tests.models import UUIDForeignKeyTarget

Expand Down Expand Up @@ -2390,6 +2391,21 @@ def test_fully_qualified_when_request_in_context(self):
assert value == 'http://example.com/example.txt'


# Tests for FilePathField.
# --------------------

class TestFilePathFieldRequired:
def test_required_passed_to_both_django_file_path_field_and_base(self):
field = serializers.FilePathField(
path=os.path.abspath(os.path.dirname(__file__)),
required=False,
)
assert "" in field.choices # Django adds empty choice if not required
assert field.required is False
with pytest.raises(SkipField):
field.run_validation(empty)


# Tests for SerializerMethodField.
# --------------------------------

Expand Down

0 comments on commit 89d6ce7

Please sign in to comment.