def get(self, request, spec_format): url_scheme = request._request.META['wsgi.url_scheme'] try: http_host = request._request.META['HTTP_HOST'] except KeyError: http_host = (f"{request._request.META['REMOTE_ADDR']}" f":{request._request.META['SERVER_PORT']}") user_auth_header = request._request.headers.get('Authorization') if user_auth_header: user_token = user_auth_header.replace('Token ', '') from_db = True else: user_token = None from_db = False server = f'{url_scheme}://{http_host}' generator = SchemaGenerator( servers=[server], patterns=self.patterns, user_token=user_token, from_db=from_db, ) schema = generator.get_schema(request=request) if spec_format == 'yaml': spec = OpenAPIRenderer().render(schema, renderer_context={}) return HttpResponse(spec.decode()) return JsonResponse(schema)
def test_openapi_yaml_rendering_without_aliases(self): renderer = OpenAPIRenderer() reused_object = {'test': 'test'} data = { 'o1': reused_object, 'o2': reused_object, } assert ( renderer.render(data) == b'o1:\n test: test\no2:\n test: test\n' or renderer.render(data) == b'o2:\n test: test\no1:\n test: test\n' # py <= 3.5 )
def get_renderer(self, format): return { 'corejson': CoreJSONRenderer(), 'openapi': OpenAPIRenderer(), 'openapi-json': JSONOpenAPIRenderer() }[format]