Merge pull request #21 from h-hirokawa/file-field-response

Add support for FileField response.
openapi3
Cristi Vîjdea 2017-12-18 13:16:21 +01:00 committed by GitHub
commit 06a461ec09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 3 deletions

View File

@ -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:

View File

@ -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'}}

View File

@ -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