SchemaJSRenderer renders invalid Javascript

Under Py3 the base64.b64encode() method returns a binary object, which gets rendered as `b'...'` in schema.js. This results in the output becoming:

    var coreJSON = window.atob('b'eyJf...'');

which is invalid Javascript. Because base64 only uses ASCII characters it is safe to decode('ascii') it. Under Py2 this will result in a unicode object, which is fine. Under Py3 it results in a string, which is also fine. This solves the problem and results in a working schema.js output.
This commit is contained in:
Sander Steffann 2017-11-18 19:15:03 +01:00 committed by GitHub
parent 9f66e8badd
commit 9c9d86d7c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -852,7 +852,7 @@ class SchemaJSRenderer(BaseRenderer):
def render(self, data, accepted_media_type=None, renderer_context=None):
codec = coreapi.codecs.CoreJSONCodec()
schema = base64.b64encode(codec.encode(data))
schema = base64.b64encode(codec.encode(data)).decode('ascii')
template = loader.get_template(self.template)
context = {'schema': mark_safe(schema)}