Fix dedupe logic to work with compact mode for the purl endpoint #49
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.
The dedupe logic in
socketdev/core/dedupe.py
was failing when processing compact format API responses that omit optional fields likecategory
,file
,start
, andend
. The code assumed these fields would always be present, causing KeyError exceptions when accessing them directly withalert["category"]
syntax. Additionally, the consolidation logic was incorrectly adding these missing fields to the output withNone
values, polluting the response with fields that weren't present in the original data.Root Cause
The
alert_key()
andalert_identity()
functions used direct dictionary access (alert["category"]
) instead of safe access (alert.get("category")
) for optional fields. The consolidation logic inconsolidate_and_merge_alerts()
also unconditionally added optional fields to the output dictionary even when they weren't present in the source data, resulting in unwantednull
values in the API response.Fix
alert_key()
method: Changedalert["category"]
toalert.get("category")
to safely handle missing fieldsalert_identity()
function: Applied the same safe access pattern for thecategory
fieldcategory
,file
,start
,end
) in the output if they exist in the original alert data using conditional checks (if "field" in alert
)This ensures that compact format responses maintain their intended structure without unnecessary fields, while still supporting full format responses that include all fields.
Public Changelog
Fixed dedupe logic to properly handle compact API responses that omit optional alert fields