Process a top-level JSON list once per record in JsonDataProcessor - #2205
Open
arpitjain099 wants to merge 2 commits into
Open
Process a top-level JSON list once per record in JsonDataProcessor#2205arpitjain099 wants to merge 2 commits into
arpitjain099 wants to merge 2 commits into
Conversation
JsonDataProcessor._process handled the list case inside the per-key loop, so a top-level list was re-processed in full for every key in the entity mapping. Each operator ran N times per value for an N-key mapping. Move the list branch above the loop and return. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes JsonDataProcessor._process so a top-level JSON list (list of records) is processed once per record, instead of re-processing the entire list once per mapped key (which would apply operators multiple times per value and break non-idempotent operators).
Changes:
- Move the
isinstance(data, list)handling above the per-key loop inJsonDataProcessor._processand return early after processing each item once. - Add a regression test using a non-idempotent custom operator to ensure each operator is applied exactly once for top-level list inputs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| presidio-structured/presidio_structured/data/data_processors.py | Processes top-level lists once per item to prevent repeated operator application per mapped key. |
| presidio-structured/tests/data/test_data_transformers.py | Adds a regression test validating single application of operators for top-level list inputs. |
Comment on lines
+213
to
217
| if text_to_operate_on: | ||
| if isinstance(text_to_operate_on, list): | ||
| for text in text_to_operate_on: | ||
| operated_text = self._operate_on_text(text, operator_callable) | ||
| self._set_nested_value(data, keys, operated_text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change Description
JsonDataProcessor._processputs theisinstance(data, list)branch inside the per-key loop:For a top-level list of records the outer loop runs once per mapped key, and each pass re-processes the entire list with the full mapping. So every operator is applied N times per value, where N is the number of keys in the entity mapping and should be 1. The dict path is correct.
The class docstring advertises arbitrary nesting of dictionaries and lists and the list branch is already there, so a top-level array of records is an intended input shape. Idempotent operators (
replace, single-shotmask) hide the problem, which is why the existing tests pass. Non-idempotent ones do not: a custom lambda runs N times,hashbecomes a hash of a hash,encryptdouble-encrypts, and surrogate maps built for de-anonymization end up wrong.The fix moves the list branch above the loop and returns, so each record is visited once with the whole mapping.
Issue reference
None, found while reading the structured processors.
Checklist
Added
TestJsonDataProcessor::test_process_top_level_list_applies_each_operator_once, which uses a non-idempotent custom operator over a two-key mapping. Before the change it producesAlice!!instead ofAlice!; after the change the whole presidio-structured suite is 30 passed.