From 67392e97ee05bed9f0ec98beb133b10789cdf5de Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Tue, 26 Jan 2021 12:16:08 +0000 Subject: [PATCH] Fix issue with looping in list merging The last_dest_idx variable should always have been tracking the last index in the source list. The original intention, implemented incorrectly, was to just append any items which source has no item at that index. --- app/utils.py | 4 ++-- tests/app/test_utils.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/utils.py b/app/utils.py index 75f301b72..554ccc83c 100644 --- a/app/utils.py +++ b/app/utils.py @@ -651,9 +651,9 @@ def merge_jsonlike(source, destination): return True def merge_lists(source, destination): - last_dest_idx = len(destination) - 1 + last_src_idx = len(source) - 1 for idx, item in enumerate(destination): - if idx <= last_dest_idx: + if idx <= last_src_idx: # assign destination value if can't be merged into source if merge_items(source[idx], destination[idx]) is False: source[idx] = destination[idx] diff --git a/tests/app/test_utils.py b/tests/app/test_utils.py index b198cf1b0..1f5cd0c8e 100644 --- a/tests/app/test_utils.py +++ b/tests/app/test_utils.py @@ -630,6 +630,8 @@ def test_get_sample_template_returns_template(template_type): ([{"b": "c"}], [{"b": "e"}], [{"b": "e"}]), # if nested dicts in lists have different keys, additive behaviour ([{"b": "c"}], [{"d": {"e": "f"}}], [{"b": "c", "d": {"e": "f"}}]), + # if dicts in destination list but not source, they just get added to end of source + ([{"a": "b"}], [{"a": "b"}, {"a": "b"}, {"c": "d"}], [{"a": "b"}, {"a": "b"}, {"c": "d"}]), # merge a dict with a null object returns that dict (does not work the other way round) ({"a": {"b": "c"}}, None, {"a": {"b": "c"}}), # double nested dicts, new adds new Boolean key: value, additive behaviour