From 5bce4abf5bf18c814e1c5e7a39ad8a88d18eab38 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 14 May 2026 06:47:08 +0000 Subject: [PATCH] Validate JsonAnalysisBuilder root type for clearer list handling Agent-Logs-Url: https://github.com/microsoft/presidio/sessions/438db274-63ab-4a22-af9f-6f2f3b7296d2 Co-authored-by: omri374 <3776619+omri374@users.noreply.github.com> --- .../presidio_structured/analysis_builder.py | 10 ++++++++-- presidio-structured/tests/test_analysis_builder.py | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/presidio-structured/presidio_structured/analysis_builder.py b/presidio-structured/presidio_structured/analysis_builder.py index 27cd9bb54..29a15e754 100644 --- a/presidio-structured/presidio_structured/analysis_builder.py +++ b/presidio-structured/presidio_structured/analysis_builder.py @@ -104,9 +104,15 @@ class JsonAnalysisBuilder(AnalysisBuilder): """ logger.debug("Starting JSON BatchAnalyzer analysis") key_recognizer_result_map = {} - objects_to_analyze = data if isinstance(data, list) else [data] + if not isinstance(data, (dict, list)): + raise ValueError( + "JsonAnalysisBuilder only supports dictionary input " + "or lists of dictionaries." + ) - for json_object in objects_to_analyze: + dict_list = data if isinstance(data, list) else [data] + + for json_object in dict_list: if not isinstance(json_object, dict): raise ValueError( "Each element in the list must be a dictionary. " diff --git a/presidio-structured/tests/test_analysis_builder.py b/presidio-structured/tests/test_analysis_builder.py index 9869446cd..d2bc763c5 100644 --- a/presidio-structured/tests/test_analysis_builder.py +++ b/presidio-structured/tests/test_analysis_builder.py @@ -163,6 +163,11 @@ def test_generate_analysis_json_with_top_level_list_of_objects(json_analysis_bui assert structured_analysis.entity_mapping["email"] == "EMAIL_ADDRESS" +def test_generate_analysis_json_with_invalid_root_type_should_raise(json_analysis_builder): + with pytest.raises(ValueError): + json_analysis_builder.generate_analysis("not a json object") + + def test_analysis_json_when_default_threshold_is_high_then_only_email_passes( sample_json, ):