Skip to content

fix: recursively merge and deduplicate database_specific maps and arrays#1317

Open
jeffrey-theog06 wants to merge 1 commit into
ossf:mainfrom
jeffrey-theog06:fix-database-specific-deduplication
Open

fix: recursively merge and deduplicate database_specific maps and arrays#1317
jeffrey-theog06 wants to merge 1 commit into
ossf:mainfrom
jeffrey-theog06:fix-database-specific-deduplication

Conversation

@jeffrey-theog06

Copy link
Copy Markdown

Context / Problem

Addresses [Issue #1315]
(#1315)
(Point 2).

When merging reports (such as multiple AWS Inspector reports or updates), duplicate entries inside lists in nested database_specific fields (like cwes list of objects or nested lists in indicators) were being preserved, causing duplicated data in the final merged OSV JSON files.

Furthermore, if a nested object like database_specific.indicators existed in both reports, merging them would silently discard keys that already existed, rather than merging their contents recursively.

Solution

-> Refactored combineDatabaseSpecific in internal/report/merge.go to recursively traverse and merge maps, and append/deduplicate slices.
-> Added helper function deduplicateSlice utilizing reflect.DeepEqual to correctly and generically deduplicate arrays of any types (including complex structs/dictionaries like cwes).
-> Added helper function sanitizeAndDeduplicate to ensure initial reports are deduplicated.
-> Updated TestMerge_AffectedDatabaseSpecific to expect duplicate-free arrays.
-> Added new unit test cases covering recursive merges and nested object list deduplication (TestMerge_AffectedDatabaseSpecific_Recursive, TestMerge_AffectedDatabaseSpecific_DeduplicateObjects).

Verification

All unit tests in internal/report/ were successfully updated and extended to cover nested structure merging and deduplication.

Signed-off-by: Jeffrey Shalom <jeffshalom06@gmail.com>
@jeffrey-theog06 jeffrey-theog06 force-pushed the fix-database-specific-deduplication branch from 9b03b60 to 33c8357 Compare June 17, 2026 06:13
@calebbrown

Copy link
Copy Markdown
Contributor

Hi, I appreciate your interest in contributing.

However a decision about how to solve this problem has not been made.

Recursing through an arbitrary JSON object tree and collapsing duplicate entries without understanding the semantic context of those entries may have unintended consequences that need to be considered.

@jeffrey-theog06

Copy link
Copy Markdown
Author

Thank you for the feedback Mr @calebbrown ! That is a very valid architectural concern.

The intent behind this PR was to address the specific issue of duplicate CWE objects and indicators (like domains/IPs) that get generated during ingestion/merging. Currently, the implementation only recurses within the database_specific map, but you are absolutely right that a completely generic recursive merge might have unintended consequences if other custom fields need to preserve duplicates.

If you prefer a more conservative approach, we can easily modify this PR to:

  1. Restrict the deduplication strictly to known fields (like cwes and the lists under indicators).
  2. Or, we can pause this PR and wait until the team decides on the preferred design/strategy for handling these fields.

Let me know how you'd like to proceed, and I'd be happy to adapt the code to match the preferred approach!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants