Merge pull request #21 from h-hirokawa/file-field-response
Add support for FileField response.openapi3
commit
06a461ec09
|
|
@ -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
|
||||||
|
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
|
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:
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,13 @@ class ArticleSerializer(serializers.ModelSerializer):
|
||||||
child=serializers.URLField(help_text="but i needed to test these 2 fields somehow"),
|
child=serializers.URLField(help_text="but i needed to test these 2 fields somehow"),
|
||||||
)
|
)
|
||||||
uuid = serializers.UUIDField(help_text="should articles have UUIDs?")
|
uuid = serializers.UUIDField(help_text="should articles have UUIDs?")
|
||||||
|
cover_name = serializers.FileField(use_url=False, source='cover', read_only=True)
|
||||||
|
|
||||||
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',
|
||||||
read_only_fields = ('date_created', 'date_modified')
|
'references', 'uuid', 'cover', 'cover_name')
|
||||||
|
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'}}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -493,6 +493,13 @@ 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
|
||||||
|
cover_name:
|
||||||
|
type: string
|
||||||
|
readOnly: true
|
||||||
Project:
|
Project:
|
||||||
required:
|
required:
|
||||||
- project_name
|
- project_name
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue