Skip to content
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

Fix file fields #69

Merged
merged 1 commit into from
May 27, 2020
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
12 changes: 10 additions & 2 deletions drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,16 @@ def _map_serializer_field(self, field, direction):
return append_meta(content, meta)

if isinstance(field, serializers.FileField):
# TODO returns filename. but does it accept binary data on upload?
return append_meta(build_basic_type(OpenApiTypes.STR), meta)
if direction == 'response':
use_url = getattr(field, 'use_url', api_settings.UPLOADED_FILES_USE_URL)
if use_url:
return append_meta(build_basic_type(OpenApiTypes.URI), meta)
else:
return append_meta(build_basic_type(OpenApiTypes.STR), meta)
else:
content = build_basic_type(OpenApiTypes.STR)
content['format'] = 'binary'
return append_meta(content, meta)

if isinstance(field, serializers.SerializerMethodField):
method = getattr(field.parent, field.method_name)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,10 @@ components:
minimum: -1000
field_file:
type: string
format: uri
field_img:
type: string
format: uri
field_date:
type: string
format: date
Expand Down