Skip to content

Commit

Permalink
Fix file fields
Browse files Browse the repository at this point in the history
They are binary on upload, and usually type uri in responses.
  • Loading branch information
jayvdb committed May 26, 2020
1 parent ddeb5bb commit 573cf09
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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

0 comments on commit 573cf09

Please sign in to comment.