mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-29 04:54:00 +03:00
parent
f9df0dc965
commit
351e0a4a99
|
@ -241,7 +241,7 @@ to the Open API ("Swagger") format:
|
||||||
from openapi_codec import OpenAPICodec
|
from openapi_codec import OpenAPICodec
|
||||||
|
|
||||||
class SwaggerRenderer(renderers.BaseRenderer):
|
class SwaggerRenderer(renderers.BaseRenderer):
|
||||||
media_type = 'application/openapi+json;version=2.0'
|
media_type = 'application/openapi+json'
|
||||||
format = 'swagger'
|
format = 'swagger'
|
||||||
|
|
||||||
def render(self, data, media_type=None, renderer_context=None):
|
def render(self, data, media_type=None, renderer_context=None):
|
||||||
|
|
|
@ -51,9 +51,11 @@ class Response(SimpleTemplateResponse):
|
||||||
@property
|
@property
|
||||||
def rendered_content(self):
|
def rendered_content(self):
|
||||||
renderer = getattr(self, 'accepted_renderer', None)
|
renderer = getattr(self, 'accepted_renderer', None)
|
||||||
|
accepted_media_type = getattr(self, 'accepted_media_type', None)
|
||||||
context = getattr(self, 'renderer_context', None)
|
context = getattr(self, 'renderer_context', None)
|
||||||
|
|
||||||
assert renderer, ".accepted_renderer not set on Response"
|
assert renderer, ".accepted_renderer not set on Response"
|
||||||
|
assert accepted_media_type, ".accepted_media_type not set on Response"
|
||||||
assert context, ".renderer_context not set on Response"
|
assert context, ".renderer_context not set on Response"
|
||||||
context['response'] = self
|
context['response'] = self
|
||||||
|
|
||||||
|
@ -67,7 +69,7 @@ class Response(SimpleTemplateResponse):
|
||||||
content_type = media_type
|
content_type = media_type
|
||||||
self['Content-Type'] = content_type
|
self['Content-Type'] = content_type
|
||||||
|
|
||||||
ret = renderer.render(self.data, media_type, context)
|
ret = renderer.render(self.data, accepted_media_type, context)
|
||||||
if isinstance(ret, six.text_type):
|
if isinstance(ret, six.text_type):
|
||||||
assert charset, (
|
assert charset, (
|
||||||
'renderer returned unicode, and did not specify '
|
'renderer returned unicode, and did not specify '
|
||||||
|
|
|
@ -276,6 +276,8 @@ class DefaultRouter(SimpleRouter):
|
||||||
default_schema_renderers = [renderers.CoreJSONRenderer]
|
default_schema_renderers = [renderers.CoreJSONRenderer]
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
if 'schema_renderers' in kwargs:
|
||||||
|
assert 'schema_title' in kwargs, 'Missing "schema_title" argument.'
|
||||||
self.schema_title = kwargs.pop('schema_title', None)
|
self.schema_title = kwargs.pop('schema_title', None)
|
||||||
self.schema_renderers = kwargs.pop('schema_renderers', self.default_schema_renderers)
|
self.schema_renderers = kwargs.pop('schema_renderers', self.default_schema_renderers)
|
||||||
super(DefaultRouter, self).__init__(*args, **kwargs)
|
super(DefaultRouter, self).__init__(*args, **kwargs)
|
||||||
|
|
|
@ -10,6 +10,11 @@ from rest_framework.test import APIRequestFactory
|
||||||
factory = APIRequestFactory()
|
factory = APIRequestFactory()
|
||||||
|
|
||||||
|
|
||||||
|
class MockOpenAPIRenderer(BaseRenderer):
|
||||||
|
media_type = 'application/openapi+json;version=2.0'
|
||||||
|
format = 'swagger'
|
||||||
|
|
||||||
|
|
||||||
class MockJSONRenderer(BaseRenderer):
|
class MockJSONRenderer(BaseRenderer):
|
||||||
media_type = 'application/json'
|
media_type = 'application/json'
|
||||||
|
|
||||||
|
@ -24,7 +29,7 @@ class NoCharsetSpecifiedRenderer(BaseRenderer):
|
||||||
|
|
||||||
class TestAcceptedMediaType(TestCase):
|
class TestAcceptedMediaType(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.renderers = [MockJSONRenderer(), MockHTMLRenderer()]
|
self.renderers = [MockJSONRenderer(), MockHTMLRenderer(), MockOpenAPIRenderer()]
|
||||||
self.negotiator = DefaultContentNegotiation()
|
self.negotiator = DefaultContentNegotiation()
|
||||||
|
|
||||||
def select_renderer(self, request):
|
def select_renderer(self, request):
|
||||||
|
@ -44,3 +49,9 @@ class TestAcceptedMediaType(TestCase):
|
||||||
request = Request(factory.get('/', HTTP_ACCEPT='application/json; indent=8'))
|
request = Request(factory.get('/', HTTP_ACCEPT='application/json; indent=8'))
|
||||||
accepted_renderer, accepted_media_type = self.select_renderer(request)
|
accepted_renderer, accepted_media_type = self.select_renderer(request)
|
||||||
self.assertEqual(accepted_media_type, 'application/json; indent=8')
|
self.assertEqual(accepted_media_type, 'application/json; indent=8')
|
||||||
|
|
||||||
|
def test_client_specifies_parameter(self):
|
||||||
|
request = Request(factory.get('/', HTTP_ACCEPT='application/openapi+json;version=2.0'))
|
||||||
|
accepted_renderer, accepted_media_type = self.select_renderer(request)
|
||||||
|
self.assertEqual(accepted_media_type, 'application/openapi+json;version=2.0')
|
||||||
|
self.assertEqual(accepted_renderer.format, 'swagger')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user