From 3a0fba4bdbc326153d4f5a9241c1afad6bb898f7 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Wed, 24 Mar 2021 19:09:33 +0900 Subject: [PATCH] Fix invalid string option --- decode_compile.go | 24 +++++++++++++++++++++++- decode_test.go | 3 --- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/decode_compile.go b/decode_compile.go index 67b2f0b..62f3a61 100644 --- a/decode_compile.go +++ b/decode_compile.go @@ -102,6 +102,28 @@ func decodeCompile(typ *rtype, structName, fieldName string, structTypeToDecoder } } +func isStringTagSupportedType(typ *rtype) bool { + switch { + case rtype_ptrTo(typ).Implements(unmarshalJSONType): + return false + case rtype_ptrTo(typ).Implements(unmarshalTextType): + return false + } + switch typ.Kind() { + case reflect.Map: + return false + case reflect.Slice: + return false + case reflect.Array: + return false + case reflect.Struct: + return false + case reflect.Interface: + return false + } + return true +} + func decodeCompileMapKey(typ *rtype, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) { if rtype_ptrTo(typ).Implements(unmarshalTextType) { return newUnmarshalTextDecoder(rtype_ptrTo(typ), structName, fieldName), nil @@ -416,7 +438,7 @@ func decodeCompileStruct(typ *rtype, structName, fieldName string, structTypeToD } } } else { - if tag.IsString { + if tag.IsString && isStringTagSupportedType(type2rtype(field.Type)) { dec = newWrappedStringDecoder(type2rtype(field.Type), dec, structName, field.Name) } var key string diff --git a/decode_test.go b/decode_test.go index 7d933c2..ab4b9eb 100644 --- a/decode_test.go +++ b/decode_test.go @@ -2514,7 +2514,6 @@ func TestInvalidUnmarshalText(t *testing.T) { } } -/* // Test that string option is ignored for invalid types. // Issue 9812. func TestInvalidStringOption(t *testing.T) { @@ -2532,13 +2531,11 @@ func TestInvalidStringOption(t *testing.T) { if err != nil { t.Fatalf("Marshal: %v", err) } - err = json.Unmarshal(data, &item) if err != nil { t.Fatalf("Unmarshal: %v", err) } } -*/ // Test unmarshal behavior with regards to embedded unexported structs. //