Fix panic on missing or non-map query parameter object#278
Conversation
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 Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #278 +/- ##
==========================================
- Coverage 97.63% 97.53% -0.11%
==========================================
Files 64 64
Lines 6991 7026 +35
==========================================
+ Hits 6826 6853 +27
- Misses 132 138 +6
- Partials 33 35 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
two lines missing coverage... you can do it :) |
The nil/type guards prevented the panic but also silently accepted requests where a content-wrapped object parameter could not be decoded into a map. Now each guard emits a QueryParameterCannotBeDecoded validation error before breaking, so the request correctly fails validation instead of passing silently. Add QueryParameterCannotBeDecoded error constructor (modeled after HeaderParameterCannotBeDecoded) and update the test to assert on the returned error. Add a positive test for valid JSON object parameters. Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
Fixed in d002a64. The guards now emit a |
|
Coverage is a big drop-off; need to bring it up! The libs are something I am anal about coverage on. |
… paths Add unit test for QueryParameterCannotBeDecoded error constructor in errors/ package (21 lines, 0% -> 100% per-package coverage). Add integration tests for query parameter object validation: - JSON content-wrapped with invalid property type (schema error path) - Form-encoded valid and invalid objects (non-content-wrapped path) - XML content type (second encodedObj==nil guard path) These bring patch coverage from 19% to ~78% and restore the errors/ package from 98.0% to 98.9%. Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
|
@daveshanley Coverage improved in 069d2e1:
The remaining uncovered lines are the bodies of guards 2 and 3 ( |
Problem
In
parameters/query_parameters.go, thehelpers.Objectcase uses a bare type assertion:This panics when:
encodedObjis nil (e.g. content-wrapped parameter with a non-JSON media type skips map initialization)params[p].Nameis absent fromencodedObj(map lookup returns nil, bare assertion on nil panics)map[string]interface{}Fix
Replace the bare type assertion with a three-step comma-ok pattern:
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_NoPanicwhich declares an object query parameter with atext/plaincontent wrapper. This forces thecontentWrappedpath without JSON decoding, leavingencodedObjnil. Before the fix, this test panics. After the fix, it completes without error.