Ver Fonte

jsonpb: error on scalar enum provided for repeated enums instead of panic

Stan Nelson há 7 anos atrás
pai
commit
64db29d85f
2 ficheiros alterados com 4 adições e 0 exclusões
  1. 3 0
      jsonpb/jsonpb.go
  2. 1 0
      jsonpb/jsonpb_test.go

+ 3 - 0
jsonpb/jsonpb.go

@@ -878,6 +878,9 @@ func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMe
 			target.Set(reflect.New(targetType.Elem()))
 			target = target.Elem()
 		}
+		if targetType.Kind() != reflect.Int32 {
+			return fmt.Errorf("invalid target %q for enum %s", targetType.Kind(), prop.Enum)
+		}
 		target.SetInt(int64(n))
 		return nil
 	}

+ 1 - 0
jsonpb/jsonpb_test.go

@@ -868,6 +868,7 @@ var unmarshalingShouldError = []struct {
 	{"Timestamp containing invalid character", `{"ts": "2014-05-13T16:53:20\U005a"}`, &pb.KnownTypes{}},
 	{"StringValue containing invalid character", `{"str": "\U00004E16\U0000754C"}`, &pb.KnownTypes{}},
 	{"StructValue containing invalid character", `{"str": "\U00004E16\U0000754C"}`, &stpb.Struct{}},
+	{"repeated proto3 enum with non array input", `{"rFunny":"PUNS"}`, &proto3pb.Message{RFunny: []proto3pb.Message_Humour{}}},
 }
 
 func TestUnmarshalingBadInput(t *testing.T) {