Skip to content

Ignore derivations of BrowsableAPIRenderer in OpenAPI schema #7497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rest_framework/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def map_renderers(self, path, method):
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
Expand Down
17 changes: 13 additions & 4 deletions tests/schemas/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
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

Expand Down Expand Up @@ -507,9 +509,16 @@ def test_renderer_mapping(self):
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,
Expand All @@ -524,8 +533,8 @@ class View(generics.CreateAPIView):
# 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()
Expand Down