jsonpb_test.go 43 KB

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