diff --git a/src/drf_yasg/codecs.py b/src/drf_yasg/codecs.py index 24ec252..89c82f0 100644 --- a/src/drf_yasg/codecs.py +++ b/src/drf_yasg/codecs.py @@ -1,3 +1,5 @@ +from six import binary_type, raise_from, text_type + import copy import json import logging @@ -6,8 +8,6 @@ from collections import OrderedDict from coreapi.compat import force_bytes from ruamel import yaml -from six import binary_type, raise_from, text_type - from . import openapi from .errors import SwaggerValidationError diff --git a/src/drf_yasg/utils.py b/src/drf_yasg/utils.py index b535fff..bf18aa1 100644 --- a/src/drf_yasg/utils.py +++ b/src/drf_yasg/utils.py @@ -375,14 +375,16 @@ def get_consumes(parser_classes): parser_classes = [pc for pc in parser_classes if not issubclass(pc, FileUploadParser)] media_types = [parser.media_type for parser in parser_classes or []] non_form_media_types = [encoding for encoding in media_types if not is_form_media_type(encoding)] - # Because some data to parse could be nested array and are not supported by form media type like multipart/form-data, - # we must be sure to have explicit form media types **only**. + # Because swagger Parameter objects don't support complex data types (nested objects, arrays), + # we can't use those unless we are sure the view *only* accepts form data + # This means that a view won't support file upload in swagger unless it explicitly + # sets its parser classes to include only form parsers if len(non_form_media_types) == 0: return media_types - # Otherwise, enforce a media type like application/json to be able to parse nested array, but it won't be able to - # support file upload... - else: - return non_form_media_types + + # If the form accepts both form data and another type, like json (which is the default config), + # we will render its input as a Schema and thus it file parameters will be read-only + return non_form_media_types def get_produces(renderer_classes): diff --git a/tests/test_schema_generator.py b/tests/test_schema_generator.py index 61dd5a5..4ecf392 100644 --- a/tests/test_schema_generator.py +++ b/tests/test_schema_generator.py @@ -7,11 +7,11 @@ from django.conf.urls import url from django.contrib.postgres import fields as postgres_fields from django.db import models from django.utils.inspect import get_func_args +from django_fake_model import models as fake_models from rest_framework import routers, serializers, viewsets from rest_framework.decorators import api_view from rest_framework.response import Response -from django_fake_model import models as fake_models from drf_yasg import codecs, openapi from drf_yasg.codecs import yaml_sane_load from drf_yasg.errors import SwaggerGenerationError @@ -347,6 +347,7 @@ EXPECTED_DESCRIPTION = """\ You can log in using the pre-existing `admin` user with password `passwordadmin`. """ + def test_multiline_strings(call_generate_swagger): output = call_generate_swagger(format='yaml') print("|\n|".join(output.splitlines()[:20]))