Skip to content

fix: pass original value to ParseTypeError in ArrayParameter and MapParameter#3486

Closed
he-yufeng wants to merge 1 commit into
googleapis:mainfrom
he-yufeng:fix/parsetype-error-nil-arg
Closed

fix: pass original value to ParseTypeError in ArrayParameter and MapParameter#3486
he-yufeng wants to merge 1 commit into
googleapis:mainfrom
he-yufeng:fix/parsetype-error-nil-arg

Conversation

@he-yufeng

Copy link
Copy Markdown
Contributor

In Go, a two-result type assertion like x, ok := v.(T) sets x to the zero value of T when the assertion fails — nil for slice and map types. Both ArrayParameter.Parse and MapParameter.Parse were passing this zero value to ParseTypeError instead of the original argument v, so a caller would see an error like:

<nil> not type "array"

instead of something useful that identifies what was actually passed in.

The five other Parse methods in the same file — StringParameter, IntParameter, FloatParameter, BoolParameter, and the integer sub-case — all correctly pass v to ParseTypeError. This PR brings ArrayParameter and MapParameter in line with that pattern.

Changed lines:

// Before
return nil, &ParseTypeError{p.Name, p.Type, arrVal}  // arrVal is nil on failed assertion
return nil, &ParseTypeError{p.Name, p.Type, m}        // m is nil on failed assertion

// After
return nil, &ParseTypeError{p.Name, p.Type, v}
return nil, &ParseTypeError{p.Name, p.Type, v}

Two regression tests are added in TestParseTypeErrorIncludesValue: one for ArrayParameter.Parse("not-an-array") and one for MapParameter.Parse("bad"), each asserting the error message contains the original input value. Both tests fail before this fix and pass after.

@he-yufeng he-yufeng requested a review from a team as a code owner June 22, 2026 09:27

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a bug in ArrayParameter.Parse and MapParameter.Parse where a type assertion failure resulted in ParseTypeError reporting a nil value instead of the original invalid input value. The fix correctly passes the original input v to ParseTypeError. Additionally, a new unit test TestParseTypeErrorIncludesValue has been added to verify this behavior. I have no feedback to provide.

…arameter

After a failed two-result type assertion in Go, the LHS variable is set
to the zero value for its type (nil for slices and maps). Both
ArrayParameter.Parse and MapParameter.Parse were passing this zero value
to ParseTypeError instead of the original `v`, causing error messages to
always report the zero value rather than the actual bad input.

All five other Parse methods in the same file (String, Int, Float, Bool,
and the integer sub-case) correctly pass `v`. This commit aligns
ArrayParameter and MapParameter with that pattern and adds regression
tests that verify the original value appears in the error text.
@he-yufeng he-yufeng force-pushed the fix/parsetype-error-nil-arg branch from 7ed6139 to 855708e Compare June 23, 2026 02:34
@he-yufeng

Copy link
Copy Markdown
Contributor Author

Closing in favor of #3512, which lands the same fix (passing the original v to ParseTypeError in ArrayParameter.Parse and MapParameter.Parse instead of the nil asserted variable) with a tidier table-driven test. No point keeping two open PRs for the same bug.

@he-yufeng he-yufeng closed this Jun 24, 2026
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