summaryrefslogtreecommitdiff
path: root/src/mailman/rest/helpers.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/mailman/rest/helpers.py')
-rw-r--r--src/mailman/rest/helpers.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/mailman/rest/helpers.py b/src/mailman/rest/helpers.py
index 34fff3ba3..9ab1f434e 100644
--- a/src/mailman/rest/helpers.py
+++ b/src/mailman/rest/helpers.py
@@ -21,6 +21,7 @@ import json
import falcon
import hashlib
+from contextlib import suppress
from datetime import datetime, timedelta
from enum import Enum
from lazr.config import as_boolean
@@ -46,9 +47,20 @@ class ExtendedEncoder(json.JSONEncoder):
# It's up to the decoding validator to associate this name with
# the right Enum class.
return obj.name
+ elif isinstance(obj, bytes):
+ return bytes_to_str(obj)
return super().default(obj)
+def bytes_to_str(value):
+ # Convert a string to unicode when the encoding is not declared.
+ if not isinstance(value, bytes):
+ return value
+ for encoding in ('ascii', 'utf-8', 'raw_unicode_escape'):
+ with suppress(UnicodeDecodeError):
+ return value.decode(encoding)
+
+
@public
def etag(resource):
"""Calculate the etag and return a JSON representation.