Skip to content

Commit

Permalink
make event stdout encoding more resilient to UTF-16 surrogate pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanpetrello committed Aug 17, 2020
1 parent 54f8742 commit c04c5c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions awx/api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
# Django REST Framework
from rest_framework import renderers
from rest_framework.request import override_method
from rest_framework.utils import encoders


class SurrogateEncoder(encoders.JSONEncoder):

def encode(self, obj):
ret = super(SurrogateEncoder, self).encode(obj)
try:
ret.encode()
except UnicodeEncodeError as e:
if 'surrogates not allowed' in e.reason:
ret = ret.encode('utf-8', 'replace').decode()
return ret


class DefaultJSONRenderer(renderers.JSONRenderer):

encoder_class = SurrogateEncoder


class BrowsableAPIRenderer(renderers.BrowsableAPIRenderer):
Expand Down
2 changes: 1 addition & 1 deletion awx/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def IS_TESTING(argv=None):
'awx.api.parsers.JSONParser',
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'awx.api.renderers.DefaultJSONRenderer',
'awx.api.renderers.BrowsableAPIRenderer',
),
'DEFAULT_METADATA_CLASS': 'awx.api.metadata.Metadata',
Expand Down

0 comments on commit c04c5c3

Please sign in to comment.