Skip to content

Commit 3b0844c

Browse files
NRL-1215 Fix possible intermittent related to dict order
1 parent 9a289c7 commit 3b0844c

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

layer/nrlf/core/json_duplicate_checker.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
2-
from typing import Dict, List, Set, Tuple
2+
from collections import OrderedDict
3+
from typing import Dict, List, Tuple
34

45
JsonPrimitive = str | int | float | bool | None
56
type JsonValue = JsonPrimitive | JsonObject | JsonArray
@@ -19,8 +20,8 @@ class DuplicateKeyChecker:
1920
"""
2021

2122
def __init__(self):
22-
self.duplicate_keys: Set[str] = set()
23-
self.duplicate_paths: Set[str] = set()
23+
# Here a list of paths because the same key name could be at different levels
24+
self.duplicate_keys_and_paths: OrderedDict[str, list[str]] = OrderedDict()
2425
# Track keys at each path level to detect duplicates
2526
self.key_registry: Dict[str, Dict[str, bool]] = {}
2627
self.current_duplicate_index: Dict[str, int] = {}
@@ -47,8 +48,8 @@ def check_key(self, key: str, path: List[str]) -> None:
4748
current_level = ".".join(path)
4849
current_keys = self.key_registry.setdefault(current_level, {})
4950
if key in current_keys:
50-
self.duplicate_keys.add(key)
51-
self.duplicate_paths.add(".".join(path + [key]))
51+
duplicate_path = ".".join(path + [key])
52+
self.duplicate_keys_and_paths.setdefault(key, []).append(duplicate_path)
5253
print(f"Found duplicate key: {key} at path: {'.'.join(path + [key])}")
5354
else:
5455
current_keys[key] = True
@@ -101,8 +102,9 @@ def check_duplicate_keys(json_content: str) -> Tuple[List[str], List[str]]:
101102
checker = DuplicateKeyChecker()
102103
checker.traverse_json(parsed_data, ["root"])
103104

104-
duplicates = list(checker.duplicate_keys)
105-
paths = list(checker.duplicate_paths)
105+
duplicates = list(checker.duplicate_keys_and_paths.keys())
106+
# flatten the list of paths
107+
paths = sum(checker.duplicate_keys_and_paths.values(), [])
106108
print("Final duplicates:", duplicates)
107109
print("Final paths:", paths)
108110

layer/nrlf/core/tests/test_json_duplicate_checker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,19 @@ def test_parametrized_nested_arrays(self):
292292
expected_duplicates = self.get_expected_duplicates(depth)
293293
expected_paths = self.get_expected_paths(depth)
294294

295-
print("\nActual duplicates:", sorted(duplicates))
295+
print("\nActual duplicates:", duplicates)
296296
print("Expected duplicates:", expected_duplicates)
297-
print("\nActual paths:", sorted(paths))
297+
print("\nActual paths:", paths)
298298
print("Expected paths:", expected_paths)
299299

300300
self.assertEqual(
301301
sorted(duplicates),
302-
expected_duplicates,
302+
sorted(expected_duplicates),
303303
f"Failed for depth {depth} - duplicates mismatch",
304304
)
305305
self.assertEqual(
306306
sorted(paths),
307-
expected_paths,
307+
sorted(expected_paths),
308308
f"Failed for depth {depth} - paths mismatch",
309309
)
310310
print("=== Test passed for depth", depth, "===\n")

0 commit comments

Comments
 (0)