You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1232 lines
45 KiB

  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2015 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package jsonpb
  32. import (
  33. "bytes"
  34. "encoding/json"
  35. "io"
  36. "math"
  37. "reflect"
  38. "strings"
  39. "testing"
  40. "github.com/golang/protobuf/proto"
  41. pb "github.com/golang/protobuf/jsonpb/jsonpb_test_proto"
  42. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  43. "github.com/golang/protobuf/ptypes"
  44. anypb "github.com/golang/protobuf/ptypes/any"
  45. durpb "github.com/golang/protobuf/ptypes/duration"
  46. stpb "github.com/golang/protobuf/ptypes/struct"
  47. tspb "github.com/golang/protobuf/ptypes/timestamp"
  48. wpb "github.com/golang/protobuf/ptypes/wrappers"
  49. )
  50. var (
  51. marshaler = Marshaler{}
  52. marshalerAllOptions = Marshaler{
  53. Indent: " ",
  54. }
  55. simpleObject = &pb.Simple{
  56. OInt32: proto.Int32(-32),
  57. OInt32Str: proto.Int32(-32),
  58. OInt64: proto.Int64(-6400000000),
  59. OInt64Str: proto.Int64(-6400000000),
  60. OUint32: proto.Uint32(32),
  61. OUint32Str: proto.Uint32(32),
  62. OUint64: proto.Uint64(6400000000),
  63. OUint64Str: proto.Uint64(6400000000),
  64. OSint32: proto.Int32(-13),
  65. OSint32Str: proto.Int32(-13),
  66. OSint64: proto.Int64(-2600000000),
  67. OSint64Str: proto.Int64(-2600000000),
  68. OFloat: proto.Float32(3.14),
  69. OFloatStr: proto.Float32(3.14),
  70. ODouble: proto.Float64(6.02214179e23),
  71. ODoubleStr: proto.Float64(6.02214179e23),
  72. OBool: proto.Bool(true),
  73. OString: proto.String("hello \"there\""),
  74. OBytes: []byte("beep boop"),
  75. }
  76. simpleObjectInputJSON = `{` +
  77. `"oBool":true,` +
  78. `"oInt32":-32,` +
  79. `"oInt32Str":"-32",` +
  80. `"oInt64":-6400000000,` +
  81. `"oInt64Str":"-6400000000",` +
  82. `"oUint32":32,` +
  83. `"oUint32Str":"32",` +
  84. `"oUint64":6400000000,` +
  85. `"oUint64Str":"6400000000",` +
  86. `"oSint32":-13,` +
  87. `"oSint32Str":"-13",` +
  88. `"oSint64":-2600000000,` +
  89. `"oSint64Str":"-2600000000",` +
  90. `"oFloat":3.14,` +
  91. `"oFloatStr":"3.14",` +
  92. `"oDouble":6.02214179e+23,` +
  93. `"oDoubleStr":"6.02214179e+23",` +
  94. `"oString":"hello \"there\"",` +
  95. `"oBytes":"YmVlcCBib29w"` +
  96. `}`
  97. simpleObjectOutputJSON = `{` +
  98. `"oBool":true,` +
  99. `"oInt32":-32,` +
  100. `"oInt32Str":-32,` +
  101. `"oInt64":"-6400000000",` +
  102. `"oInt64Str":"-6400000000",` +
  103. `"oUint32":32,` +
  104. `"oUint32Str":32,` +
  105. `"oUint64":"6400000000",` +
  106. `"oUint64Str":"6400000000",` +
  107. `"oSint32":-13,` +
  108. `"oSint32Str":-13,` +
  109. `"oSint64":"-2600000000",` +
  110. `"oSint64Str":"-2600000000",` +
  111. `"oFloat":3.14,` +
  112. `"oFloatStr":3.14,` +
  113. `"oDouble":6.02214179e+23,` +
  114. `"oDoubleStr":6.02214179e+23,` +
  115. `"oString":"hello \"there\"",` +
  116. `"oBytes":"YmVlcCBib29w"` +
  117. `}`
  118. simpleObjectInputPrettyJSON = `{
  119. "oBool": true,
  120. "oInt32": -32,
  121. "oInt32Str": "-32",
  122. "oInt64": -6400000000,
  123. "oInt64Str": "-6400000000",
  124. "oUint32": 32,
  125. "oUint32Str": "32",
  126. "oUint64": 6400000000,
  127. "oUint64Str": "6400000000",
  128. "oSint32": -13,
  129. "oSint32Str": "-13",
  130. "oSint64": -2600000000,
  131. "oSint64Str": "-2600000000",
  132. "oFloat": 3.14,
  133. "oFloatStr": "3.14",
  134. "oDouble": 6.02214179e+23,
  135. "oDoubleStr": "6.02214179e+23",
  136. "oString": "hello \"there\"",
  137. "oBytes": "YmVlcCBib29w"
  138. }`
  139. simpleObjectOutputPrettyJSON = `{
  140. "oBool": true,
  141. "oInt32": -32,
  142. "oInt32Str": -32,
  143. "oInt64": "-6400000000",
  144. "oInt64Str": "-6400000000",
  145. "oUint32": 32,
  146. "oUint32Str": 32,
  147. "oUint64": "6400000000",
  148. "oUint64Str": "6400000000",
  149. "oSint32": -13,
  150. "oSint32Str": -13,
  151. "oSint64": "-2600000000",
  152. "oSint64Str": "-2600000000",
  153. "oFloat": 3.14,
  154. "oFloatStr": 3.14,
  155. "oDouble": 6.02214179e+23,
  156. "oDoubleStr": 6.02214179e+23,
  157. "oString": "hello \"there\"",
  158. "oBytes": "YmVlcCBib29w"
  159. }`
  160. repeatsObject = &pb.Repeats{
  161. RBool: []bool{true, false, true},
  162. RInt32: []int32{-3, -4, -5},
  163. RInt64: []int64{-123456789, -987654321},
  164. RUint32: []uint32{1, 2, 3},
  165. RUint64: []uint64{6789012345, 3456789012},
  166. RSint32: []int32{-1, -2, -3},
  167. RSint64: []int64{-6789012345, -3456789012},
  168. RFloat: []float32{3.14, 6.28},
  169. RDouble: []float64{299792458 * 1e20, 6.62606957e-34},
  170. RString: []string{"happy", "days"},
  171. RBytes: [][]byte{[]byte("skittles"), []byte("m&m's")},
  172. }
  173. repeatsObjectJSON = `{` +
  174. `"rBool":[true,false,true],` +
  175. `"rInt32":[-3,-4,-5],` +
  176. `"rInt64":["-123456789","-987654321"],` +
  177. `"rUint32":[1,2,3],` +
  178. `"rUint64":["6789012345","3456789012"],` +
  179. `"rSint32":[-1,-2,-3],` +
  180. `"rSint64":["-6789012345","-3456789012"],` +
  181. `"rFloat":[3.14,6.28],` +
  182. `"rDouble":[2.99792458e+28,6.62606957e-34],` +
  183. `"rString":["happy","days"],` +
  184. `"rBytes":["c2tpdHRsZXM=","bSZtJ3M="]` +
  185. `}`
  186. repeatsObjectPrettyJSON = `{
  187. "rBool": [
  188. true,
  189. false,
  190. true
  191. ],
  192. "rInt32": [
  193. -3,
  194. -4,
  195. -5
  196. ],
  197. "rInt64": [
  198. "-123456789",
  199. "-987654321"
  200. ],
  201. "rUint32": [
  202. 1,
  203. 2,
  204. 3
  205. ],
  206. "rUint64": [
  207. "6789012345",
  208. "3456789012"
  209. ],
  210. "rSint32": [
  211. -1,
  212. -2,
  213. -3
  214. ],
  215. "rSint64": [
  216. "-6789012345",
  217. "-3456789012"
  218. ],
  219. "rFloat": [
  220. 3.14,
  221. 6.28
  222. ],
  223. "rDouble": [
  224. 2.99792458e+28,
  225. 6.62606957e-34
  226. ],
  227. "rString": [
  228. "happy",
  229. "days"
  230. ],
  231. "rBytes": [
  232. "c2tpdHRsZXM=",
  233. "bSZtJ3M="
  234. ]
  235. }`
  236. innerSimple = &pb.Simple{OInt32: proto.Int32(-32)}
  237. innerSimple2 = &pb.Simple{OInt64: proto.Int64(25)}
  238. innerRepeats = &pb.Repeats{RString: []string{"roses", "red"}}
  239. innerRepeats2 = &pb.Repeats{RString: []string{"violets", "blue"}}
  240. complexObject = &pb.Widget{
  241. Color: pb.Widget_GREEN.Enum(),
  242. RColor: []pb.Widget_Color{pb.Widget_RED, pb.Widget_GREEN, pb.Widget_BLUE},
  243. Simple: innerSimple,
  244. RSimple: []*pb.Simple{innerSimple, innerSimple2},
  245. Repeats: innerRepeats,
  246. RRepeats: []*pb.Repeats{innerRepeats, innerRepeats2},
  247. }
  248. complexObjectJSON = `{"color":"GREEN",` +
  249. `"rColor":["RED","GREEN","BLUE"],` +
  250. `"simple":{"oInt32":-32},` +
  251. `"rSimple":[{"oInt32":-32},{"oInt64":"25"}],` +
  252. `"repeats":{"rString":["roses","red"]},` +
  253. `"rRepeats":[{"rString":["roses","red"]},{"rString":["violets","blue"]}]` +
  254. `}`
  255. complexObjectPrettyJSON = `{
  256. "color": "GREEN",
  257. "rColor": [
  258. "RED",
  259. "GREEN",
  260. "BLUE"
  261. ],
  262. "simple": {
  263. "oInt32": -32
  264. },
  265. "rSimple": [
  266. {
  267. "oInt32": -32
  268. },
  269. {
  270. "oInt64": "25"
  271. }
  272. ],
  273. "repeats": {
  274. "rString": [
  275. "roses",
  276. "red"
  277. ]
  278. },
  279. "rRepeats": [
  280. {
  281. "rString": [
  282. "roses",
  283. "red"
  284. ]
  285. },
  286. {
  287. "rString": [
  288. "violets",
  289. "blue"
  290. ]
  291. }
  292. ]
  293. }`
  294. colorPrettyJSON = `{
  295. "color": 2
  296. }`
  297. colorListPrettyJSON = `{
  298. "color": 1000,
  299. "rColor": [
  300. "RED"
  301. ]
  302. }`
  303. nummyPrettyJSON = `{
  304. "nummy": {
  305. "1": 2,
  306. "3": 4
  307. }
  308. }`
  309. objjyPrettyJSON = `{
  310. "objjy": {
  311. "1": {
  312. "dub": 1
  313. }
  314. }
  315. }`
  316. realNumber = &pb.Real{Value: proto.Float64(3.14159265359)}
  317. realNumberName = "Pi"
  318. complexNumber = &pb.Complex{Imaginary: proto.Float64(0.5772156649)}
  319. realNumberJSON = `{` +
  320. `"value":3.14159265359,` +
  321. `"[jsonpb.Complex.real_extension]":{"imaginary":0.5772156649},` +
  322. `"[jsonpb.name]":"Pi"` +
  323. `}`
  324. anySimple = &pb.KnownTypes{
  325. An: &anypb.Any{
  326. TypeUrl: "something.example.com/jsonpb.Simple",
  327. Value: []byte{
  328. // &pb.Simple{OBool:true}
  329. 1 << 3, 1,
  330. },
  331. },
  332. }
  333. anySimpleJSON = `{"an":{"@type":"something.example.com/jsonpb.Simple","oBool":true}}`
  334. anySimplePrettyJSON = `{
  335. "an": {
  336. "@type": "something.example.com/jsonpb.Simple",
  337. "oBool": true
  338. }
  339. }`
  340. anyWellKnown = &pb.KnownTypes{
  341. An: &anypb.Any{
  342. TypeUrl: "type.googleapis.com/google.protobuf.Duration",
  343. Value: []byte{
  344. // &durpb.Duration{Seconds: 1, Nanos: 212000000 }
  345. 1 << 3, 1, // seconds
  346. 2 << 3, 0x80, 0xba, 0x8b, 0x65, // nanos
  347. },
  348. },
  349. }
  350. anyWellKnownJSON = `{"an":{"@type":"type.googleapis.com/google.protobuf.Duration","value":"1.212s"}}`
  351. anyWellKnownPrettyJSON = `{
  352. "an": {
  353. "@type": "type.googleapis.com/google.protobuf.Duration",
  354. "value": "1.212s"
  355. }
  356. }`
  357. nonFinites = &pb.NonFinites{
  358. FNan: proto.Float32(float32(math.NaN())),
  359. FPinf: proto.Float32(float32(math.Inf(1))),
  360. FNinf: proto.Float32(float32(math.Inf(-1))),
  361. DNan: proto.Float64(float64(math.NaN())),
  362. DPinf: proto.Float64(float64(math.Inf(1))),
  363. DNinf: proto.Float64(float64(math.Inf(-1))),
  364. }
  365. nonFinitesJSON = `{` +
  366. `"fNan":"NaN",` +
  367. `"fPinf":"Infinity",` +
  368. `"fNinf":"-Infinity",` +
  369. `"dNan":"NaN",` +
  370. `"dPinf":"Infinity",` +
  371. `"dNinf":"-Infinity"` +
  372. `}`
  373. )
  374. func init() {
  375. if err := proto.SetExtension(realNumber, pb.E_Name, &realNumberName); err != nil {
  376. panic(err)
  377. }
  378. if err := proto.SetExtension(realNumber, pb.E_Complex_RealExtension, complexNumber); err != nil {
  379. panic(err)
  380. }
  381. }
  382. var marshalingTests = []struct {
  383. desc string
  384. marshaler Marshaler
  385. pb proto.Message
  386. json string
  387. }{
  388. {"simple flat object", marshaler, simpleObject, simpleObjectOutputJSON},
  389. {"simple pretty object", marshalerAllOptions, simpleObject, simpleObjectOutputPrettyJSON},
  390. {"non-finite floats fields object", marshaler, nonFinites, nonFinitesJSON},
  391. {"repeated fields flat object", marshaler, repeatsObject, repeatsObjectJSON},
  392. {"repeated fields pretty object", marshalerAllOptions, repeatsObject, repeatsObjectPrettyJSON},
  393. {"nested message/enum flat object", marshaler, complexObject, complexObjectJSON},
  394. {"nested message/enum pretty object", marshalerAllOptions, complexObject, complexObjectPrettyJSON},
  395. {"enum-string flat object", Marshaler{},
  396. &pb.Widget{Color: pb.Widget_BLUE.Enum()}, `{"color":"BLUE"}`},
  397. {"enum-value pretty object", Marshaler{EnumsAsInts: true, Indent: " "},
  398. &pb.Widget{Color: pb.Widget_BLUE.Enum()}, colorPrettyJSON},
  399. {"unknown enum value object", marshalerAllOptions,
  400. &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}, colorListPrettyJSON},
  401. {"repeated proto3 enum", Marshaler{},
  402. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  403. proto3pb.Message_PUNS,
  404. proto3pb.Message_SLAPSTICK,
  405. }},
  406. `{"rFunny":["PUNS","SLAPSTICK"]}`},
  407. {"repeated proto3 enum as int", Marshaler{EnumsAsInts: true},
  408. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  409. proto3pb.Message_PUNS,
  410. proto3pb.Message_SLAPSTICK,
  411. }},
  412. `{"rFunny":[1,2]}`},
  413. {"empty value", marshaler, &pb.Simple3{}, `{}`},
  414. {"empty value emitted", Marshaler{EmitDefaults: true}, &pb.Simple3{}, `{"dub":0}`},
  415. {"empty repeated emitted", Marshaler{EmitDefaults: true}, &pb.SimpleSlice3{}, `{"slices":[]}`},
  416. {"empty map emitted", Marshaler{EmitDefaults: true}, &pb.SimpleMap3{}, `{"stringy":{}}`},
  417. {"nested struct null", Marshaler{EmitDefaults: true}, &pb.SimpleNull3{}, `{"simple":null}`},
  418. {"map<int64, int32>", marshaler, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, `{"nummy":{"1":2,"3":4}}`},
  419. {"map<int64, int32>", marshalerAllOptions, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, nummyPrettyJSON},
  420. {"map<string, string>", marshaler,
  421. &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}},
  422. `{"strry":{"\"one\"":"two","three":"four"}}`},
  423. {"map<int32, Object>", marshaler,
  424. &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}, `{"objjy":{"1":{"dub":1}}}`},
  425. {"map<int32, Object>", marshalerAllOptions,
  426. &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}, objjyPrettyJSON},
  427. {"map<int64, string>", marshaler, &pb.Mappy{Buggy: map[int64]string{1234: "yup"}},
  428. `{"buggy":{"1234":"yup"}}`},
  429. {"map<bool, bool>", marshaler, &pb.Mappy{Booly: map[bool]bool{false: true}}, `{"booly":{"false":true}}`},
  430. {"map<string, enum>", marshaler, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}, `{"enumy":{"XIV":"ROMAN"}}`},
  431. {"map<string, enum as int>", Marshaler{EnumsAsInts: true}, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}, `{"enumy":{"XIV":2}}`},
  432. {"map<int32, bool>", marshaler, &pb.Mappy{S32Booly: map[int32]bool{1: true, 3: false, 10: true, 12: false}}, `{"s32booly":{"1":true,"3":false,"10":true,"12":false}}`},
  433. {"map<int64, bool>", marshaler, &pb.Mappy{S64Booly: map[int64]bool{1: true, 3: false, 10: true, 12: false}}, `{"s64booly":{"1":true,"3":false,"10":true,"12":false}}`},
  434. {"map<uint32, bool>", marshaler, &pb.Mappy{U32Booly: map[uint32]bool{1: true, 3: false, 10: true, 12: false}}, `{"u32booly":{"1":true,"3":false,"10":true,"12":false}}`},
  435. {"map<uint64, bool>", marshaler, &pb.Mappy{U64Booly: map[uint64]bool{1: true, 3: false, 10: true, 12: false}}, `{"u64booly":{"1":true,"3":false,"10":true,"12":false}}`},
  436. {"proto2 map<int64, string>", marshaler, &pb.Maps{MInt64Str: map[int64]string{213: "cat"}},
  437. `{"mInt64Str":{"213":"cat"}}`},
  438. {"proto2 map<bool, Object>", marshaler,
  439. &pb.Maps{MBoolSimple: map[bool]*pb.Simple{true: {OInt32: proto.Int32(1)}}},
  440. `{"mBoolSimple":{"true":{"oInt32":1}}}`},
  441. {"oneof, not set", marshaler, &pb.MsgWithOneof{}, `{}`},
  442. {"oneof, set", marshaler, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Title{"Grand Poobah"}}, `{"title":"Grand Poobah"}`},
  443. {"force orig_name", Marshaler{OrigName: true}, &pb.Simple{OInt32: proto.Int32(4)},
  444. `{"o_int32":4}`},
  445. {"proto2 extension", marshaler, realNumber, realNumberJSON},
  446. {"Any with message", marshaler, anySimple, anySimpleJSON},
  447. {"Any with message and indent", marshalerAllOptions, anySimple, anySimplePrettyJSON},
  448. {"Any with WKT", marshaler, anyWellKnown, anyWellKnownJSON},
  449. {"Any with WKT and indent", marshalerAllOptions, anyWellKnown, anyWellKnownPrettyJSON},
  450. {"Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}, `{"dur":"3s"}`},
  451. {"Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3, Nanos: 1e6}}, `{"dur":"3.001s"}`},
  452. {"Duration beyond float64 precision", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 100000000, Nanos: 1}}, `{"dur":"100000000.000000001s"}`},
  453. {"negative Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: -123, Nanos: -456}}, `{"dur":"-123.000000456s"}`},
  454. {"Struct", marshaler, &pb.KnownTypes{St: &stpb.Struct{
  455. Fields: map[string]*stpb.Value{
  456. "one": {Kind: &stpb.Value_StringValue{"loneliest number"}},
  457. "two": {Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}},
  458. },
  459. }}, `{"st":{"one":"loneliest number","two":null}}`},
  460. {"empty ListValue", marshaler, &pb.KnownTypes{Lv: &stpb.ListValue{}}, `{"lv":[]}`},
  461. {"basic ListValue", marshaler, &pb.KnownTypes{Lv: &stpb.ListValue{Values: []*stpb.Value{
  462. {Kind: &stpb.Value_StringValue{"x"}},
  463. {Kind: &stpb.Value_NullValue{}},
  464. {Kind: &stpb.Value_NumberValue{3}},
  465. {Kind: &stpb.Value_BoolValue{true}},
  466. }}}, `{"lv":["x",null,3,true]}`},
  467. {"Timestamp", marshaler, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}, `{"ts":"2014-05-13T16:53:20.021Z"}`},
  468. {"Timestamp", marshaler, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 0}}, `{"ts":"2014-05-13T16:53:20Z"}`},
  469. {"number Value", marshaler, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NumberValue{1}}}, `{"val":1}`},
  470. {"null Value", marshaler, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}}}, `{"val":null}`},
  471. {"string number value", marshaler, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_StringValue{"9223372036854775807"}}}, `{"val":"9223372036854775807"}`},
  472. {"list of lists Value", marshaler, &pb.KnownTypes{Val: &stpb.Value{
  473. Kind: &stpb.Value_ListValue{&stpb.ListValue{
  474. Values: []*stpb.Value{
  475. {Kind: &stpb.Value_StringValue{"x"}},
  476. {Kind: &stpb.Value_ListValue{&stpb.ListValue{
  477. Values: []*stpb.Value{
  478. {Kind: &stpb.Value_ListValue{&stpb.ListValue{
  479. Values: []*stpb.Value{{Kind: &stpb.Value_StringValue{"y"}}},
  480. }}},
  481. {Kind: &stpb.Value_StringValue{"z"}},
  482. },
  483. }}},
  484. },
  485. }},
  486. }}, `{"val":["x",[["y"],"z"]]}`},
  487. {"DoubleValue", marshaler, &pb.KnownTypes{Dbl: &wpb.DoubleValue{Value: 1.2}}, `{"dbl":1.2}`},
  488. {"FloatValue", marshaler, &pb.KnownTypes{Flt: &wpb.FloatValue{Value: 1.2}}, `{"flt":1.2}`},
  489. {"Int64Value", marshaler, &pb.KnownTypes{I64: &wpb.Int64Value{Value: -3}}, `{"i64":"-3"}`},
  490. {"UInt64Value", marshaler, &pb.KnownTypes{U64: &wpb.UInt64Value{Value: 3}}, `{"u64":"3"}`},
  491. {"Int32Value", marshaler, &pb.KnownTypes{I32: &wpb.Int32Value{Value: -4}}, `{"i32":-4}`},
  492. {"UInt32Value", marshaler, &pb.KnownTypes{U32: &wpb.UInt32Value{Value: 4}}, `{"u32":4}`},
  493. {"BoolValue", marshaler, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}, `{"bool":true}`},
  494. {"StringValue", marshaler, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}, `{"str":"plush"}`},
  495. {"BytesValue", marshaler, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}, `{"bytes":"d293"}`},
  496. {"required", marshaler, &pb.MsgWithRequired{Str: proto.String("hello")}, `{"str":"hello"}`},
  497. {"required bytes", marshaler, &pb.MsgWithRequiredBytes{Byts: []byte{}}, `{"byts":""}`},
  498. }
  499. func TestMarshaling(t *testing.T) {
  500. for _, tt := range marshalingTests {
  501. json, err := tt.marshaler.MarshalToString(tt.pb)
  502. if err != nil {
  503. t.Errorf("%s: marshaling error: %v", tt.desc, err)
  504. } else if tt.json != json {
  505. t.Errorf("%s: got [%v] want [%v]", tt.desc, json, tt.json)
  506. }
  507. }
  508. }
  509. func TestMarshalingNil(t *testing.T) {
  510. var msg *pb.Simple
  511. m := &Marshaler{}
  512. if _, err := m.MarshalToString(msg); err == nil {
  513. t.Errorf("mashaling nil returned no error")
  514. }
  515. }
  516. func TestMarshalIllegalTime(t *testing.T) {
  517. tests := []struct {
  518. pb proto.Message
  519. fail bool
  520. }{
  521. {&pb.KnownTypes{Dur: &durpb.Duration{Seconds: 1, Nanos: 0}}, false},
  522. {&pb.KnownTypes{Dur: &durpb.Duration{Seconds: -1, Nanos: 0}}, false},
  523. {&pb.KnownTypes{Dur: &durpb.Duration{Seconds: 1, Nanos: -1}}, true},
  524. {&pb.KnownTypes{Dur: &durpb.Duration{Seconds: -1, Nanos: 1}}, true},
  525. {&pb.KnownTypes{Dur: &durpb.Duration{Seconds: 1, Nanos: 1000000000}}, true},
  526. {&pb.KnownTypes{Dur: &durpb.Duration{Seconds: -1, Nanos: -1000000000}}, true},
  527. {&pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 1, Nanos: 1}}, false},
  528. {&pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 1, Nanos: -1}}, true},
  529. {&pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 1, Nanos: 1000000000}}, true},
  530. }
  531. for _, tt := range tests {
  532. _, err := marshaler.MarshalToString(tt.pb)
  533. if err == nil && tt.fail {
  534. t.Errorf("marshaler.MarshalToString(%v) = _, <nil>; want _, <non-nil>", tt.pb)
  535. }
  536. if err != nil && !tt.fail {
  537. t.Errorf("marshaler.MarshalToString(%v) = _, %v; want _, <nil>", tt.pb, err)
  538. }
  539. }
  540. }
  541. func TestMarshalJSONPBMarshaler(t *testing.T) {
  542. rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
  543. msg := dynamicMessage{RawJson: rawJson}
  544. str, err := new(Marshaler).MarshalToString(&msg)
  545. if err != nil {
  546. t.Errorf("an unexpected error occurred when marshalling JSONPBMarshaler: %v", err)
  547. }
  548. if str != rawJson {
  549. t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", str, rawJson)
  550. }
  551. }
  552. func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
  553. msg := dynamicMessage{RawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
  554. a, err := ptypes.MarshalAny(&msg)
  555. if err != nil {
  556. t.Errorf("an unexpected error occurred when marshalling to Any: %v", err)
  557. }
  558. str, err := new(Marshaler).MarshalToString(a)
  559. if err != nil {
  560. t.Errorf("an unexpected error occurred when marshalling Any to JSON: %v", err)
  561. }
  562. // after custom marshaling, it's round-tripped through JSON decoding/encoding already,
  563. // so the keys are sorted, whitespace is compacted, and "@type" key has been added
  564. expected := `{"@type":"type.googleapis.com/` + dynamicMessageName + `","baz":[0,1,2,3],"foo":"bar"}`
  565. if str != expected {
  566. t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", str, expected)
  567. }
  568. }
  569. func TestMarshalWithCustomValidation(t *testing.T) {
  570. msg := dynamicMessage{RawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`, Dummy: &dynamicMessage{}}
  571. js, err := new(Marshaler).MarshalToString(&msg)
  572. if err != nil {
  573. t.Errorf("an unexpected error occurred when marshalling to json: %v", err)
  574. }
  575. err = Unmarshal(strings.NewReader(js), &msg)
  576. if err != nil {
  577. t.Errorf("an unexpected error occurred when unmarshalling from json: %v", err)
  578. }
  579. }
  580. // Test marshaling message containing unset required fields should produce error.
  581. func TestMarshalUnsetRequiredFields(t *testing.T) {
  582. msgExt := &pb.Real{}
  583. proto.SetExtension(msgExt, pb.E_Extm, &pb.MsgWithRequired{})
  584. tests := []struct {
  585. desc string
  586. marshaler *Marshaler
  587. pb proto.Message
  588. }{
  589. {
  590. desc: "direct required field",
  591. marshaler: &Marshaler{},
  592. pb: &pb.MsgWithRequired{},
  593. },
  594. {
  595. desc: "direct required field + emit defaults",
  596. marshaler: &Marshaler{EmitDefaults: true},
  597. pb: &pb.MsgWithRequired{},
  598. },
  599. {
  600. desc: "indirect required field",
  601. marshaler: &Marshaler{},
  602. pb: &pb.MsgWithIndirectRequired{Subm: &pb.MsgWithRequired{}},
  603. },
  604. {
  605. desc: "indirect required field + emit defaults",
  606. marshaler: &Marshaler{EmitDefaults: true},
  607. pb: &pb.MsgWithIndirectRequired{Subm: &pb.MsgWithRequired{}},
  608. },
  609. {
  610. desc: "direct required wkt field",
  611. marshaler: &Marshaler{},
  612. pb: &pb.MsgWithRequiredWKT{},
  613. },
  614. {
  615. desc: "direct required wkt field + emit defaults",
  616. marshaler: &Marshaler{EmitDefaults: true},
  617. pb: &pb.MsgWithRequiredWKT{},
  618. },
  619. {
  620. desc: "direct required bytes field",
  621. marshaler: &Marshaler{},
  622. pb: &pb.MsgWithRequiredBytes{},
  623. },
  624. {
  625. desc: "required in map value",
  626. marshaler: &Marshaler{},
  627. pb: &pb.MsgWithIndirectRequired{
  628. MapField: map[string]*pb.MsgWithRequired{
  629. "key": {},
  630. },
  631. },
  632. },
  633. {
  634. desc: "required in repeated item",
  635. marshaler: &Marshaler{},
  636. pb: &pb.MsgWithIndirectRequired{
  637. SliceField: []*pb.MsgWithRequired{
  638. {Str: proto.String("hello")},
  639. {},
  640. },
  641. },
  642. },
  643. {
  644. desc: "required inside oneof",
  645. marshaler: &Marshaler{},
  646. pb: &pb.MsgWithOneof{
  647. Union: &pb.MsgWithOneof_MsgWithRequired{&pb.MsgWithRequired{}},
  648. },
  649. },
  650. {
  651. desc: "required inside extension",
  652. marshaler: &Marshaler{},
  653. pb: msgExt,
  654. },
  655. }
  656. for _, tc := range tests {
  657. if _, err := tc.marshaler.MarshalToString(tc.pb); err == nil {
  658. t.Errorf("%s: expecting error in marshaling with unset required fields %+v", tc.desc, tc.pb)
  659. }
  660. }
  661. }
  662. var unmarshalingTests = []struct {
  663. desc string
  664. unmarshaler Unmarshaler
  665. json string
  666. pb proto.Message
  667. }{
  668. {"simple flat object", Unmarshaler{}, simpleObjectInputJSON, simpleObject},
  669. {"simple pretty object", Unmarshaler{}, simpleObjectInputPrettyJSON, simpleObject},
  670. {"repeated fields flat object", Unmarshaler{}, repeatsObjectJSON, repeatsObject},
  671. {"repeated fields pretty object", Unmarshaler{}, repeatsObjectPrettyJSON, repeatsObject},
  672. {"nested message/enum flat object", Unmarshaler{}, complexObjectJSON, complexObject},
  673. {"nested message/enum pretty object", Unmarshaler{}, complexObjectPrettyJSON, complexObject},
  674. {"enum-string object", Unmarshaler{}, `{"color":"BLUE"}`, &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
  675. {"enum-value object", Unmarshaler{}, "{\n \"color\": 2\n}", &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
  676. {"unknown field with allowed option", Unmarshaler{AllowUnknownFields: true}, `{"unknown": "foo"}`, new(pb.Simple)},
  677. {"proto3 enum string", Unmarshaler{}, `{"hilarity":"PUNS"}`, &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
  678. {"proto3 enum value", Unmarshaler{}, `{"hilarity":1}`, &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
  679. {"unknown enum value object",
  680. Unmarshaler{},
  681. "{\n \"color\": 1000,\n \"r_color\": [\n \"RED\"\n ]\n}",
  682. &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}},
  683. {"repeated proto3 enum", Unmarshaler{}, `{"rFunny":["PUNS","SLAPSTICK"]}`,
  684. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  685. proto3pb.Message_PUNS,
  686. proto3pb.Message_SLAPSTICK,
  687. }}},
  688. {"repeated proto3 enum as int", Unmarshaler{}, `{"rFunny":[1,2]}`,
  689. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  690. proto3pb.Message_PUNS,
  691. proto3pb.Message_SLAPSTICK,
  692. }}},
  693. {"repeated proto3 enum as mix of strings and ints", Unmarshaler{}, `{"rFunny":["PUNS",2]}`,
  694. &proto3pb.Message{RFunny: []proto3pb.Message_Humour{
  695. proto3pb.Message_PUNS,
  696. proto3pb.Message_SLAPSTICK,
  697. }}},
  698. {"unquoted int64 object", Unmarshaler{}, `{"oInt64":-314}`, &pb.Simple{OInt64: proto.Int64(-314)}},
  699. {"unquoted uint64 object", Unmarshaler{}, `{"oUint64":123}`, &pb.Simple{OUint64: proto.Uint64(123)}},
  700. {"NaN", Unmarshaler{}, `{"oDouble":"NaN"}`, &pb.Simple{ODouble: proto.Float64(math.NaN())}},
  701. {"Inf", Unmarshaler{}, `{"oFloat":"Infinity"}`, &pb.Simple{OFloat: proto.Float32(float32(math.Inf(1)))}},
  702. {"-Inf", Unmarshaler{}, `{"oDouble":"-Infinity"}`, &pb.Simple{ODouble: proto.Float64(math.Inf(-1))}},
  703. {"map<int64, int32>", Unmarshaler{}, `{"nummy":{"1":2,"3":4}}`, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}},
  704. {"map<string, string>", Unmarshaler{}, `{"strry":{"\"one\"":"two","three":"four"}}`, &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}}},
  705. {"map<int32, Object>", Unmarshaler{}, `{"objjy":{"1":{"dub":1}}}`, &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: {Dub: 1}}}},
  706. {"proto2 extension", Unmarshaler{}, realNumberJSON, realNumber},
  707. {"Any with message", Unmarshaler{}, anySimpleJSON, anySimple},
  708. {"Any with message and indent", Unmarshaler{}, anySimplePrettyJSON, anySimple},
  709. {"Any with WKT", Unmarshaler{}, anyWellKnownJSON, anyWellKnown},
  710. {"Any with WKT and indent", Unmarshaler{}, anyWellKnownPrettyJSON, anyWellKnown},
  711. {"map<string, enum>", Unmarshaler{}, `{"enumy":{"XIV":"ROMAN"}}`, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}},
  712. {"map<string, enum as int>", Unmarshaler{}, `{"enumy":{"XIV":2}}`, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}},
  713. {"oneof", Unmarshaler{}, `{"salary":31000}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Salary{31000}}},
  714. {"oneof spec name", Unmarshaler{}, `{"Country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{"Australia"}}},
  715. {"oneof orig_name", Unmarshaler{}, `{"Country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{"Australia"}}},
  716. {"oneof spec name2", Unmarshaler{}, `{"homeAddress":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_HomeAddress{"Australia"}}},
  717. {"oneof orig_name2", Unmarshaler{}, `{"home_address":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_HomeAddress{"Australia"}}},
  718. {"orig_name input", Unmarshaler{}, `{"o_bool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
  719. {"camelName input", Unmarshaler{}, `{"oBool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
  720. {"Duration", Unmarshaler{}, `{"dur":"3.000s"}`, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}},
  721. {"Duration", Unmarshaler{}, `{"dur":"4s"}`, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 4}}},
  722. {"Duration with unicode", Unmarshaler{}, `{"dur": "3\u0073"}`, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}},
  723. {"null Duration", Unmarshaler{}, `{"dur":null}`, &pb.KnownTypes{Dur: nil}},
  724. {"Timestamp", Unmarshaler{}, `{"ts":"2014-05-13T16:53:20.021Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}},
  725. {"Timestamp", Unmarshaler{}, `{"ts":"2014-05-13T16:53:20Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 0}}},
  726. {"Timestamp with unicode", Unmarshaler{}, `{"ts": "2014-05-13T16:53:20\u005a"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 0}}},
  727. {"PreEpochTimestamp", Unmarshaler{}, `{"ts":"1969-12-31T23:59:58.999999995Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: -2, Nanos: 999999995}}},
  728. {"ZeroTimeTimestamp", Unmarshaler{}, `{"ts":"0001-01-01T00:00:00Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: -62135596800, Nanos: 0}}},
  729. {"null Timestamp", Unmarshaler{}, `{"ts":null}`, &pb.KnownTypes{Ts: nil}},
  730. {"null Struct", Unmarshaler{}, `{"st": null}`, &pb.KnownTypes{St: nil}},
  731. {"empty Struct", Unmarshaler{}, `{"st": {}}`, &pb.KnownTypes{St: &stpb.Struct{}}},
  732. {"basic Struct", Unmarshaler{}, `{"st": {"a": "x", "b": null, "c": 3, "d": true}}`, &pb.KnownTypes{St: &stpb.Struct{Fields: map[string]*stpb.Value{
  733. "a": {Kind: &stpb.Value_StringValue{"x"}},
  734. "b": {Kind: &stpb.Value_NullValue{}},
  735. "c": {Kind: &stpb.Value_NumberValue{3}},
  736. "d": {Kind: &stpb.Value_BoolValue{true}},
  737. }}}},
  738. {"nested Struct", Unmarshaler{}, `{"st": {"a": {"b": 1, "c": [{"d": true}, "f"]}}}`, &pb.KnownTypes{St: &stpb.Struct{Fields: map[string]*stpb.Value{
  739. "a": {Kind: &stpb.Value_StructValue{&stpb.Struct{Fields: map[string]*stpb.Value{
  740. "b": {Kind: &stpb.Value_NumberValue{1}},
  741. "c": {Kind: &stpb.Value_ListValue{&stpb.ListValue{Values: []*stpb.Value{
  742. {Kind: &stpb.Value_StructValue{&stpb.Struct{Fields: map[string]*stpb.Value{"d": {Kind: &stpb.Value_BoolValue{true}}}}}},
  743. {Kind: &stpb.Value_StringValue{"f"}},
  744. }}}},
  745. }}}},
  746. }}}},
  747. {"null ListValue", Unmarshaler{}, `{"lv": null}`, &pb.KnownTypes{Lv: nil}},
  748. {"empty ListValue", Unmarshaler{}, `{"lv": []}`, &pb.KnownTypes{Lv: &stpb.ListValue{}}},
  749. {"basic ListValue", Unmarshaler{}, `{"lv": ["x", null, 3, true]}`, &pb.KnownTypes{Lv: &stpb.ListValue{Values: []*stpb.Value{
  750. {Kind: &stpb.Value_StringValue{"x"}},
  751. {Kind: &stpb.Value_NullValue{}},
  752. {Kind: &stpb.Value_NumberValue{3}},
  753. {Kind: &stpb.Value_BoolValue{true}},
  754. }}}},
  755. {"number Value", Unmarshaler{}, `{"val":1}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NumberValue{1}}}},
  756. {"null Value", Unmarshaler{}, `{"val":null}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}}}},
  757. {"bool Value", Unmarshaler{}, `{"val":true}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_BoolValue{true}}}},
  758. {"string Value", Unmarshaler{}, `{"val":"x"}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_StringValue{"x"}}}},
  759. {"string number value", Unmarshaler{}, `{"val":"9223372036854775807"}`, &pb.KnownTypes{Val: &stpb.Value{Kind: &stpb.Value_StringValue{"9223372036854775807"}}}},
  760. {"list of lists Value", Unmarshaler{}, `{"val":["x", [["y"], "z"]]}`, &pb.KnownTypes{Val: &stpb.Value{
  761. Kind: &stpb.Value_ListValue{&stpb.ListValue{
  762. Values: []*stpb.Value{
  763. {Kind: &stpb.Value_StringValue{"x"}},
  764. {Kind: &stpb.Value_ListValue{&stpb.ListValue{
  765. Values: []*stpb.Value{
  766. {Kind: &stpb.Value_ListValue{&stpb.ListValue{
  767. Values: []*stpb.Value{{Kind: &stpb.Value_StringValue{"y"}}},
  768. }}},
  769. {Kind: &stpb.Value_StringValue{"z"}},
  770. },
  771. }}},
  772. },
  773. }}}}},
  774. {"DoubleValue", Unmarshaler{}, `{"dbl":1.2}`, &pb.KnownTypes{Dbl: &wpb.DoubleValue{Value: 1.2}}},
  775. {"FloatValue", Unmarshaler{}, `{"flt":1.2}`, &pb.KnownTypes{Flt: &wpb.FloatValue{Value: 1.2}}},
  776. {"Int64Value", Unmarshaler{}, `{"i64":"-3"}`, &pb.KnownTypes{I64: &wpb.Int64Value{Value: -3}}},
  777. {"UInt64Value", Unmarshaler{}, `{"u64":"3"}`, &pb.KnownTypes{U64: &wpb.UInt64Value{Value: 3}}},
  778. {"Int32Value", Unmarshaler{}, `{"i32":-4}`, &pb.KnownTypes{I32: &wpb.Int32Value{Value: -4}}},
  779. {"UInt32Value", Unmarshaler{}, `{"u32":4}`, &pb.KnownTypes{U32: &wpb.UInt32Value{Value: 4}}},
  780. {"BoolValue", Unmarshaler{}, `{"bool":true}`, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}},
  781. {"StringValue", Unmarshaler{}, `{"str":"plush"}`, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}},
  782. {"StringValue containing escaped character", Unmarshaler{}, `{"str":"a\/b"}`, &pb.KnownTypes{Str: &wpb.StringValue{Value: "a/b"}}},
  783. {"StructValue containing StringValue's", Unmarshaler{}, `{"escaped": "a\/b", "unicode": "\u00004E16\u0000754C"}`,
  784. &stpb.Struct{
  785. Fields: map[string]*stpb.Value{
  786. "escaped": {Kind: &stpb.Value_StringValue{"a/b"}},
  787. "unicode": {Kind: &stpb.Value_StringValue{"\u00004E16\u0000754C"}},
  788. },
  789. }},
  790. {"BytesValue", Unmarshaler{}, `{"bytes":"d293"}`, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}},
  791. // Ensure that `null` as a value ends up with a nil pointer instead of a [type]Value struct.
  792. {"null DoubleValue", Unmarshaler{}, `{"dbl":null}`, &pb.KnownTypes{Dbl: nil}},
  793. {"null FloatValue", Unmarshaler{}, `{"flt":null}`, &pb.KnownTypes{Flt: nil}},
  794. {"null Int64Value", Unmarshaler{}, `{"i64":null}`, &pb.KnownTypes{I64: nil}},
  795. {"null UInt64Value", Unmarshaler{}, `{"u64":null}`, &pb.KnownTypes{U64: nil}},
  796. {"null Int32Value", Unmarshaler{}, `{"i32":null}`, &pb.KnownTypes{I32: nil}},
  797. {"null UInt32Value", Unmarshaler{}, `{"u32":null}`, &pb.KnownTypes{U32: nil}},
  798. {"null BoolValue", Unmarshaler{}, `{"bool":null}`, &pb.KnownTypes{Bool: nil}},
  799. {"null StringValue", Unmarshaler{}, `{"str":null}`, &pb.KnownTypes{Str: nil}},
  800. {"null BytesValue", Unmarshaler{}, `{"bytes":null}`, &pb.KnownTypes{Bytes: nil}},
  801. {"required", Unmarshaler{}, `{"str":"hello"}`, &pb.MsgWithRequired{Str: proto.String("hello")}},
  802. {"required bytes", Unmarshaler{}, `{"byts": []}`, &pb.MsgWithRequiredBytes{Byts: []byte{}}},
  803. }
  804. func TestUnmarshaling(t *testing.T) {
  805. for _, tt := range unmarshalingTests {
  806. // Make a new instance of the type of our expected object.
  807. p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
  808. err := tt.unmarshaler.Unmarshal(strings.NewReader(tt.json), p)
  809. if err != nil {
  810. t.Errorf("unmarshalling %s: %v", tt.desc, err)
  811. continue
  812. }
  813. // For easier diffs, compare text strings of the protos.
  814. exp := proto.MarshalTextString(tt.pb)
  815. act := proto.MarshalTextString(p)
  816. if string(exp) != string(act) {
  817. t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
  818. }
  819. }
  820. }
  821. func TestUnmarshalNullArray(t *testing.T) {
  822. var repeats pb.Repeats
  823. if err := UnmarshalString(`{"rBool":null}`, &repeats); err != nil {
  824. t.Fatal(err)
  825. }
  826. if !reflect.DeepEqual(repeats, pb.Repeats{}) {
  827. t.Errorf("got non-nil fields in [%#v]", repeats)
  828. }
  829. }
  830. func TestUnmarshalNullObject(t *testing.T) {
  831. var maps pb.Maps
  832. if err := UnmarshalString(`{"mInt64Str":null}`, &maps); err != nil {
  833. t.Fatal(err)
  834. }
  835. if !reflect.DeepEqual(maps, pb.Maps{}) {
  836. t.Errorf("got non-nil fields in [%#v]", maps)
  837. }
  838. }
  839. func TestUnmarshalNext(t *testing.T) {
  840. // We only need to check against a few, not all of them.
  841. tests := unmarshalingTests[:5]
  842. // Create a buffer with many concatenated JSON objects.
  843. var b bytes.Buffer
  844. for _, tt := range tests {
  845. b.WriteString(tt.json)
  846. }
  847. dec := json.NewDecoder(&b)
  848. for _, tt := range tests {
  849. // Make a new instance of the type of our expected object.
  850. p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
  851. err := tt.unmarshaler.UnmarshalNext(dec, p)
  852. if err != nil {
  853. t.Errorf("%s: %v", tt.desc, err)
  854. continue
  855. }
  856. // For easier diffs, compare text strings of the protos.
  857. exp := proto.MarshalTextString(tt.pb)
  858. act := proto.MarshalTextString(p)
  859. if string(exp) != string(act) {
  860. t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
  861. }
  862. }
  863. p := &pb.Simple{}
  864. err := new(Unmarshaler).UnmarshalNext(dec, p)
  865. if err != io.EOF {
  866. t.Errorf("eof: got %v, expected io.EOF", err)
  867. }
  868. }
  869. var unmarshalingShouldError = []struct {
  870. desc string
  871. in string
  872. pb proto.Message
  873. }{
  874. {"a value", "666", new(pb.Simple)},
  875. {"gibberish", "{adskja123;l23=-=", new(pb.Simple)},
  876. {"unknown field", `{"unknown": "foo"}`, new(pb.Simple)},
  877. {"unknown enum name", `{"hilarity":"DAVE"}`, new(proto3pb.Message)},
  878. {"Duration containing invalid character", `{"dur": "3\U0073"}`, &pb.KnownTypes{}},
  879. {"Timestamp containing invalid character", `{"ts": "2014-05-13T16:53:20\U005a"}`, &pb.KnownTypes{}},
  880. {"StringValue containing invalid character", `{"str": "\U00004E16\U0000754C"}`, &pb.KnownTypes{}},
  881. {"StructValue containing invalid character", `{"str": "\U00004E16\U0000754C"}`, &stpb.Struct{}},
  882. {"repeated proto3 enum with non array input", `{"rFunny":"PUNS"}`, &proto3pb.Message{RFunny: []proto3pb.Message_Humour{}}},
  883. }
  884. func TestUnmarshalingBadInput(t *testing.T) {
  885. for _, tt := range unmarshalingShouldError {
  886. err := UnmarshalString(tt.in, tt.pb)
  887. if err == nil {
  888. t.Errorf("an error was expected when parsing %q instead of an object", tt.desc)
  889. }
  890. }
  891. }
  892. type funcResolver func(turl string) (proto.Message, error)
  893. func (fn funcResolver) Resolve(turl string) (proto.Message, error) {
  894. return fn(turl)
  895. }
  896. func TestAnyWithCustomResolver(t *testing.T) {
  897. var resolvedTypeUrls []string
  898. resolver := funcResolver(func(turl string) (proto.Message, error) {
  899. resolvedTypeUrls = append(resolvedTypeUrls, turl)
  900. return new(pb.Simple), nil
  901. })
  902. msg := &pb.Simple{
  903. OBytes: []byte{1, 2, 3, 4},
  904. OBool: proto.Bool(true),
  905. OString: proto.String("foobar"),
  906. OInt64: proto.Int64(1020304),
  907. }
  908. msgBytes, err := proto.Marshal(msg)
  909. if err != nil {
  910. t.Errorf("an unexpected error occurred when marshaling message: %v", err)
  911. }
  912. // make an Any with a type URL that won't resolve w/out custom resolver
  913. any := &anypb.Any{
  914. TypeUrl: "https://foobar.com/some.random.MessageKind",
  915. Value: msgBytes,
  916. }
  917. m := Marshaler{AnyResolver: resolver}
  918. js, err := m.MarshalToString(any)
  919. if err != nil {
  920. t.Errorf("an unexpected error occurred when marshaling any to JSON: %v", err)
  921. }
  922. if len(resolvedTypeUrls) != 1 {
  923. t.Errorf("custom resolver was not invoked during marshaling")
  924. } else if resolvedTypeUrls[0] != "https://foobar.com/some.random.MessageKind" {
  925. t.Errorf("custom resolver was invoked with wrong URL: got %q, wanted %q", resolvedTypeUrls[0], "https://foobar.com/some.random.MessageKind")
  926. }
  927. wanted := `{"@type":"https://foobar.com/some.random.MessageKind","oBool":true,"oInt64":"1020304","oString":"foobar","oBytes":"AQIDBA=="}`
  928. if js != wanted {
  929. t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", js, wanted)
  930. }
  931. u := Unmarshaler{AnyResolver: resolver}
  932. roundTrip := &anypb.Any{}
  933. err = u.Unmarshal(bytes.NewReader([]byte(js)), roundTrip)
  934. if err != nil {
  935. t.Errorf("an unexpected error occurred when unmarshaling any from JSON: %v", err)
  936. }
  937. if len(resolvedTypeUrls) != 2 {
  938. t.Errorf("custom resolver was not invoked during marshaling")
  939. } else if resolvedTypeUrls[1] != "https://foobar.com/some.random.MessageKind" {
  940. t.Errorf("custom resolver was invoked with wrong URL: got %q, wanted %q", resolvedTypeUrls[1], "https://foobar.com/some.random.MessageKind")
  941. }
  942. if !proto.Equal(any, roundTrip) {
  943. t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", roundTrip, any)
  944. }
  945. }
  946. func TestUnmarshalJSONPBUnmarshaler(t *testing.T) {
  947. rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
  948. var msg dynamicMessage
  949. if err := Unmarshal(strings.NewReader(rawJson), &msg); err != nil {
  950. t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
  951. }
  952. if msg.RawJson != rawJson {
  953. t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", msg.RawJson, rawJson)
  954. }
  955. }
  956. func TestUnmarshalNullWithJSONPBUnmarshaler(t *testing.T) {
  957. rawJson := `{"stringField":null}`
  958. var ptrFieldMsg ptrFieldMessage
  959. if err := Unmarshal(strings.NewReader(rawJson), &ptrFieldMsg); err != nil {
  960. t.Errorf("unmarshal error: %v", err)
  961. }
  962. want := ptrFieldMessage{StringField: &stringField{IsSet: true, StringValue: "null"}}
  963. if !proto.Equal(&ptrFieldMsg, &want) {
  964. t.Errorf("unmarshal result StringField: got %v, want %v", ptrFieldMsg, want)
  965. }
  966. }
  967. func TestUnmarshalAnyJSONPBUnmarshaler(t *testing.T) {
  968. rawJson := `{ "@type": "blah.com/` + dynamicMessageName + `", "foo": "bar", "baz": [0, 1, 2, 3] }`
  969. var got anypb.Any
  970. if err := Unmarshal(strings.NewReader(rawJson), &got); err != nil {
  971. t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
  972. }
  973. dm := &dynamicMessage{RawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
  974. var want anypb.Any
  975. if b, err := proto.Marshal(dm); err != nil {
  976. t.Errorf("an unexpected error occurred when marshaling message: %v", err)
  977. } else {
  978. want.TypeUrl = "blah.com/" + dynamicMessageName
  979. want.Value = b
  980. }
  981. if !proto.Equal(&got, &want) {
  982. t.Errorf("message contents not set correctly after unmarshalling JSON: got %v, wanted %v", got, want)
  983. }
  984. }
  985. const (
  986. dynamicMessageName = "google.protobuf.jsonpb.testing.dynamicMessage"
  987. )
  988. func init() {
  989. // we register the custom type below so that we can use it in Any types
  990. proto.RegisterType((*dynamicMessage)(nil), dynamicMessageName)
  991. }
  992. type ptrFieldMessage struct {
  993. StringField *stringField `protobuf:"bytes,1,opt,name=stringField"`
  994. }
  995. func (m *ptrFieldMessage) Reset() {
  996. }
  997. func (m *ptrFieldMessage) String() string {
  998. return m.StringField.StringValue
  999. }
  1000. func (m *ptrFieldMessage) ProtoMessage() {
  1001. }
  1002. type stringField struct {
  1003. IsSet bool `protobuf:"varint,1,opt,name=isSet"`
  1004. StringValue string `protobuf:"bytes,2,opt,name=stringValue"`
  1005. }
  1006. func (s *stringField) Reset() {
  1007. }
  1008. func (s *stringField) String() string {
  1009. return s.StringValue
  1010. }
  1011. func (s *stringField) ProtoMessage() {
  1012. }
  1013. func (s *stringField) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
  1014. s.IsSet = true
  1015. s.StringValue = string(js)
  1016. return nil
  1017. }
  1018. // dynamicMessage implements protobuf.Message but is not a normal generated message type.
  1019. // It provides implementations of JSONPBMarshaler and JSONPBUnmarshaler for JSON support.
  1020. type dynamicMessage struct {
  1021. RawJson string `protobuf:"bytes,1,opt,name=rawJson"`
  1022. // an unexported nested message is present just to ensure that it
  1023. // won't result in a panic (see issue #509)
  1024. Dummy *dynamicMessage `protobuf:"bytes,2,opt,name=dummy"`
  1025. }
  1026. func (m *dynamicMessage) Reset() {
  1027. m.RawJson = "{}"
  1028. }
  1029. func (m *dynamicMessage) String() string {
  1030. return m.RawJson
  1031. }
  1032. func (m *dynamicMessage) ProtoMessage() {
  1033. }
  1034. func (m *dynamicMessage) MarshalJSONPB(jm *Marshaler) ([]byte, error) {
  1035. return []byte(m.RawJson), nil
  1036. }
  1037. func (m *dynamicMessage) UnmarshalJSONPB(jum *Unmarshaler, js []byte) error {
  1038. m.RawJson = string(js)
  1039. return nil
  1040. }
  1041. // Test unmarshaling message containing unset required fields should produce error.
  1042. func TestUnmarshalUnsetRequiredFields(t *testing.T) {
  1043. tests := []struct {
  1044. desc string
  1045. pb proto.Message
  1046. json string
  1047. }{
  1048. {
  1049. desc: "direct required field missing",
  1050. pb: &pb.MsgWithRequired{},
  1051. json: `{}`,
  1052. },
  1053. {
  1054. desc: "direct required field set to null",
  1055. pb: &pb.MsgWithRequired{},
  1056. json: `{"str": null}`,
  1057. },
  1058. {
  1059. desc: "indirect required field missing",
  1060. pb: &pb.MsgWithIndirectRequired{},
  1061. json: `{"subm": {}}`,
  1062. },
  1063. {
  1064. desc: "indirect required field set to null",
  1065. pb: &pb.MsgWithIndirectRequired{},
  1066. json: `{"subm": {"str": null}}`,
  1067. },
  1068. {
  1069. desc: "direct required bytes field missing",
  1070. pb: &pb.MsgWithRequiredBytes{},
  1071. json: `{}`,
  1072. },
  1073. {
  1074. desc: "direct required bytes field set to null",
  1075. pb: &pb.MsgWithRequiredBytes{},
  1076. json: `{"byts": null}`,
  1077. },
  1078. {
  1079. desc: "direct required wkt field missing",
  1080. pb: &pb.MsgWithRequiredWKT{},
  1081. json: `{}`,
  1082. },
  1083. {
  1084. desc: "direct required wkt field set to null",
  1085. pb: &pb.MsgWithRequiredWKT{},
  1086. json: `{"str": null}`,
  1087. },
  1088. {
  1089. desc: "any containing message with required field set to null",
  1090. pb: &pb.KnownTypes{},
  1091. json: `{"an": {"@type": "example.com/jsonpb.MsgWithRequired", "str": null}}`,
  1092. },
  1093. {
  1094. desc: "any containing message with missing required field",
  1095. pb: &pb.KnownTypes{},
  1096. json: `{"an": {"@type": "example.com/jsonpb.MsgWithRequired"}}`,
  1097. },
  1098. {
  1099. desc: "missing required in map value",
  1100. pb: &pb.MsgWithIndirectRequired{},
  1101. json: `{"map_field": {"a": {}, "b": {"str": "hi"}}}`,
  1102. },
  1103. {
  1104. desc: "required in map value set to null",
  1105. pb: &pb.MsgWithIndirectRequired{},
  1106. json: `{"map_field": {"a": {"str": "hello"}, "b": {"str": null}}}`,
  1107. },
  1108. {
  1109. desc: "missing required in slice item",
  1110. pb: &pb.MsgWithIndirectRequired{},
  1111. json: `{"slice_field": [{}, {"str": "hi"}]}`,
  1112. },
  1113. {
  1114. desc: "required in slice item set to null",
  1115. pb: &pb.MsgWithIndirectRequired{},
  1116. json: `{"slice_field": [{"str": "hello"}, {"str": null}]}`,
  1117. },
  1118. {
  1119. desc: "required inside oneof missing",
  1120. pb: &pb.MsgWithOneof{},
  1121. json: `{"msgWithRequired": {}}`,
  1122. },
  1123. {
  1124. desc: "required inside oneof set to null",
  1125. pb: &pb.MsgWithOneof{},
  1126. json: `{"msgWithRequired": {"str": null}}`,
  1127. },
  1128. {
  1129. desc: "required field in extension missing",
  1130. pb: &pb.Real{},
  1131. json: `{"[jsonpb.extm]":{}}`,
  1132. },
  1133. {
  1134. desc: "required field in extension set to null",
  1135. pb: &pb.Real{},
  1136. json: `{"[jsonpb.extm]":{"str": null}}`,
  1137. },
  1138. }
  1139. for _, tc := range tests {
  1140. if err := UnmarshalString(tc.json, tc.pb); err == nil {
  1141. t.Errorf("%s: expecting error in unmarshaling with unset required fields %s", tc.desc, tc.json)
  1142. }
  1143. }
  1144. }