Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: an incompatible behavior on map key decoder #353

Merged
merged 1 commit into from Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions decode_test.go
Expand Up @@ -3889,3 +3889,21 @@ func TestIssue348(t *testing.T) {
}
}
}

type issue342 string

func (t *issue342) UnmarshalJSON(b []byte) error {
panic("unreachable")
}

func TestIssue342(t *testing.T) {
var v map[issue342]int
in := []byte(`{"a":1}`)
if err := json.Unmarshal(in, &v); err != nil {
t.Errorf("unexpected error: %v", err)
}
expected := 1
if got := v["a"]; got != expected {
t.Errorf("unexpected result: got(%v) != expected(%v)", got, expected)
}
}
3 changes: 3 additions & 0 deletions internal/decoder/compile.go
Expand Up @@ -154,6 +154,9 @@ func compileMapKey(typ *runtime.Type, structName, fieldName string, structTypeTo
if runtime.PtrTo(typ).Implements(unmarshalTextType) {
return newUnmarshalTextDecoder(runtime.PtrTo(typ), structName, fieldName), nil
}
if typ.Kind() == reflect.String {
return newStringDecoder(structName, fieldName), nil
}
dec, err := compile(typ, structName, fieldName, structTypeToDecoder)
if err != nil {
return nil, err
Expand Down