diff --git a/src/drf_yasg/utils.py b/src/drf_yasg/utils.py index 13e7900..e8d0712 100644 --- a/src/drf_yasg/utils.py +++ b/src/drf_yasg/utils.py @@ -4,6 +4,7 @@ from django.core.validators import RegexValidator from django.utils.encoding import force_text from rest_framework import serializers from rest_framework.mixins import RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin +from rest_framework.settings import api_settings from . import openapi from .errors import SwaggerGenerationError @@ -287,7 +288,13 @@ def serializer_field_to_swagger(field, swagger_object_type, definitions=None, ** # swagger 2.0 does not support specifics about file fields, so ImageFile gets no special treatment # OpenAPI 3.0 does support it, so a future implementation could handle this better err = SwaggerGenerationError("parameter of type file is supported only in a formData Parameter") - if swagger_object_type != openapi.Parameter: + if swagger_object_type == openapi.Schema: + # FileField.to_representation returns URL or file name + result = SwaggerType(type=openapi.TYPE_STRING, read_only=True) + if getattr(field, 'use_url', api_settings.UPLOADED_FILES_USE_URL): + result.format = openapi.FORMAT_URI + return result + elif swagger_object_type != openapi.Parameter: raise err # pragma: no cover param = SwaggerType(type=openapi.TYPE_FILE) if param['in'] != openapi.IN_FORM: diff --git a/testproj/articles/serializers.py b/testproj/articles/serializers.py index 1509911..63f0abb 100644 --- a/testproj/articles/serializers.py +++ b/testproj/articles/serializers.py @@ -9,11 +9,13 @@ class ArticleSerializer(serializers.ModelSerializer): child=serializers.URLField(help_text="but i needed to test these 2 fields somehow"), ) uuid = serializers.UUIDField(help_text="should articles have UUIDs?") + cover_name = serializers.FileField(use_url=False, source='cover', read_only=True) class Meta: model = Article - fields = ('title', 'body', 'slug', 'date_created', 'date_modified', 'references', 'uuid') - read_only_fields = ('date_created', 'date_modified') + fields = ('title', 'body', 'slug', 'date_created', 'date_modified', + 'references', 'uuid', 'cover', 'cover_name') + read_only_fields = ('date_created', 'date_modified', 'cover') lookup_field = 'slug' extra_kwargs = {'body': {'help_text': 'body serializer help_text'}} diff --git a/tests/reference.yaml b/tests/reference.yaml index 3b06bf9..ac7f253 100644 --- a/tests/reference.yaml +++ b/tests/reference.yaml @@ -493,6 +493,13 @@ definitions: description: should articles have UUIDs? type: string format: uuid + cover: + type: string + format: uri + readOnly: true + cover_name: + type: string + readOnly: true Project: required: - project_name