From 9d01872495e718e4e601f233625232f5f9fd8d99 Mon Sep 17 00:00:00 2001 From: Emmanuel Cazenave Date: Wed, 19 Oct 2022 13:17:43 +0200 Subject: [PATCH] unflatten : add an escape character (#70474) --- passerelle/utils/json.py | 19 ++++++++++++++++--- tests/test_utils_json.py | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/passerelle/utils/json.py b/passerelle/utils/json.py index 7b8c9401..597d9e1b 100644 --- a/passerelle/utils/json.py +++ b/passerelle/utils/json.py @@ -29,6 +29,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import re from passerelle.utils.validation import is_number @@ -49,10 +50,22 @@ def unflatten(d, separator=FLATTEN_SEPARATOR): # ok d is a dict - def map_digits(l): - return [int(x) if is_number(x) else x for x in l] + def split_key(key): + def map_key(x): + if is_number(x): + return int(x) + elif isinstance(x, str): + return x.replace('%s%s' % (FLATTEN_SEPARATOR, FLATTEN_SEPARATOR), FLATTEN_SEPARATOR) + return x - keys = [(map_digits(key.split(separator)), key) for key in d] + return [ + map_key(x) + for x in re.split( + r'(?