Skip to content

Fix panic on missing or non-map query parameter object#278

Open
SebTardif wants to merge 1 commit into
pb33f:mainfrom
SebTardif:fix/query-param-panic
Open

Fix panic on missing or non-map query parameter object#278
SebTardif wants to merge 1 commit into
pb33f:mainfrom
SebTardif:fix/query-param-panic

Conversation

@SebTardif
Copy link
Copy Markdown

Problem

In parameters/query_parameters.go, the helpers.Object case uses a bare type assertion:

encodedObj[params[p].Name].(map[string]interface{})

This panics when:

  1. encodedObj is nil (e.g. content-wrapped parameter with a non-JSON media type skips map initialization)
  2. The key params[p].Name is absent from encodedObj (map lookup returns nil, bare assertion on nil panics)
  3. The value is present but is not a map[string]interface{}

Fix

Replace the bare type assertion with a three-step comma-ok pattern:

if encodedObj == nil {
    break skipValues
}
objVal, objExists := encodedObj[params[p].Name]
if !objExists || objVal == nil {
    break skipValues
}
objMap, mapOk := objVal.(map[string]interface{})
if !mapOk {
    break skipValues
}

This follows the existing control flow: when the value cannot be decoded, we break skipValues (the same pattern used for JSON unmarshal failures a few lines above).

Test

Added TestQueryParamObjectMissingKey_NoPanic which declares an object query parameter with a text/plain content wrapper. This forces the contentWrapped path without JSON decoding, leaving encodedObj nil. Before the fix, this test panics. After the fix, it completes without error.

Replace bare type assertion with comma-ok pattern to prevent panic
when query parameter object key is absent or value is not a map.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 29, 2026

Codecov Report

❌ Patch coverage is 55.55556% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.58%. Comparing base (54cf7b7) to head (d2d6c8e).

Files with missing lines Patch % Lines
parameters/query_parameters.go 55.55% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #278      +/-   ##
==========================================
- Coverage   97.63%   97.58%   -0.06%     
==========================================
  Files          64       64              
  Lines        6991     6999       +8     
==========================================
+ Hits         6826     6830       +4     
- Misses        132      134       +2     
- Partials       33       35       +2     
Flag Coverage Δ
unittests 97.58% <55.55%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant