Skip to content

Fix FilePathField required argument #8805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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