From 0bdd537cc454477886fefc9873353b2364d94158 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 12 Oct 2020 17:40:26 +0300 Subject: [PATCH] Ignore derivations of BrowsableAPIRenderer in OpenAPI schema (#7497) * Ignore derivations of BrowsableAPIRenderer in OpenAPI schema * Improve test_renderer_mapping test Co-authored-by: Carlton Gibson --- rest_framework/schemas/openapi.py | 2 +- tests/schemas/test_openapi.py | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 8a8e267e0..4ecb7a65f 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -595,7 +595,7 @@ class AutoSchema(ViewInspector): media_types = [] for renderer in self.view.renderer_classes: # BrowsableAPIRenderer not relevant to OpenAPI spec - if renderer == renderers.BrowsableAPIRenderer: + if issubclass(renderer, renderers.BrowsableAPIRenderer): continue media_types.append(renderer.media_type) return media_types diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 8ea910dc1..542c377b1 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -10,7 +10,9 @@ from rest_framework import filters, generics, pagination, routers, serializers from rest_framework.authtoken.views import obtain_auth_token from rest_framework.compat import uritemplate from rest_framework.parsers import JSONParser, MultiPartParser -from rest_framework.renderers import JSONRenderer, OpenAPIRenderer +from rest_framework.renderers import ( + BaseRenderer, BrowsableAPIRenderer, JSONRenderer, OpenAPIRenderer +) from rest_framework.request import Request from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator @@ -507,9 +509,16 @@ class TestOperationIntrospection(TestCase): path = '/{id}/' method = 'GET' + class CustomBrowsableAPIRenderer(BrowsableAPIRenderer): + media_type = 'image/jpeg' # that's a wild API renderer + + class TextRenderer(BaseRenderer): + media_type = 'text/plain' + format = 'text' + class View(generics.CreateAPIView): serializer_class = views.ExampleSerializer - renderer_classes = [JSONRenderer] + renderer_classes = [JSONRenderer, TextRenderer, BrowsableAPIRenderer, CustomBrowsableAPIRenderer] view = create_view( View, @@ -524,8 +533,8 @@ class TestOperationIntrospection(TestCase): # schema support is there success_response = responses['200'] - assert len(success_response['content'].keys()) == 1 - assert 'application/json' in success_response['content'] + # Check that the API renderers aren't included, but custom renderers are + assert set(success_response['content']) == {'application/json', 'text/plain'} def test_openapi_yaml_rendering_without_aliases(self): renderer = OpenAPIRenderer()