From 00f88384a6fab19e13fd613479a201d6bd019c50 Mon Sep 17 00:00:00 2001 From: mmurashov Date: Thu, 14 Jan 2021 15:20:16 +0300 Subject: [PATCH 1/2] Add utf-8 support for generated formats --- src/drf_yasg/codecs.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/drf_yasg/codecs.py b/src/drf_yasg/codecs.py index dad4e229..af2297ba 100644 --- a/src/drf_yasg/codecs.py +++ b/src/drf_yasg/codecs.py @@ -117,12 +117,12 @@ def _dump_dict(self, spec): :rtype: str""" if self.pretty: - out = json.dumps(spec, indent=4, separators=(',', ': ')) + out = json.dumps(spec, indent=4, separators=(',', ': '), ensure_ascii=False) if out[-1] != '\n': out += '\n' return out else: - return json.dumps(spec) + return json.dumps(spec, ensure_ascii=False) YAML_MAP_TAG = u'tag:yaml.org,2002:map' @@ -199,7 +199,13 @@ def yaml_sane_dump(data, binary): :return: the serialized YAML :rtype: str or bytes """ - return yaml.dump(data, Dumper=SaneYamlDumper, default_flow_style=False, encoding='utf-8' if binary else None) + encoding = None + allow_unicode = None + if binary: + encoding = 'utf-8' + allow_unicode = True + return yaml.dump( + data, Dumper=SaneYamlDumper, default_flow_style=False, encoding=encoding, allow_unicode=allow_unicode) class SaneYamlLoader(yaml.SafeLoader): From 55d2805af41e285165b82c0a9dbd1dca7b123c12 Mon Sep 17 00:00:00 2001 From: Joel Lefkowitz <45922542+JoelLefkowitz@users.noreply.github.com> Date: Thu, 14 Jul 2022 17:36:26 +0100 Subject: [PATCH 2/2] Inline allow_unicode parameter --- src/drf_yasg/codecs.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/drf_yasg/codecs.py b/src/drf_yasg/codecs.py index af2297ba..a5826edf 100644 --- a/src/drf_yasg/codecs.py +++ b/src/drf_yasg/codecs.py @@ -199,13 +199,13 @@ def yaml_sane_dump(data, binary): :return: the serialized YAML :rtype: str or bytes """ - encoding = None - allow_unicode = None - if binary: - encoding = 'utf-8' - allow_unicode = True return yaml.dump( - data, Dumper=SaneYamlDumper, default_flow_style=False, encoding=encoding, allow_unicode=allow_unicode) + data, + Dumper=SaneYamlDumper, + default_flow_style=False, + encoding='utf-8' if binary else None, + allow_unicode=binary + ) class SaneYamlLoader(yaml.SafeLoader):