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 <carlton.gibson@noumenal.es>
This commit is contained in:
Aarni Koskela 2020-10-12 17:40:26 +03:00 committed by GitHub
parent eff97efa28
commit 0bdd537cc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View File

@ -595,7 +595,7 @@ class AutoSchema(ViewInspector):
media_types = [] media_types = []
for renderer in self.view.renderer_classes: for renderer in self.view.renderer_classes:
# BrowsableAPIRenderer not relevant to OpenAPI spec # BrowsableAPIRenderer not relevant to OpenAPI spec
if renderer == renderers.BrowsableAPIRenderer: if issubclass(renderer, renderers.BrowsableAPIRenderer):
continue continue
media_types.append(renderer.media_type) media_types.append(renderer.media_type)
return media_types return media_types

View File

@ -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.authtoken.views import obtain_auth_token
from rest_framework.compat import uritemplate from rest_framework.compat import uritemplate
from rest_framework.parsers import JSONParser, MultiPartParser 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.request import Request
from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator
@ -507,9 +509,16 @@ class TestOperationIntrospection(TestCase):
path = '/{id}/' path = '/{id}/'
method = 'GET' 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): class View(generics.CreateAPIView):
serializer_class = views.ExampleSerializer serializer_class = views.ExampleSerializer
renderer_classes = [JSONRenderer] renderer_classes = [JSONRenderer, TextRenderer, BrowsableAPIRenderer, CustomBrowsableAPIRenderer]
view = create_view( view = create_view(
View, View,
@ -524,8 +533,8 @@ class TestOperationIntrospection(TestCase):
# schema support is there # schema support is there
success_response = responses['200'] success_response = responses['200']
assert len(success_response['content'].keys()) == 1 # Check that the API renderers aren't included, but custom renderers are
assert 'application/json' in success_response['content'] assert set(success_response['content']) == {'application/json', 'text/plain'}
def test_openapi_yaml_rendering_without_aliases(self): def test_openapi_yaml_rendering_without_aliases(self):
renderer = OpenAPIRenderer() renderer = OpenAPIRenderer()