fix(mapping): support unmarshaling into interface{} fields for mixed types (fix #5721) by Jaymr1031-Z · Pull Request #5749 · zeromicro/go-zero · GitHub
Skip to content

fix(mapping): support unmarshaling into interface{} fields for mixed types (fix #5721) - #5749

Closed
Jaymr1031-Z wants to merge 2 commits into
zeromicro:masterfrom
Jaymr1031-Z:fix/unmarshal-interface-field
Closed

Jaymr1031-Z wants to merge 2 commits into
zeromicro:masterfrom
Jaymr1031-Z:fix/unmarshal-interface-field

Conversation

@Jaymr1031-Z

Copy link
Copy Markdown

Fixes #5721

Problem

httpx.Parse (and mapping.UnmarshalJsonBytes) failed with ype mismatch for field "..." when unmarshaling into a struct field of type interface{} / �ny whose JSON value is a primitive string, number (json.Number), or bool. This occurred because processFieldPrimitive checked ypeKind == valueKind without handling
eflect.Interface.

Solution

  • In core/mapping/unmarshaler.go processFieldPrimitive: Check if ypeKind == reflect.Interface, and if so directly invoke illWithSameType.
  • In setSameKindValue: Handle argetType.Kind() == reflect.Interface with arget.Set(reflect.ValueOf(value)).
  • Added unit test TestUnmarshalInterfaceField covering structs with �ny fields containing mixed strings and numbers.

Copilot AI 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.

Pull request overview

This PR updates go-zero’s core/mapping JSON unmarshaling logic to correctly populate interface{} / any struct fields when the incoming JSON contains mixed primitive types (notably json.Number vs string/bool), aligning behavior with standard encoding/json decoding and fixing the reported type mismatch for field ... failure in httpx.Parse/mapping.UnmarshalJsonBytes.

Changes:

  • Allow primitive values (including json.Number) to be assigned into interface{} fields during unmarshaling.
  • Add a unit test that verifies mixed string/number values can be unmarshaled into any fields.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
core/mapping/unmarshaler.go Adds interface-kind handling for primitive assignment and json.Number paths during field processing.
core/mapping/unmarshaler_test.go Adds coverage for unmarshaling mixed primitive types into any fields.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 648 to +652
switch v := mapValue.(type) {
case json.Number:
if typeKind == reflect.Interface {
return fillWithSameType(fieldType, value, mapValue, opts)
}
@kevwan
kevwan force-pushed the fix/unmarshal-interface-field branch from ca96f7e to 73b70a6 Compare September 5, 2026 05:18
@codecov

codecov Bot commented Sep 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.23529% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
core/mapping/unmarshaler.go 88.23% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@Jaymr1031-Z Jaymr1031-Z closed this by deleting the head repository Oct 23, 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.

httpx.Parse fails to unmarshal interface{} field with mixed string/number values

2 participants