From b5aec62d3ed33b10b4179b07628d362d0957d1da Mon Sep 17 00:00:00 2001 From: Walter Schulze Date: Mon, 18 Jul 2016 13:49:43 +0200 Subject: [PATCH] merge 001690d39bd620847bb265d93a7c5e1bd3737308 and 62e4364d64b32762febb61f2c88c0a29bc49a225 from golang/protobuf --- jsonpb/jsonpb.go | 65 +++++++-- jsonpb/jsonpb_test.go | 139 ++++++++++---------- jsonpb/jsonpb_test_proto/test_objects.pb.go | 131 ++++++++++-------- jsonpb/jsonpb_test_proto/test_objects.proto | 1 + proto/text.go | 3 +- 5 files changed, 206 insertions(+), 133 deletions(-) diff --git a/jsonpb/jsonpb.go b/jsonpb/jsonpb.go index b9684fd858..07fefd64d7 100644 --- a/jsonpb/jsonpb.go +++ b/jsonpb/jsonpb.go @@ -65,6 +65,9 @@ type Marshaler struct { // value, and for newlines to be appear between fields and array // elements. Indent string + + // Whether to use the original (.proto) name for fields. + OrigName bool } // Marshal marshals a protocol buffer into JSON. @@ -145,7 +148,7 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent string value = sv.Field(0) valueField = sv.Type().Field(0) } - prop := jsonProperties(valueField) + prop := jsonProperties(valueField, m.OrigName) if !firstField { m.writeSep(out) } @@ -194,7 +197,7 @@ func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent string value := reflect.ValueOf(ext) var prop proto.Properties prop.Parse(desc.Tag) - prop.OrigName = fmt.Sprintf("[%s]", desc.Name) + prop.JSONName = fmt.Sprintf("[%s]", desc.Name) if !firstField { m.writeSep(out) } @@ -229,7 +232,7 @@ func (m *Marshaler) marshalField(out *errWriter, prop *proto.Properties, v refle out.write(m.Indent) } out.write(`"`) - out.write(prop.OrigName) + out.write(prop.JSONName) out.write(`":`) if m.Indent != "" { out.write(" ") @@ -439,19 +442,38 @@ func unmarshalValue(target reflect.Value, inputValue json.RawMessage) error { return err } + consumeField := func(prop *proto.Properties) (json.RawMessage, bool) { + // Be liberal in what names we accept; both orig_name and camelName are okay. + fieldNames := acceptedJSONFieldNames(prop) + + vOrig, okOrig := jsonFields[fieldNames.orig] + vCamel, okCamel := jsonFields[fieldNames.camel] + if !okOrig && !okCamel { + return nil, false + } + // If, for some reason, both are present in the data, favour the camelName. + var raw json.RawMessage + if okOrig { + raw = vOrig + delete(jsonFields, fieldNames.orig) + } + if okCamel { + raw = vCamel + delete(jsonFields, fieldNames.camel) + } + return raw, true + } + sprops := proto.GetProperties(targetType) for i := 0; i < target.NumField(); i++ { ft := target.Type().Field(i) if strings.HasPrefix(ft.Name, "XXX_") { continue } - fieldName := jsonProperties(ft).OrigName - - valueForField, ok := jsonFields[fieldName] + valueForField, ok := consumeField(sprops.Prop[i]) if !ok { continue } - delete(jsonFields, fieldName) // Handle enums, which have an underlying type of int32, // and may appear as strings. We do this while handling @@ -475,20 +497,22 @@ func unmarshalValue(target reflect.Value, inputValue json.RawMessage) error { f.SetInt(int64(n)) continue } - if err := unmarshalValue(target.Field(i), valueForField); err != nil { return err } } // Check for any oneof fields. - for fname, raw := range jsonFields { - if oop, ok := sprops.OneofTypes[fname]; ok { + if len(jsonFields) > 0 { + for _, oop := range sprops.OneofTypes { + raw, ok := consumeField(oop.Prop) + if !ok { + continue + } nv := reflect.New(oop.Type.Elem()) target.Field(oop.Field).Set(nv) if err := unmarshalValue(nv.Elem().Field(0), raw); err != nil { return err } - delete(jsonFields, fname) } } if len(jsonFields) > 0 { @@ -577,13 +601,28 @@ func unmarshalValue(target reflect.Value, inputValue json.RawMessage) error { return json.Unmarshal(inputValue, target.Addr().Interface()) } -// jsonProperties returns parsed proto.Properties for the field. -func jsonProperties(f reflect.StructField) *proto.Properties { +// jsonProperties returns parsed proto.Properties for the field and corrects JSONName attribute. +func jsonProperties(f reflect.StructField, origName bool) *proto.Properties { var prop proto.Properties prop.Init(f.Type, f.Name, f.Tag.Get("protobuf"), &f) + if origName || prop.JSONName == "" { + prop.JSONName = prop.OrigName + } return &prop } +type fieldNames struct { + orig, camel string +} + +func acceptedJSONFieldNames(prop *proto.Properties) fieldNames { + opts := fieldNames{orig: prop.OrigName, camel: prop.OrigName} + if prop.JSONName != "" { + opts.camel = prop.JSONName + } + return opts +} + // extendableProto is an interface implemented by any protocol buffer that may be extended. type extendableProto interface { proto.Message diff --git a/jsonpb/jsonpb_test.go b/jsonpb/jsonpb_test.go index ca7c820799..436f5a49b7 100644 --- a/jsonpb/jsonpb_test.go +++ b/jsonpb/jsonpb_test.go @@ -63,33 +63,33 @@ var ( } simpleObjectJSON = `{` + - `"o_bool":true,` + - `"o_int32":-32,` + - `"o_int64":"-6400000000",` + - `"o_uint32":32,` + - `"o_uint64":"6400000000",` + - `"o_sint32":-13,` + - `"o_sint64":"-2600000000",` + - `"o_float":3.14,` + - `"o_double":6.02214179e+23,` + - `"o_string":"hello \"there\"",` + - `"o_bytes":"YmVlcCBib29w",` + - `"o_cast_bytes":"d293"` + + `"oBool":true,` + + `"oInt32":-32,` + + `"oInt64":"-6400000000",` + + `"oUint32":32,` + + `"oUint64":"6400000000",` + + `"oSint32":-13,` + + `"oSint64":"-2600000000",` + + `"oFloat":3.14,` + + `"oDouble":6.02214179e+23,` + + `"oString":"hello \"there\"",` + + `"oBytes":"YmVlcCBib29w",` + + `"oCastBytes":"d293"` + `}` simpleObjectPrettyJSON = `{ - "o_bool": true, - "o_int32": -32, - "o_int64": "-6400000000", - "o_uint32": 32, - "o_uint64": "6400000000", - "o_sint32": -13, - "o_sint64": "-2600000000", - "o_float": 3.14, - "o_double": 6.02214179e+23, - "o_string": "hello \"there\"", - "o_bytes": "YmVlcCBib29w", - "o_cast_bytes": "d293" + "oBool": true, + "oInt32": -32, + "oInt64": "-6400000000", + "oUint32": 32, + "oUint64": "6400000000", + "oSint32": -13, + "oSint64": "-2600000000", + "oFloat": 3.14, + "oDouble": 6.02214179e+23, + "oString": "hello \"there\"", + "oBytes": "YmVlcCBib29w", + "oCastBytes": "d293" }` repeatsObject = &pb.Repeats{ @@ -107,65 +107,65 @@ var ( } repeatsObjectJSON = `{` + - `"r_bool":[true,false,true],` + - `"r_int32":[-3,-4,-5],` + - `"r_int64":["-123456789","-987654321"],` + - `"r_uint32":[1,2,3],` + - `"r_uint64":["6789012345","3456789012"],` + - `"r_sint32":[-1,-2,-3],` + - `"r_sint64":["-6789012345","-3456789012"],` + - `"r_float":[3.14,6.28],` + - `"r_double":[2.99792458e+08,6.62606957e-34],` + - `"r_string":["happy","days"],` + - `"r_bytes":["c2tpdHRsZXM=","bSZtJ3M="]` + + `"rBool":[true,false,true],` + + `"rInt32":[-3,-4,-5],` + + `"rInt64":["-123456789","-987654321"],` + + `"rUint32":[1,2,3],` + + `"rUint64":["6789012345","3456789012"],` + + `"rSint32":[-1,-2,-3],` + + `"rSint64":["-6789012345","-3456789012"],` + + `"rFloat":[3.14,6.28],` + + `"rDouble":[2.99792458e+08,6.62606957e-34],` + + `"rString":["happy","days"],` + + `"rBytes":["c2tpdHRsZXM=","bSZtJ3M="]` + `}` repeatsObjectPrettyJSON = `{ - "r_bool": [ + "rBool": [ true, false, true ], - "r_int32": [ + "rInt32": [ -3, -4, -5 ], - "r_int64": [ + "rInt64": [ "-123456789", "-987654321" ], - "r_uint32": [ + "rUint32": [ 1, 2, 3 ], - "r_uint64": [ + "rUint64": [ "6789012345", "3456789012" ], - "r_sint32": [ + "rSint32": [ -1, -2, -3 ], - "r_sint64": [ + "rSint64": [ "-6789012345", "-3456789012" ], - "r_float": [ + "rFloat": [ 3.14, 6.28 ], - "r_double": [ + "rDouble": [ 2.99792458e+08, 6.62606957e-34 ], - "r_string": [ + "rString": [ "happy", "days" ], - "r_bytes": [ + "rBytes": [ "c2tpdHRsZXM=", "bSZtJ3M=" ] @@ -185,46 +185,46 @@ var ( } complexObjectJSON = `{"color":"GREEN",` + - `"r_color":["RED","GREEN","BLUE"],` + - `"simple":{"o_int32":-32},` + - `"r_simple":[{"o_int32":-32},{"o_int64":"25"}],` + - `"repeats":{"r_string":["roses","red"]},` + - `"r_repeats":[{"r_string":["roses","red"]},{"r_string":["violets","blue"]}]` + + `"rColor":["RED","GREEN","BLUE"],` + + `"simple":{"oInt32":-32},` + + `"rSimple":[{"oInt32":-32},{"oInt64":"25"}],` + + `"repeats":{"rString":["roses","red"]},` + + `"rRepeats":[{"rString":["roses","red"]},{"rString":["violets","blue"]}]` + `}` complexObjectPrettyJSON = `{ "color": "GREEN", - "r_color": [ + "rColor": [ "RED", "GREEN", "BLUE" ], "simple": { - "o_int32": -32 + "oInt32": -32 }, - "r_simple": [ + "rSimple": [ { - "o_int32": -32 + "oInt32": -32 }, { - "o_int64": "25" + "oInt64": "25" } ], "repeats": { - "r_string": [ + "rString": [ "roses", "red" ] }, - "r_repeats": [ + "rRepeats": [ { - "r_string": [ + "rString": [ "roses", "red" ] }, { - "r_string": [ + "rString": [ "violets", "blue" ] @@ -238,7 +238,7 @@ var ( colorListPrettyJSON = `{ "color": 1000, - "r_color": [ + "rColor": [ "RED" ] }` @@ -309,12 +309,14 @@ var marshalingTests = []struct { `{"buggy":{"1234":"yup"}}`}, {"map", marshaler, &pb.Mappy{Booly: map[bool]bool{false: true}}, `{"booly":{"false":true}}`}, {"proto2 map", marshaler, &pb.Maps{MInt64Str: map[int64]string{213: "cat"}}, - `{"m_int64_str":{"213":"cat"}}`}, + `{"mInt64Str":{"213":"cat"}}`}, {"proto2 map", marshaler, &pb.Maps{MBoolSimple: map[bool]*pb.Simple{true: {OInt32: proto.Int32(1)}}}, - `{"m_bool_simple":{"true":{"o_int32":1}}}`}, + `{"mBoolSimple":{"true":{"oInt32":1}}}`}, {"oneof, not set", marshaler, &pb.MsgWithOneof{}, `{}`}, - {"oneof, set", marshaler, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Title{Title: "Grand Poobah"}}, `{"title":"Grand Poobah"}`}, + {"oneof, set", marshaler, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Title{"Grand Poobah"}}, `{"title":"Grand Poobah"}`}, + {"force orig_name", Marshaler{OrigName: true}, &pb.Simple{OInt32: proto.Int32(4)}, + `{"o_int32":4}`}, {"proto2 extension", marshaler, realNumber, realNumberJSON}, } @@ -347,19 +349,22 @@ var unmarshalingTests = []struct { {"unknown enum value object", "{\n \"color\": 1000,\n \"r_color\": [\n \"RED\"\n ]\n}", &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}}, - {"unquoted int64 object", `{"o_int64":-314}`, &pb.Simple{OInt64: proto.Int64(-314)}}, - {"unquoted uint64 object", `{"o_uint64":123}`, &pb.Simple{OUint64: proto.Uint64(123)}}, + {"unquoted int64 object", `{"oInt64":-314}`, &pb.Simple{OInt64: proto.Int64(-314)}}, + {"unquoted uint64 object", `{"oUint64":123}`, &pb.Simple{OUint64: proto.Uint64(123)}}, {"map", `{"nummy":{"1":2,"3":4}}`, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}}, {"map", `{"strry":{"\"one\"":"two","three":"four"}}`, &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}}}, {"map", `{"objjy":{"1":{"dub":1}}}`, &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}}, {"oneof", `{"salary":31000}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Salary{Salary: 31000}}}, + {"oneof spec name", `{"country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{Country: "Australia"}}}, + {"oneof orig_name", `{"Country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{Country: "Australia"}}}, + {"orig_name input", `{"o_bool":true}`, &pb.Simple{OBool: proto.Bool(true)}}, + {"camelName input", `{"oBool":true}`, &pb.Simple{OBool: proto.Bool(true)}}, } func TestUnmarshaling(t *testing.T) { for _, tt := range unmarshalingTests { // Make a new instance of the type of our expected object. p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message) - err := UnmarshalString(tt.json, p) if err != nil { t.Error(err) diff --git a/jsonpb/jsonpb_test_proto/test_objects.pb.go b/jsonpb/jsonpb_test_proto/test_objects.pb.go index cda24f3a2d..2104b46478 100644 --- a/jsonpb/jsonpb_test_proto/test_objects.pb.go +++ b/jsonpb/jsonpb_test_proto/test_objects.pb.go @@ -343,6 +343,7 @@ type MsgWithOneof struct { // Types that are valid to be assigned to Union: // *MsgWithOneof_Title // *MsgWithOneof_Salary + // *MsgWithOneof_Country Union isMsgWithOneof_Union `protobuf_oneof:"union"` XXX_unrecognized []byte `json:"-"` } @@ -362,9 +363,13 @@ type MsgWithOneof_Title struct { type MsgWithOneof_Salary struct { Salary int64 `protobuf:"varint,2,opt,name=salary,oneof"` } +type MsgWithOneof_Country struct { + Country string `protobuf:"bytes,3,opt,name=Country,json=country,oneof"` +} -func (*MsgWithOneof_Title) isMsgWithOneof_Union() {} -func (*MsgWithOneof_Salary) isMsgWithOneof_Union() {} +func (*MsgWithOneof_Title) isMsgWithOneof_Union() {} +func (*MsgWithOneof_Salary) isMsgWithOneof_Union() {} +func (*MsgWithOneof_Country) isMsgWithOneof_Union() {} func (m *MsgWithOneof) GetUnion() isMsgWithOneof_Union { if m != nil { @@ -387,11 +392,19 @@ func (m *MsgWithOneof) GetSalary() int64 { return 0 } +func (m *MsgWithOneof) GetCountry() string { + if x, ok := m.GetUnion().(*MsgWithOneof_Country); ok { + return x.Country + } + return "" +} + // XXX_OneofFuncs is for the internal use of the proto package. func (*MsgWithOneof) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{}) { return _MsgWithOneof_OneofMarshaler, _MsgWithOneof_OneofUnmarshaler, _MsgWithOneof_OneofSizer, []interface{}{ (*MsgWithOneof_Title)(nil), (*MsgWithOneof_Salary)(nil), + (*MsgWithOneof_Country)(nil), } } @@ -405,6 +418,9 @@ func _MsgWithOneof_OneofMarshaler(msg proto.Message, b *proto.Buffer) error { case *MsgWithOneof_Salary: _ = b.EncodeVarint(2<<3 | proto.WireVarint) _ = b.EncodeVarint(uint64(x.Salary)) + case *MsgWithOneof_Country: + _ = b.EncodeVarint(3<<3 | proto.WireBytes) + _ = b.EncodeStringBytes(x.Country) case nil: default: return fmt.Errorf("MsgWithOneof.Union has unexpected type %T", x) @@ -429,6 +445,13 @@ func _MsgWithOneof_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.B x, err := b.DecodeVarint() m.Union = &MsgWithOneof_Salary{int64(x)} return true, err + case 3: // union.Country + if wire != proto.WireBytes { + return true, proto.ErrInternalBadWireType + } + x, err := b.DecodeStringBytes() + m.Union = &MsgWithOneof_Country{x} + return true, err default: return false, nil } @@ -445,6 +468,10 @@ func _MsgWithOneof_OneofSizer(msg proto.Message) (n int) { case *MsgWithOneof_Salary: n += proto.SizeVarint(2<<3 | proto.WireVarint) n += proto.SizeVarint(uint64(x.Salary)) + case *MsgWithOneof_Country: + n += proto.SizeVarint(3<<3 | proto.WireBytes) + n += proto.SizeVarint(uint64(len(x.Country))) + n += len(x.Country) case nil: default: panic(fmt.Sprintf("proto: unexpected type %T in oneof", x)) @@ -546,54 +573,54 @@ func init() { } var fileDescriptorTestObjects = []byte{ - // 769 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x74, 0x94, 0x51, 0x6e, 0xd3, 0x4a, - 0x14, 0x86, 0x1b, 0x4f, 0x1c, 0xdb, 0x93, 0x34, 0x37, 0x77, 0x94, 0xde, 0xeb, 0xdb, 0x0b, 0xa2, - 0x8a, 0xa0, 0x82, 0x02, 0x41, 0x0a, 0x55, 0x85, 0x0a, 0x2f, 0xa4, 0x0d, 0x50, 0x89, 0x16, 0x69, - 0xaa, 0xaa, 0xbc, 0x45, 0x4e, 0xeb, 0x06, 0x17, 0xc7, 0x13, 0x1d, 0x3b, 0xd0, 0x08, 0x1e, 0x58, - 0x04, 0x6b, 0x60, 0x09, 0x2c, 0x84, 0x05, 0xb0, 0x10, 0x9e,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0xc9, 0xeb, 0xec, 0xd1, 0xc6, - 0x7e, 0x3a, 0x3e, 0x8e, 0xb2, 0x77, 0x6f, 0x92, 0x50, 0x9c, 0xb1, 0x7f, 0xa8, 0x9d, 0x45, 0x99, - 0xbc, 0x18, 0xa6, 0x79, 0xaf, 0x96, 0xb8, 0x2e, 0x99, 0x2f, 0x27, 0x22, 0x88, 0x03, 0x98, 0xab, - 0x48, 0x22, 0x85, 0xbc, 0xee, 0x3b, 0xd4, 0x9e, 0x25, 0x91, 0x48, 0x3a, 0xeb, 0xb4, 0xca, 0xc3, - 0x20, 0x36, 0x87, 0xaf, 0xa8, 0x8f, 0x88, 0x2e, 0x36, 0x5c, 0xf7, 0xb4, 0xf5, 0x45, 0xfe, 0x59, - 0x9d, 0x8f, 0xd4, 0xd9, 0x11, 0x78, 0x8e, 0x0b, 0x76, 0x83, 0x7a, 0xd1, 0x24, 0x18, 0x47, 0x09, - 0x06, 0xeb, 0x76, 0xb3, 0x61, 0x2c, 0xbd, 0x5d, 0xda, 0x04, 0x19, 0x3d, 0x0c, 0x2f, 0xb2, 0x30, - 0x49, 0xe5, 0x8f, 0xb1, 0x86, 0x19, 0x88, 0x20, 0xf6, 0x3f, 0x5d, 0x9e, 0xa8, 0x3c, 0x9e, 0x2f, - 0xa3, 0x69, 0x50, 0x78, 0xb6, 0xd7, 0x68, 0x35, 0x09, 0x26, 0xe1, 0x15, 0xef, 0x67, 0x85, 0x58, - 0x29, 0x6f, 0x97, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff, 0x14, 0x11, 0xc4, 0x75, 0x10, 0x06, 0x00, - 0x00, + // 784 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x74, 0x94, 0xdf, 0x6e, 0xd3, 0x48, + 0x14, 0xc6, 0x1b, 0x4f, 0x1c, 0xdb, 0x93, 0x34, 0x9b, 0x1d, 0xa5, 0xbb, 0xde, 0xec, 0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 0xbb, 0x1c, 0x29, 0xb1, 0x5c, 0x92, 0x2e, 0x75, 0x88, 0xf3, 0x58, + 0x04, 0x99, 0xef, 0x4a, 0xc5, 0x92, 0x87, 0x78, 0x82, 0x95, 0xf6, 0x9c, 0x89, 0xc5, 0x38, 0x0e, + 0x7d, 0x4f, 0x2a, 0x15, 0xe9, 0x39, 0x50, 0x65, 0x1e, 0x97, 0x41, 0x94, 0x4c, 0x7d, 0x2a, 0x25, + 0x0f, 0xe3, 0x54, 0xa9, 0xe3, 0xc6, 0x4b, 0x89, 0xd1, 0xaf, 0x4b, 0xa5, 0x21, 0xe3, 0x06, 0x58, + 0xb1, 0xdb, 0xb4, 0x21, 0x46, 0x93, 0x40, 0xd2, 0xd5, 0x6a, 0x03, 0xd5, 0x81, 0xf7, 0xfd, 0xdb, + 0x3f, 0xb6, 0x6a, 0xe0, 0x54, 0xec, 0x4b, 0x55, 0xad, 0xbb, 0x9f, 0x2d, 0xea, 0xf0, 0x70, 0x1e, + 0x06, 0x59, 0x8a, 0x54, 0xa1, 0xa0, 0x4a, 0x90, 0x2a, 0x14, 0x54, 0xa1, 0xa4, 0x4a, 0x90, 0x2a, + 0x94, 0x54, 0xa1, 0xa4, 0x4a, 0x90, 0x2a, 0x94, 0x54, 0xc1, 0x50, 0x25, 0x48, 0x15, 0x0c, 0x55, + 0x30, 0x54, 0x09, 0x52, 0x05, 0x43, 0x15, 0x0c, 0x55, 0x82, 0x54, 0xe1, 0x78, 0xc5, 0x55, 0x52, + 0x25, 0x48, 0x15, 0x0c, 0x55, 0x28, 0xa9, 0x12, 0xa4, 0x0a, 0x25, 0x55, 0x30, 0x54, 0x09, 0x52, + 0x05, 0x43, 0x15, 0x0c, 0x55, 0x82, 0x54, 0xc1, 0x50, 0x85, 0x92, 0x2a, 0x41, 0xaa, 0xa0, 0x41, + 0x7d, 0x91, 0xd3, 0x77, 0x1a, 0x9d, 0x4d, 0xc3, 0x8c, 0x6d, 0x53, 0x7b, 0x22, 0x62, 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12, 0xcc, 0xc2, 0x6b, 0xde, 0x0f, 0x0a, 0xbf, 0x52, + 0x5e, 0xad, 0xfd, 0x08, 0x00, 0x00, 0xff, 0xff, 0x6b, 0x95, 0x80, 0x0d, 0x2c, 0x06, 0x00, 0x00, } diff --git a/jsonpb/jsonpb_test_proto/test_objects.proto b/jsonpb/jsonpb_test_proto/test_objects.proto index 6a3ca3971b..c1333ed156 100644 --- a/jsonpb/jsonpb_test_proto/test_objects.proto +++ b/jsonpb/jsonpb_test_proto/test_objects.proto @@ -92,6 +92,7 @@ message MsgWithOneof { oneof union { string title = 1; int64 salary = 2; + string Country = 3; } } diff --git a/proto/text.go b/proto/text.go index e2b99b122d..0a6bc9e588 100644 --- a/proto/text.go +++ b/proto/text.go @@ -335,7 +335,8 @@ func writeStruct(w *textWriter, sv reflect.Value) error { } inner := fv.Elem().Elem() // interface -> *T -> T tag := inner.Type().Field(0).Tag.Get("protobuf") - props.Parse(tag) // Overwrite the outer props. + props = new(Properties) // Overwrite the outer props var, but not its pointee. + props.Parse(tag) // Write the value in the oneof, not the oneof itself. fv = inner.Field(0)