Add support for serializers.FileField response.

openapi3
h-hirokawa 2017-12-18 19:03:40 +09:00
parent a2c21539f7
commit 8a0d5a964d
3 changed files with 14 additions and 3 deletions

View File

@ -4,6 +4,7 @@ from django.core.validators import RegexValidator
from django.utils.encoding import force_text from django.utils.encoding import force_text
from rest_framework import serializers from rest_framework import serializers
from rest_framework.mixins import RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin from rest_framework.mixins import RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin
from rest_framework.settings import api_settings
from . import openapi from . import openapi
from .errors import SwaggerGenerationError 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 # 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 # 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") 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
if getattr(field, 'use_url', api_settings.UPLOADED_FILES_USE_URL):
return SwaggerType(type=openapi.TYPE_STRING, format=openapi.FORMAT_URI)
else:
return SwaggerType(type=openapi.TYPE_STRING)
elif swagger_object_type != openapi.Parameter:
raise err # pragma: no cover raise err # pragma: no cover
param = SwaggerType(type=openapi.TYPE_FILE) param = SwaggerType(type=openapi.TYPE_FILE)
if param['in'] != openapi.IN_FORM: if param['in'] != openapi.IN_FORM:

View File

@ -12,8 +12,8 @@ class ArticleSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Article model = Article
fields = ('title', 'body', 'slug', 'date_created', 'date_modified', 'references', 'uuid') fields = ('title', 'body', 'slug', 'date_created', 'date_modified', 'references', 'uuid', 'cover')
read_only_fields = ('date_created', 'date_modified') read_only_fields = ('date_created', 'date_modified', 'cover')
lookup_field = 'slug' lookup_field = 'slug'
extra_kwargs = {'body': {'help_text': 'body serializer help_text'}} extra_kwargs = {'body': {'help_text': 'body serializer help_text'}}

View File

@ -493,6 +493,10 @@ definitions:
description: should articles have UUIDs? description: should articles have UUIDs?
type: string type: string
format: uuid format: uuid
cover:
type: string
format: uri
readOnly: true
Project: Project:
required: required:
- project_name - project_name