Merge pull request #353 from orisano/fix/#342

fix: an incompatible behavior on map key decoder
This commit is contained in:
Masaaki Goshima 2022-03-24 16:43:54 +09:00 committed by GitHub
commit 7cb5120ad2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -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)
}
}

View File

@ -154,6 +154,9 @@ func compileMapKey(typ *runtime.Type, structName, fieldName string, structTypeTo
if runtime.PtrTo(typ).Implements(unmarshalTextType) { if runtime.PtrTo(typ).Implements(unmarshalTextType) {
return newUnmarshalTextDecoder(runtime.PtrTo(typ), structName, fieldName), nil 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) dec, err := compile(typ, structName, fieldName, structTypeToDecoder)
if err != nil { if err != nil {
return nil, err return nil, err