encode_test.go 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. // Copyright 2019 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_test
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "math"
  9. "strings"
  10. "testing"
  11. "github.com/google/go-cmp/cmp"
  12. "github.com/google/go-cmp/cmp/cmpopts"
  13. "google.golang.org/protobuf/encoding/jsonpb"
  14. "google.golang.org/protobuf/internal/encoding/pack"
  15. "google.golang.org/protobuf/internal/encoding/wire"
  16. pimpl "google.golang.org/protobuf/internal/impl"
  17. "google.golang.org/protobuf/internal/scalar"
  18. "google.golang.org/protobuf/proto"
  19. preg "google.golang.org/protobuf/reflect/protoregistry"
  20. "google.golang.org/protobuf/runtime/protoiface"
  21. "google.golang.org/protobuf/encoding/testprotos/pb2"
  22. "google.golang.org/protobuf/encoding/testprotos/pb3"
  23. knownpb "google.golang.org/protobuf/types/known"
  24. )
  25. // splitLines is a cmpopts.Option for comparing strings with line breaks.
  26. var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
  27. return strings.Split(s, "\n")
  28. })
  29. func pb2Enum(i int32) *pb2.Enum {
  30. p := new(pb2.Enum)
  31. *p = pb2.Enum(i)
  32. return p
  33. }
  34. func pb2Enums_NestedEnum(i int32) *pb2.Enums_NestedEnum {
  35. p := new(pb2.Enums_NestedEnum)
  36. *p = pb2.Enums_NestedEnum(i)
  37. return p
  38. }
  39. func setExtension(m proto.Message, xd *protoiface.ExtensionDescV1, val interface{}) {
  40. knownFields := m.ProtoReflect().KnownFields()
  41. extTypes := knownFields.ExtensionTypes()
  42. extTypes.Register(xd.Type)
  43. if val == nil {
  44. return
  45. }
  46. pval := xd.Type.ValueOf(val)
  47. knownFields.Set(wire.Number(xd.Field), pval)
  48. }
  49. // dhex decodes a hex-string and returns the bytes and panics if s is invalid.
  50. func dhex(s string) []byte {
  51. b, err := hex.DecodeString(s)
  52. if err != nil {
  53. panic(err)
  54. }
  55. return b
  56. }
  57. func TestMarshal(t *testing.T) {
  58. tests := []struct {
  59. desc string
  60. mo jsonpb.MarshalOptions
  61. input proto.Message
  62. want string
  63. wantErr bool // TODO: Verify error message substring.
  64. }{{
  65. desc: "proto2 optional scalars not set",
  66. input: &pb2.Scalars{},
  67. want: "{}",
  68. }, {
  69. desc: "proto3 scalars not set",
  70. input: &pb3.Scalars{},
  71. want: "{}",
  72. }, {
  73. desc: "proto2 optional scalars set to zero values",
  74. input: &pb2.Scalars{
  75. OptBool: scalar.Bool(false),
  76. OptInt32: scalar.Int32(0),
  77. OptInt64: scalar.Int64(0),
  78. OptUint32: scalar.Uint32(0),
  79. OptUint64: scalar.Uint64(0),
  80. OptSint32: scalar.Int32(0),
  81. OptSint64: scalar.Int64(0),
  82. OptFixed32: scalar.Uint32(0),
  83. OptFixed64: scalar.Uint64(0),
  84. OptSfixed32: scalar.Int32(0),
  85. OptSfixed64: scalar.Int64(0),
  86. OptFloat: scalar.Float32(0),
  87. OptDouble: scalar.Float64(0),
  88. OptBytes: []byte{},
  89. OptString: scalar.String(""),
  90. },
  91. want: `{
  92. "optBool": false,
  93. "optInt32": 0,
  94. "optInt64": "0",
  95. "optUint32": 0,
  96. "optUint64": "0",
  97. "optSint32": 0,
  98. "optSint64": "0",
  99. "optFixed32": 0,
  100. "optFixed64": "0",
  101. "optSfixed32": 0,
  102. "optSfixed64": "0",
  103. "optFloat": 0,
  104. "optDouble": 0,
  105. "optBytes": "",
  106. "optString": ""
  107. }`,
  108. }, {
  109. desc: "proto2 optional scalars set to some values",
  110. input: &pb2.Scalars{
  111. OptBool: scalar.Bool(true),
  112. OptInt32: scalar.Int32(0xff),
  113. OptInt64: scalar.Int64(0xdeadbeef),
  114. OptUint32: scalar.Uint32(47),
  115. OptUint64: scalar.Uint64(0xdeadbeef),
  116. OptSint32: scalar.Int32(-1001),
  117. OptSint64: scalar.Int64(-0xffff),
  118. OptFixed64: scalar.Uint64(64),
  119. OptSfixed32: scalar.Int32(-32),
  120. OptFloat: scalar.Float32(1.02),
  121. OptDouble: scalar.Float64(1.234),
  122. OptBytes: []byte("谷歌"),
  123. OptString: scalar.String("谷歌"),
  124. },
  125. want: `{
  126. "optBool": true,
  127. "optInt32": 255,
  128. "optInt64": "3735928559",
  129. "optUint32": 47,
  130. "optUint64": "3735928559",
  131. "optSint32": -1001,
  132. "optSint64": "-65535",
  133. "optFixed64": "64",
  134. "optSfixed32": -32,
  135. "optFloat": 1.02,
  136. "optDouble": 1.234,
  137. "optBytes": "6LC35q2M",
  138. "optString": "谷歌"
  139. }`,
  140. }, {
  141. desc: "string",
  142. input: &pb3.Scalars{
  143. SString: "谷歌",
  144. },
  145. want: `{
  146. "sString": "谷歌"
  147. }`,
  148. }, {
  149. desc: "string with invalid UTF8",
  150. input: &pb3.Scalars{
  151. SString: "abc\xff",
  152. },
  153. want: "{\n \"sString\": \"abc\xff\"\n}",
  154. wantErr: true,
  155. }, {
  156. desc: "float nan",
  157. input: &pb3.Scalars{
  158. SFloat: float32(math.NaN()),
  159. },
  160. want: `{
  161. "sFloat": "NaN"
  162. }`,
  163. }, {
  164. desc: "float positive infinity",
  165. input: &pb3.Scalars{
  166. SFloat: float32(math.Inf(1)),
  167. },
  168. want: `{
  169. "sFloat": "Infinity"
  170. }`,
  171. }, {
  172. desc: "float negative infinity",
  173. input: &pb3.Scalars{
  174. SFloat: float32(math.Inf(-1)),
  175. },
  176. want: `{
  177. "sFloat": "-Infinity"
  178. }`,
  179. }, {
  180. desc: "double nan",
  181. input: &pb3.Scalars{
  182. SDouble: math.NaN(),
  183. },
  184. want: `{
  185. "sDouble": "NaN"
  186. }`,
  187. }, {
  188. desc: "double positive infinity",
  189. input: &pb3.Scalars{
  190. SDouble: math.Inf(1),
  191. },
  192. want: `{
  193. "sDouble": "Infinity"
  194. }`,
  195. }, {
  196. desc: "double negative infinity",
  197. input: &pb3.Scalars{
  198. SDouble: math.Inf(-1),
  199. },
  200. want: `{
  201. "sDouble": "-Infinity"
  202. }`,
  203. }, {
  204. desc: "proto2 enum not set",
  205. input: &pb2.Enums{},
  206. want: "{}",
  207. }, {
  208. desc: "proto2 enum set to zero value",
  209. input: &pb2.Enums{
  210. OptEnum: pb2Enum(0),
  211. OptNestedEnum: pb2Enums_NestedEnum(0),
  212. },
  213. want: `{
  214. "optEnum": 0,
  215. "optNestedEnum": 0
  216. }`,
  217. }, {
  218. desc: "proto2 enum",
  219. input: &pb2.Enums{
  220. OptEnum: pb2.Enum_ONE.Enum(),
  221. OptNestedEnum: pb2.Enums_UNO.Enum(),
  222. },
  223. want: `{
  224. "optEnum": "ONE",
  225. "optNestedEnum": "UNO"
  226. }`,
  227. }, {
  228. desc: "proto2 enum set to numeric values",
  229. input: &pb2.Enums{
  230. OptEnum: pb2Enum(2),
  231. OptNestedEnum: pb2Enums_NestedEnum(2),
  232. },
  233. want: `{
  234. "optEnum": "TWO",
  235. "optNestedEnum": "DOS"
  236. }`,
  237. }, {
  238. desc: "proto2 enum set to unnamed numeric values",
  239. input: &pb2.Enums{
  240. OptEnum: pb2Enum(101),
  241. OptNestedEnum: pb2Enums_NestedEnum(-101),
  242. },
  243. want: `{
  244. "optEnum": 101,
  245. "optNestedEnum": -101
  246. }`,
  247. }, {
  248. desc: "proto3 enum not set",
  249. input: &pb3.Enums{},
  250. want: "{}",
  251. }, {
  252. desc: "proto3 enum set to zero value",
  253. input: &pb3.Enums{
  254. SEnum: pb3.Enum_ZERO,
  255. SNestedEnum: pb3.Enums_CERO,
  256. },
  257. want: "{}",
  258. }, {
  259. desc: "proto3 enum",
  260. input: &pb3.Enums{
  261. SEnum: pb3.Enum_ONE,
  262. SNestedEnum: pb3.Enums_UNO,
  263. },
  264. want: `{
  265. "sEnum": "ONE",
  266. "sNestedEnum": "UNO"
  267. }`,
  268. }, {
  269. desc: "proto3 enum set to numeric values",
  270. input: &pb3.Enums{
  271. SEnum: 2,
  272. SNestedEnum: 2,
  273. },
  274. want: `{
  275. "sEnum": "TWO",
  276. "sNestedEnum": "DOS"
  277. }`,
  278. }, {
  279. desc: "proto3 enum set to unnamed numeric values",
  280. input: &pb3.Enums{
  281. SEnum: -47,
  282. SNestedEnum: 47,
  283. },
  284. want: `{
  285. "sEnum": -47,
  286. "sNestedEnum": 47
  287. }`,
  288. }, {
  289. desc: "proto2 nested message not set",
  290. input: &pb2.Nests{},
  291. want: "{}",
  292. }, {
  293. desc: "proto2 nested message set to empty",
  294. input: &pb2.Nests{
  295. OptNested: &pb2.Nested{},
  296. Optgroup: &pb2.Nests_OptGroup{},
  297. },
  298. want: `{
  299. "optNested": {},
  300. "optgroup": {}
  301. }`,
  302. }, {
  303. desc: "proto2 nested messages",
  304. input: &pb2.Nests{
  305. OptNested: &pb2.Nested{
  306. OptString: scalar.String("nested message"),
  307. OptNested: &pb2.Nested{
  308. OptString: scalar.String("another nested message"),
  309. },
  310. },
  311. },
  312. want: `{
  313. "optNested": {
  314. "optString": "nested message",
  315. "optNested": {
  316. "optString": "another nested message"
  317. }
  318. }
  319. }`,
  320. }, {
  321. desc: "proto2 groups",
  322. input: &pb2.Nests{
  323. Optgroup: &pb2.Nests_OptGroup{
  324. OptString: scalar.String("inside a group"),
  325. OptNested: &pb2.Nested{
  326. OptString: scalar.String("nested message inside a group"),
  327. },
  328. Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
  329. OptFixed32: scalar.Uint32(47),
  330. },
  331. },
  332. },
  333. want: `{
  334. "optgroup": {
  335. "optString": "inside a group",
  336. "optNested": {
  337. "optString": "nested message inside a group"
  338. },
  339. "optnestedgroup": {
  340. "optFixed32": 47
  341. }
  342. }
  343. }`,
  344. }, {
  345. desc: "proto3 nested message not set",
  346. input: &pb3.Nests{},
  347. want: "{}",
  348. }, {
  349. desc: "proto3 nested message set to empty",
  350. input: &pb3.Nests{
  351. SNested: &pb3.Nested{},
  352. },
  353. want: `{
  354. "sNested": {}
  355. }`,
  356. }, {
  357. desc: "proto3 nested message",
  358. input: &pb3.Nests{
  359. SNested: &pb3.Nested{
  360. SString: "nested message",
  361. SNested: &pb3.Nested{
  362. SString: "another nested message",
  363. },
  364. },
  365. },
  366. want: `{
  367. "sNested": {
  368. "sString": "nested message",
  369. "sNested": {
  370. "sString": "another nested message"
  371. }
  372. }
  373. }`,
  374. }, {
  375. desc: "oneof not set",
  376. input: &pb3.Oneofs{},
  377. want: "{}",
  378. }, {
  379. desc: "oneof set to empty string",
  380. input: &pb3.Oneofs{
  381. Union: &pb3.Oneofs_OneofString{},
  382. },
  383. want: `{
  384. "oneofString": ""
  385. }`,
  386. }, {
  387. desc: "oneof set to string",
  388. input: &pb3.Oneofs{
  389. Union: &pb3.Oneofs_OneofString{
  390. OneofString: "hello",
  391. },
  392. },
  393. want: `{
  394. "oneofString": "hello"
  395. }`,
  396. }, {
  397. desc: "oneof set to enum",
  398. input: &pb3.Oneofs{
  399. Union: &pb3.Oneofs_OneofEnum{
  400. OneofEnum: pb3.Enum_ZERO,
  401. },
  402. },
  403. want: `{
  404. "oneofEnum": "ZERO"
  405. }`,
  406. }, {
  407. desc: "oneof set to empty message",
  408. input: &pb3.Oneofs{
  409. Union: &pb3.Oneofs_OneofNested{
  410. OneofNested: &pb3.Nested{},
  411. },
  412. },
  413. want: `{
  414. "oneofNested": {}
  415. }`,
  416. }, {
  417. desc: "oneof set to message",
  418. input: &pb3.Oneofs{
  419. Union: &pb3.Oneofs_OneofNested{
  420. OneofNested: &pb3.Nested{
  421. SString: "nested message",
  422. },
  423. },
  424. },
  425. want: `{
  426. "oneofNested": {
  427. "sString": "nested message"
  428. }
  429. }`,
  430. }, {
  431. desc: "repeated fields not set",
  432. input: &pb2.Repeats{},
  433. want: "{}",
  434. }, {
  435. desc: "repeated fields set to empty slices",
  436. input: &pb2.Repeats{
  437. RptBool: []bool{},
  438. RptInt32: []int32{},
  439. RptInt64: []int64{},
  440. RptUint32: []uint32{},
  441. RptUint64: []uint64{},
  442. RptFloat: []float32{},
  443. RptDouble: []float64{},
  444. RptBytes: [][]byte{},
  445. },
  446. want: "{}",
  447. }, {
  448. desc: "repeated fields set to some values",
  449. input: &pb2.Repeats{
  450. RptBool: []bool{true, false, true, true},
  451. RptInt32: []int32{1, 6, 0, 0},
  452. RptInt64: []int64{-64, 47},
  453. RptUint32: []uint32{0xff, 0xffff},
  454. RptUint64: []uint64{0xdeadbeef},
  455. RptFloat: []float32{float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), 1.034},
  456. RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308},
  457. RptString: []string{"hello", "世界"},
  458. RptBytes: [][]byte{
  459. []byte("hello"),
  460. []byte("\xe4\xb8\x96\xe7\x95\x8c"),
  461. },
  462. },
  463. want: `{
  464. "rptBool": [
  465. true,
  466. false,
  467. true,
  468. true
  469. ],
  470. "rptInt32": [
  471. 1,
  472. 6,
  473. 0,
  474. 0
  475. ],
  476. "rptInt64": [
  477. "-64",
  478. "47"
  479. ],
  480. "rptUint32": [
  481. 255,
  482. 65535
  483. ],
  484. "rptUint64": [
  485. "3735928559"
  486. ],
  487. "rptFloat": [
  488. "NaN",
  489. "Infinity",
  490. "-Infinity",
  491. 1.034
  492. ],
  493. "rptDouble": [
  494. "NaN",
  495. "Infinity",
  496. "-Infinity",
  497. 1.23e-308
  498. ],
  499. "rptString": [
  500. "hello",
  501. "世界"
  502. ],
  503. "rptBytes": [
  504. "aGVsbG8=",
  505. "5LiW55WM"
  506. ]
  507. }`,
  508. }, {
  509. desc: "repeated enums",
  510. input: &pb2.Enums{
  511. RptEnum: []pb2.Enum{pb2.Enum_ONE, 2, pb2.Enum_TEN, 42},
  512. RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10},
  513. },
  514. want: `{
  515. "rptEnum": [
  516. "ONE",
  517. "TWO",
  518. "TEN",
  519. 42
  520. ],
  521. "rptNestedEnum": [
  522. "DOS",
  523. 47,
  524. "DIEZ"
  525. ]
  526. }`,
  527. }, {
  528. desc: "repeated messages set to empty",
  529. input: &pb2.Nests{
  530. RptNested: []*pb2.Nested{},
  531. Rptgroup: []*pb2.Nests_RptGroup{},
  532. },
  533. want: "{}",
  534. }, {
  535. desc: "repeated messages",
  536. input: &pb2.Nests{
  537. RptNested: []*pb2.Nested{
  538. {
  539. OptString: scalar.String("repeat nested one"),
  540. },
  541. {
  542. OptString: scalar.String("repeat nested two"),
  543. OptNested: &pb2.Nested{
  544. OptString: scalar.String("inside repeat nested two"),
  545. },
  546. },
  547. {},
  548. },
  549. },
  550. want: `{
  551. "rptNested": [
  552. {
  553. "optString": "repeat nested one"
  554. },
  555. {
  556. "optString": "repeat nested two",
  557. "optNested": {
  558. "optString": "inside repeat nested two"
  559. }
  560. },
  561. {}
  562. ]
  563. }`,
  564. }, {
  565. desc: "repeated messages contains nil value",
  566. input: &pb2.Nests{
  567. RptNested: []*pb2.Nested{nil, {}},
  568. },
  569. want: `{
  570. "rptNested": [
  571. {},
  572. {}
  573. ]
  574. }`,
  575. }, {
  576. desc: "repeated groups",
  577. input: &pb2.Nests{
  578. Rptgroup: []*pb2.Nests_RptGroup{
  579. {
  580. RptString: []string{"hello", "world"},
  581. },
  582. {},
  583. nil,
  584. },
  585. },
  586. want: `{
  587. "rptgroup": [
  588. {
  589. "rptString": [
  590. "hello",
  591. "world"
  592. ]
  593. },
  594. {},
  595. {}
  596. ]
  597. }`,
  598. }, {
  599. desc: "map fields not set",
  600. input: &pb3.Maps{},
  601. want: "{}",
  602. }, {
  603. desc: "map fields set to empty",
  604. input: &pb3.Maps{
  605. Int32ToStr: map[int32]string{},
  606. BoolToUint32: map[bool]uint32{},
  607. Uint64ToEnum: map[uint64]pb3.Enum{},
  608. StrToNested: map[string]*pb3.Nested{},
  609. StrToOneofs: map[string]*pb3.Oneofs{},
  610. },
  611. want: "{}",
  612. }, {
  613. desc: "map fields 1",
  614. input: &pb3.Maps{
  615. BoolToUint32: map[bool]uint32{
  616. true: 42,
  617. false: 101,
  618. },
  619. },
  620. want: `{
  621. "boolToUint32": {
  622. "false": 101,
  623. "true": 42
  624. }
  625. }`,
  626. }, {
  627. desc: "map fields 2",
  628. input: &pb3.Maps{
  629. Int32ToStr: map[int32]string{
  630. -101: "-101",
  631. 0xff: "0xff",
  632. 0: "zero",
  633. },
  634. },
  635. want: `{
  636. "int32ToStr": {
  637. "-101": "-101",
  638. "0": "zero",
  639. "255": "0xff"
  640. }
  641. }`,
  642. }, {
  643. desc: "map fields 3",
  644. input: &pb3.Maps{
  645. Uint64ToEnum: map[uint64]pb3.Enum{
  646. 1: pb3.Enum_ONE,
  647. 2: pb3.Enum_TWO,
  648. 10: pb3.Enum_TEN,
  649. 47: 47,
  650. },
  651. },
  652. want: `{
  653. "uint64ToEnum": {
  654. "1": "ONE",
  655. "2": "TWO",
  656. "10": "TEN",
  657. "47": 47
  658. }
  659. }`,
  660. }, {
  661. desc: "map fields 4",
  662. input: &pb3.Maps{
  663. StrToNested: map[string]*pb3.Nested{
  664. "nested": &pb3.Nested{
  665. SString: "nested in a map",
  666. },
  667. },
  668. },
  669. want: `{
  670. "strToNested": {
  671. "nested": {
  672. "sString": "nested in a map"
  673. }
  674. }
  675. }`,
  676. }, {
  677. desc: "map fields 5",
  678. input: &pb3.Maps{
  679. StrToOneofs: map[string]*pb3.Oneofs{
  680. "string": &pb3.Oneofs{
  681. Union: &pb3.Oneofs_OneofString{
  682. OneofString: "hello",
  683. },
  684. },
  685. "nested": &pb3.Oneofs{
  686. Union: &pb3.Oneofs_OneofNested{
  687. OneofNested: &pb3.Nested{
  688. SString: "nested oneof in map field value",
  689. },
  690. },
  691. },
  692. },
  693. },
  694. want: `{
  695. "strToOneofs": {
  696. "nested": {
  697. "oneofNested": {
  698. "sString": "nested oneof in map field value"
  699. }
  700. },
  701. "string": {
  702. "oneofString": "hello"
  703. }
  704. }
  705. }`,
  706. }, {
  707. desc: "map field contains nil value",
  708. input: &pb3.Maps{
  709. StrToNested: map[string]*pb3.Nested{
  710. "nil": nil,
  711. },
  712. },
  713. want: `{
  714. "strToNested": {
  715. "nil": {}
  716. }
  717. }`,
  718. }, {
  719. desc: "required fields not set",
  720. input: &pb2.Requireds{},
  721. want: `{}`,
  722. wantErr: true,
  723. }, {
  724. desc: "required fields partially set",
  725. input: &pb2.Requireds{
  726. ReqBool: scalar.Bool(false),
  727. ReqSfixed64: scalar.Int64(0),
  728. ReqDouble: scalar.Float64(1.23),
  729. ReqString: scalar.String("hello"),
  730. ReqEnum: pb2.Enum_ONE.Enum(),
  731. },
  732. want: `{
  733. "reqBool": false,
  734. "reqSfixed64": "0",
  735. "reqDouble": 1.23,
  736. "reqString": "hello",
  737. "reqEnum": "ONE"
  738. }`,
  739. wantErr: true,
  740. }, {
  741. desc: "required fields not set with AllowPartial",
  742. mo: jsonpb.MarshalOptions{AllowPartial: true},
  743. input: &pb2.Requireds{
  744. ReqBool: scalar.Bool(false),
  745. ReqSfixed64: scalar.Int64(0),
  746. ReqDouble: scalar.Float64(1.23),
  747. ReqString: scalar.String("hello"),
  748. ReqEnum: pb2.Enum_ONE.Enum(),
  749. },
  750. want: `{
  751. "reqBool": false,
  752. "reqSfixed64": "0",
  753. "reqDouble": 1.23,
  754. "reqString": "hello",
  755. "reqEnum": "ONE"
  756. }`,
  757. }, {
  758. desc: "required fields all set",
  759. input: &pb2.Requireds{
  760. ReqBool: scalar.Bool(false),
  761. ReqSfixed64: scalar.Int64(0),
  762. ReqDouble: scalar.Float64(1.23),
  763. ReqString: scalar.String("hello"),
  764. ReqEnum: pb2.Enum_ONE.Enum(),
  765. ReqNested: &pb2.Nested{},
  766. },
  767. want: `{
  768. "reqBool": false,
  769. "reqSfixed64": "0",
  770. "reqDouble": 1.23,
  771. "reqString": "hello",
  772. "reqEnum": "ONE",
  773. "reqNested": {}
  774. }`,
  775. }, {
  776. desc: "indirect required field",
  777. input: &pb2.IndirectRequired{
  778. OptNested: &pb2.NestedWithRequired{},
  779. },
  780. want: `{
  781. "optNested": {}
  782. }`,
  783. wantErr: true,
  784. }, {
  785. desc: "indirect required field with AllowPartial",
  786. mo: jsonpb.MarshalOptions{AllowPartial: true},
  787. input: &pb2.IndirectRequired{
  788. OptNested: &pb2.NestedWithRequired{},
  789. },
  790. want: `{
  791. "optNested": {}
  792. }`,
  793. }, {
  794. desc: "indirect required field in empty repeated",
  795. input: &pb2.IndirectRequired{
  796. RptNested: []*pb2.NestedWithRequired{},
  797. },
  798. want: `{}`,
  799. }, {
  800. desc: "indirect required field in repeated",
  801. input: &pb2.IndirectRequired{
  802. RptNested: []*pb2.NestedWithRequired{
  803. &pb2.NestedWithRequired{},
  804. },
  805. },
  806. want: `{
  807. "rptNested": [
  808. {}
  809. ]
  810. }`,
  811. wantErr: true,
  812. }, {
  813. desc: "indirect required field in repeated with AllowPartial",
  814. mo: jsonpb.MarshalOptions{AllowPartial: true},
  815. input: &pb2.IndirectRequired{
  816. RptNested: []*pb2.NestedWithRequired{
  817. &pb2.NestedWithRequired{},
  818. },
  819. },
  820. want: `{
  821. "rptNested": [
  822. {}
  823. ]
  824. }`,
  825. }, {
  826. desc: "indirect required field in empty map",
  827. input: &pb2.IndirectRequired{
  828. StrToNested: map[string]*pb2.NestedWithRequired{},
  829. },
  830. want: "{}",
  831. }, {
  832. desc: "indirect required field in map",
  833. input: &pb2.IndirectRequired{
  834. StrToNested: map[string]*pb2.NestedWithRequired{
  835. "fail": &pb2.NestedWithRequired{},
  836. },
  837. },
  838. want: `{
  839. "strToNested": {
  840. "fail": {}
  841. }
  842. }`,
  843. wantErr: true,
  844. }, {
  845. desc: "indirect required field in map with AllowPartial",
  846. mo: jsonpb.MarshalOptions{AllowPartial: true},
  847. input: &pb2.IndirectRequired{
  848. StrToNested: map[string]*pb2.NestedWithRequired{
  849. "fail": &pb2.NestedWithRequired{},
  850. },
  851. },
  852. want: `{
  853. "strToNested": {
  854. "fail": {}
  855. }
  856. }`,
  857. }, {
  858. desc: "indirect required field in oneof",
  859. input: &pb2.IndirectRequired{
  860. Union: &pb2.IndirectRequired_OneofNested{
  861. OneofNested: &pb2.NestedWithRequired{},
  862. },
  863. },
  864. want: `{
  865. "oneofNested": {}
  866. }`,
  867. wantErr: true,
  868. }, {
  869. desc: "indirect required field in oneof with AllowPartial",
  870. mo: jsonpb.MarshalOptions{AllowPartial: true},
  871. input: &pb2.IndirectRequired{
  872. Union: &pb2.IndirectRequired_OneofNested{
  873. OneofNested: &pb2.NestedWithRequired{},
  874. },
  875. },
  876. want: `{
  877. "oneofNested": {}
  878. }`,
  879. }, {
  880. desc: "unknown fields are ignored",
  881. input: &pb2.Scalars{
  882. OptString: scalar.String("no unknowns"),
  883. XXX_unrecognized: pack.Message{
  884. pack.Tag{101, pack.BytesType}, pack.String("hello world"),
  885. }.Marshal(),
  886. },
  887. want: `{
  888. "optString": "no unknowns"
  889. }`,
  890. }, {
  891. desc: "json_name",
  892. input: &pb3.JSONNames{
  893. SString: "json_name",
  894. },
  895. want: `{
  896. "foo_bar": "json_name"
  897. }`,
  898. }, {
  899. desc: "extensions of non-repeated fields",
  900. input: func() proto.Message {
  901. m := &pb2.Extensions{
  902. OptString: scalar.String("non-extension field"),
  903. OptBool: scalar.Bool(true),
  904. OptInt32: scalar.Int32(42),
  905. }
  906. setExtension(m, pb2.E_OptExtBool, true)
  907. setExtension(m, pb2.E_OptExtString, "extension field")
  908. setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
  909. setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
  910. OptString: scalar.String("nested in an extension"),
  911. OptNested: &pb2.Nested{
  912. OptString: scalar.String("another nested in an extension"),
  913. },
  914. })
  915. return m
  916. }(),
  917. want: `{
  918. "optString": "non-extension field",
  919. "optBool": true,
  920. "optInt32": 42,
  921. "[pb2.opt_ext_bool]": true,
  922. "[pb2.opt_ext_enum]": "TEN",
  923. "[pb2.opt_ext_nested]": {
  924. "optString": "nested in an extension",
  925. "optNested": {
  926. "optString": "another nested in an extension"
  927. }
  928. },
  929. "[pb2.opt_ext_string]": "extension field"
  930. }`,
  931. }, {
  932. desc: "extension message field set to nil",
  933. input: func() proto.Message {
  934. m := &pb2.Extensions{}
  935. setExtension(m, pb2.E_OptExtNested, nil)
  936. return m
  937. }(),
  938. want: "{}",
  939. }, {
  940. desc: "extensions of repeated fields",
  941. input: func() proto.Message {
  942. m := &pb2.Extensions{}
  943. setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  944. setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
  945. setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
  946. &pb2.Nested{OptString: scalar.String("one")},
  947. &pb2.Nested{OptString: scalar.String("two")},
  948. &pb2.Nested{OptString: scalar.String("three")},
  949. })
  950. return m
  951. }(),
  952. want: `{
  953. "[pb2.rpt_ext_enum]": [
  954. "TEN",
  955. 101,
  956. "ONE"
  957. ],
  958. "[pb2.rpt_ext_fixed32]": [
  959. 42,
  960. 47
  961. ],
  962. "[pb2.rpt_ext_nested]": [
  963. {
  964. "optString": "one"
  965. },
  966. {
  967. "optString": "two"
  968. },
  969. {
  970. "optString": "three"
  971. }
  972. ]
  973. }`,
  974. }, {
  975. desc: "extensions of non-repeated fields in another message",
  976. input: func() proto.Message {
  977. m := &pb2.Extensions{}
  978. setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
  979. setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
  980. setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
  981. setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
  982. OptString: scalar.String("nested in an extension"),
  983. OptNested: &pb2.Nested{
  984. OptString: scalar.String("another nested in an extension"),
  985. },
  986. })
  987. return m
  988. }(),
  989. want: `{
  990. "[pb2.ExtensionsContainer.opt_ext_bool]": true,
  991. "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN",
  992. "[pb2.ExtensionsContainer.opt_ext_nested]": {
  993. "optString": "nested in an extension",
  994. "optNested": {
  995. "optString": "another nested in an extension"
  996. }
  997. },
  998. "[pb2.ExtensionsContainer.opt_ext_string]": "extension field"
  999. }`,
  1000. }, {
  1001. desc: "extensions of repeated fields in another message",
  1002. input: func() proto.Message {
  1003. m := &pb2.Extensions{
  1004. OptString: scalar.String("non-extension field"),
  1005. OptBool: scalar.Bool(true),
  1006. OptInt32: scalar.Int32(42),
  1007. }
  1008. setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  1009. setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
  1010. setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
  1011. &pb2.Nested{OptString: scalar.String("one")},
  1012. &pb2.Nested{OptString: scalar.String("two")},
  1013. &pb2.Nested{OptString: scalar.String("three")},
  1014. })
  1015. return m
  1016. }(),
  1017. want: `{
  1018. "optString": "non-extension field",
  1019. "optBool": true,
  1020. "optInt32": 42,
  1021. "[pb2.ExtensionsContainer.rpt_ext_enum]": [
  1022. "TEN",
  1023. 101,
  1024. "ONE"
  1025. ],
  1026. "[pb2.ExtensionsContainer.rpt_ext_nested]": [
  1027. {
  1028. "optString": "one"
  1029. },
  1030. {
  1031. "optString": "two"
  1032. },
  1033. {
  1034. "optString": "three"
  1035. }
  1036. ],
  1037. "[pb2.ExtensionsContainer.rpt_ext_string]": [
  1038. "hello",
  1039. "world"
  1040. ]
  1041. }`,
  1042. }, {
  1043. desc: "MessageSet",
  1044. input: func() proto.Message {
  1045. m := &pb2.MessageSet{}
  1046. setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
  1047. OptString: scalar.String("a messageset extension"),
  1048. })
  1049. setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
  1050. OptString: scalar.String("not a messageset extension"),
  1051. })
  1052. setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
  1053. OptString: scalar.String("just a regular extension"),
  1054. })
  1055. return m
  1056. }(),
  1057. want: `{
  1058. "[pb2.MessageSetExtension]": {
  1059. "optString": "a messageset extension"
  1060. },
  1061. "[pb2.MessageSetExtension.ext_nested]": {
  1062. "optString": "just a regular extension"
  1063. },
  1064. "[pb2.MessageSetExtension.not_message_set_extension]": {
  1065. "optString": "not a messageset extension"
  1066. }
  1067. }`,
  1068. }, {
  1069. desc: "not real MessageSet 1",
  1070. input: func() proto.Message {
  1071. m := &pb2.FakeMessageSet{}
  1072. setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1073. OptString: scalar.String("not a messageset extension"),
  1074. })
  1075. return m
  1076. }(),
  1077. want: `{
  1078. "[pb2.FakeMessageSetExtension.message_set_extension]": {
  1079. "optString": "not a messageset extension"
  1080. }
  1081. }`,
  1082. }, {
  1083. desc: "not real MessageSet 2",
  1084. input: func() proto.Message {
  1085. m := &pb2.MessageSet{}
  1086. setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1087. OptString: scalar.String("another not a messageset extension"),
  1088. })
  1089. return m
  1090. }(),
  1091. want: `{
  1092. "[pb2.message_set_extension]": {
  1093. "optString": "another not a messageset extension"
  1094. }
  1095. }`,
  1096. }, {
  1097. desc: "BoolValue empty",
  1098. input: &knownpb.BoolValue{},
  1099. want: `false`,
  1100. }, {
  1101. desc: "BoolValue",
  1102. input: &knownpb.BoolValue{Value: true},
  1103. want: `true`,
  1104. }, {
  1105. desc: "Int32Value empty",
  1106. input: &knownpb.Int32Value{},
  1107. want: `0`,
  1108. }, {
  1109. desc: "Int32Value",
  1110. input: &knownpb.Int32Value{Value: 42},
  1111. want: `42`,
  1112. }, {
  1113. desc: "Int64Value",
  1114. input: &knownpb.Int64Value{Value: 42},
  1115. want: `"42"`,
  1116. }, {
  1117. desc: "UInt32Value",
  1118. input: &knownpb.UInt32Value{Value: 42},
  1119. want: `42`,
  1120. }, {
  1121. desc: "UInt64Value",
  1122. input: &knownpb.UInt64Value{Value: 42},
  1123. want: `"42"`,
  1124. }, {
  1125. desc: "FloatValue",
  1126. input: &knownpb.FloatValue{Value: 1.02},
  1127. want: `1.02`,
  1128. }, {
  1129. desc: "FloatValue Infinity",
  1130. input: &knownpb.FloatValue{Value: float32(math.Inf(-1))},
  1131. want: `"-Infinity"`,
  1132. }, {
  1133. desc: "DoubleValue",
  1134. input: &knownpb.DoubleValue{Value: 1.02},
  1135. want: `1.02`,
  1136. }, {
  1137. desc: "DoubleValue NaN",
  1138. input: &knownpb.DoubleValue{Value: math.NaN()},
  1139. want: `"NaN"`,
  1140. }, {
  1141. desc: "StringValue empty",
  1142. input: &knownpb.StringValue{},
  1143. want: `""`,
  1144. }, {
  1145. desc: "StringValue",
  1146. input: &knownpb.StringValue{Value: "谷歌"},
  1147. want: `"谷歌"`,
  1148. }, {
  1149. desc: "StringValue with invalid UTF8 error",
  1150. input: &knownpb.StringValue{Value: "abc\xff"},
  1151. want: "\"abc\xff\"",
  1152. wantErr: true,
  1153. }, {
  1154. desc: "StringValue field with invalid UTF8 error",
  1155. input: &pb2.KnownTypes{
  1156. OptString: &knownpb.StringValue{Value: "abc\xff"},
  1157. },
  1158. want: "{\n \"optString\": \"abc\xff\"\n}",
  1159. wantErr: true,
  1160. }, {
  1161. desc: "BytesValue",
  1162. input: &knownpb.BytesValue{Value: []byte("hello")},
  1163. want: `"aGVsbG8="`,
  1164. }, {
  1165. desc: "Empty",
  1166. input: &knownpb.Empty{},
  1167. want: `{}`,
  1168. }, {
  1169. desc: "NullValue field",
  1170. input: &pb2.KnownTypes{OptNull: new(knownpb.NullValue)},
  1171. want: `{
  1172. "optNull": null
  1173. }`,
  1174. }, {
  1175. desc: "Value empty",
  1176. input: &knownpb.Value{},
  1177. wantErr: true,
  1178. }, {
  1179. desc: "Value empty field",
  1180. input: &pb2.KnownTypes{
  1181. OptValue: &knownpb.Value{},
  1182. },
  1183. wantErr: true,
  1184. }, {
  1185. desc: "Value contains NullValue",
  1186. input: &knownpb.Value{Kind: &knownpb.Value_NullValue{}},
  1187. want: `null`,
  1188. }, {
  1189. desc: "Value contains BoolValue",
  1190. input: &knownpb.Value{Kind: &knownpb.Value_BoolValue{}},
  1191. want: `false`,
  1192. }, {
  1193. desc: "Value contains NumberValue",
  1194. input: &knownpb.Value{Kind: &knownpb.Value_NumberValue{1.02}},
  1195. want: `1.02`,
  1196. }, {
  1197. desc: "Value contains StringValue",
  1198. input: &knownpb.Value{Kind: &knownpb.Value_StringValue{"hello"}},
  1199. want: `"hello"`,
  1200. }, {
  1201. desc: "Value contains StringValue with invalid UTF8",
  1202. input: &knownpb.Value{Kind: &knownpb.Value_StringValue{"\xff"}},
  1203. want: "\"\xff\"",
  1204. wantErr: true,
  1205. }, {
  1206. desc: "Value contains Struct",
  1207. input: &knownpb.Value{
  1208. Kind: &knownpb.Value_StructValue{
  1209. &knownpb.Struct{
  1210. Fields: map[string]*knownpb.Value{
  1211. "null": {Kind: &knownpb.Value_NullValue{}},
  1212. "number": {Kind: &knownpb.Value_NumberValue{}},
  1213. "string": {Kind: &knownpb.Value_StringValue{}},
  1214. "struct": {Kind: &knownpb.Value_StructValue{}},
  1215. "list": {Kind: &knownpb.Value_ListValue{}},
  1216. "bool": {Kind: &knownpb.Value_BoolValue{}},
  1217. },
  1218. },
  1219. },
  1220. },
  1221. want: `{
  1222. "bool": false,
  1223. "list": [],
  1224. "null": null,
  1225. "number": 0,
  1226. "string": "",
  1227. "struct": {}
  1228. }`,
  1229. }, {
  1230. desc: "Value contains ListValue",
  1231. input: &knownpb.Value{
  1232. Kind: &knownpb.Value_ListValue{
  1233. &knownpb.ListValue{
  1234. Values: []*knownpb.Value{
  1235. {Kind: &knownpb.Value_BoolValue{}},
  1236. {Kind: &knownpb.Value_NullValue{}},
  1237. {Kind: &knownpb.Value_NumberValue{}},
  1238. {Kind: &knownpb.Value_StringValue{}},
  1239. {Kind: &knownpb.Value_StructValue{}},
  1240. {Kind: &knownpb.Value_ListValue{}},
  1241. },
  1242. },
  1243. },
  1244. },
  1245. want: `[
  1246. false,
  1247. null,
  1248. 0,
  1249. "",
  1250. {},
  1251. []
  1252. ]`,
  1253. }, {
  1254. desc: "Struct with nil map",
  1255. input: &knownpb.Struct{},
  1256. want: `{}`,
  1257. }, {
  1258. desc: "Struct with empty map",
  1259. input: &knownpb.Struct{
  1260. Fields: map[string]*knownpb.Value{},
  1261. },
  1262. want: `{}`,
  1263. }, {
  1264. desc: "Struct",
  1265. input: &knownpb.Struct{
  1266. Fields: map[string]*knownpb.Value{
  1267. "bool": {Kind: &knownpb.Value_BoolValue{true}},
  1268. "null": {Kind: &knownpb.Value_NullValue{}},
  1269. "number": {Kind: &knownpb.Value_NumberValue{3.1415}},
  1270. "string": {Kind: &knownpb.Value_StringValue{"hello"}},
  1271. "struct": {
  1272. Kind: &knownpb.Value_StructValue{
  1273. &knownpb.Struct{
  1274. Fields: map[string]*knownpb.Value{
  1275. "string": {Kind: &knownpb.Value_StringValue{"world"}},
  1276. },
  1277. },
  1278. },
  1279. },
  1280. "list": {
  1281. Kind: &knownpb.Value_ListValue{
  1282. &knownpb.ListValue{
  1283. Values: []*knownpb.Value{
  1284. {Kind: &knownpb.Value_BoolValue{}},
  1285. {Kind: &knownpb.Value_NullValue{}},
  1286. {Kind: &knownpb.Value_NumberValue{}},
  1287. },
  1288. },
  1289. },
  1290. },
  1291. },
  1292. },
  1293. want: `{
  1294. "bool": true,
  1295. "list": [
  1296. false,
  1297. null,
  1298. 0
  1299. ],
  1300. "null": null,
  1301. "number": 3.1415,
  1302. "string": "hello",
  1303. "struct": {
  1304. "string": "world"
  1305. }
  1306. }`,
  1307. }, {
  1308. desc: "Struct message with invalid UTF8 string",
  1309. input: &knownpb.Struct{
  1310. Fields: map[string]*knownpb.Value{
  1311. "string": {Kind: &knownpb.Value_StringValue{"\xff"}},
  1312. },
  1313. },
  1314. want: "{\n \"string\": \"\xff\"\n}",
  1315. wantErr: true,
  1316. }, {
  1317. desc: "ListValue with nil values",
  1318. input: &knownpb.ListValue{},
  1319. want: `[]`,
  1320. }, {
  1321. desc: "ListValue with empty values",
  1322. input: &knownpb.ListValue{
  1323. Values: []*knownpb.Value{},
  1324. },
  1325. want: `[]`,
  1326. }, {
  1327. desc: "ListValue",
  1328. input: &knownpb.ListValue{
  1329. Values: []*knownpb.Value{
  1330. {Kind: &knownpb.Value_BoolValue{true}},
  1331. {Kind: &knownpb.Value_NullValue{}},
  1332. {Kind: &knownpb.Value_NumberValue{3.1415}},
  1333. {Kind: &knownpb.Value_StringValue{"hello"}},
  1334. {
  1335. Kind: &knownpb.Value_ListValue{
  1336. &knownpb.ListValue{
  1337. Values: []*knownpb.Value{
  1338. {Kind: &knownpb.Value_BoolValue{}},
  1339. {Kind: &knownpb.Value_NullValue{}},
  1340. {Kind: &knownpb.Value_NumberValue{}},
  1341. },
  1342. },
  1343. },
  1344. },
  1345. {
  1346. Kind: &knownpb.Value_StructValue{
  1347. &knownpb.Struct{
  1348. Fields: map[string]*knownpb.Value{
  1349. "string": {Kind: &knownpb.Value_StringValue{"world"}},
  1350. },
  1351. },
  1352. },
  1353. },
  1354. },
  1355. },
  1356. want: `[
  1357. true,
  1358. null,
  1359. 3.1415,
  1360. "hello",
  1361. [
  1362. false,
  1363. null,
  1364. 0
  1365. ],
  1366. {
  1367. "string": "world"
  1368. }
  1369. ]`,
  1370. }, {
  1371. desc: "ListValue with invalid UTF8 string",
  1372. input: &knownpb.ListValue{
  1373. Values: []*knownpb.Value{
  1374. {Kind: &knownpb.Value_StringValue{"\xff"}},
  1375. },
  1376. },
  1377. want: "[\n \"\xff\"\n]",
  1378. wantErr: true,
  1379. }, {
  1380. desc: "Duration empty",
  1381. input: &knownpb.Duration{},
  1382. want: `"0s"`,
  1383. }, {
  1384. desc: "Duration with secs",
  1385. input: &knownpb.Duration{Seconds: 3},
  1386. want: `"3s"`,
  1387. }, {
  1388. desc: "Duration with -secs",
  1389. input: &knownpb.Duration{Seconds: -3},
  1390. want: `"-3s"`,
  1391. }, {
  1392. desc: "Duration with nanos",
  1393. input: &knownpb.Duration{Nanos: 1e6},
  1394. want: `"0.001s"`,
  1395. }, {
  1396. desc: "Duration with -nanos",
  1397. input: &knownpb.Duration{Nanos: -1e6},
  1398. want: `"-0.001s"`,
  1399. }, {
  1400. desc: "Duration with large secs",
  1401. input: &knownpb.Duration{Seconds: 1e10, Nanos: 1},
  1402. want: `"10000000000.000000001s"`,
  1403. }, {
  1404. desc: "Duration with 6-digit nanos",
  1405. input: &knownpb.Duration{Nanos: 1e4},
  1406. want: `"0.000010s"`,
  1407. }, {
  1408. desc: "Duration with 3-digit nanos",
  1409. input: &knownpb.Duration{Nanos: 1e6},
  1410. want: `"0.001s"`,
  1411. }, {
  1412. desc: "Duration with -secs -nanos",
  1413. input: &knownpb.Duration{Seconds: -123, Nanos: -450},
  1414. want: `"-123.000000450s"`,
  1415. }, {
  1416. desc: "Duration max value",
  1417. input: &knownpb.Duration{Seconds: 315576000000, Nanos: 999999999},
  1418. want: `"315576000000.999999999s"`,
  1419. }, {
  1420. desc: "Duration min value",
  1421. input: &knownpb.Duration{Seconds: -315576000000, Nanos: -999999999},
  1422. want: `"-315576000000.999999999s"`,
  1423. }, {
  1424. desc: "Duration with +secs -nanos",
  1425. input: &knownpb.Duration{Seconds: 1, Nanos: -1},
  1426. wantErr: true,
  1427. }, {
  1428. desc: "Duration with -secs +nanos",
  1429. input: &knownpb.Duration{Seconds: -1, Nanos: 1},
  1430. wantErr: true,
  1431. }, {
  1432. desc: "Duration with +secs out of range",
  1433. input: &knownpb.Duration{Seconds: 315576000001},
  1434. wantErr: true,
  1435. }, {
  1436. desc: "Duration with -secs out of range",
  1437. input: &knownpb.Duration{Seconds: -315576000001},
  1438. wantErr: true,
  1439. }, {
  1440. desc: "Duration with +nanos out of range",
  1441. input: &knownpb.Duration{Seconds: 0, Nanos: 1e9},
  1442. wantErr: true,
  1443. }, {
  1444. desc: "Duration with -nanos out of range",
  1445. input: &knownpb.Duration{Seconds: 0, Nanos: -1e9},
  1446. wantErr: true,
  1447. }, {
  1448. desc: "Timestamp zero",
  1449. input: &knownpb.Timestamp{},
  1450. want: `"1970-01-01T00:00:00Z"`,
  1451. }, {
  1452. desc: "Timestamp",
  1453. input: &knownpb.Timestamp{Seconds: 1553036601},
  1454. want: `"2019-03-19T23:03:21Z"`,
  1455. }, {
  1456. desc: "Timestamp with nanos",
  1457. input: &knownpb.Timestamp{Seconds: 1553036601, Nanos: 1},
  1458. want: `"2019-03-19T23:03:21.000000001Z"`,
  1459. }, {
  1460. desc: "Timestamp with 6-digit nanos",
  1461. input: &knownpb.Timestamp{Nanos: 1e3},
  1462. want: `"1970-01-01T00:00:00.000001Z"`,
  1463. }, {
  1464. desc: "Timestamp with 3-digit nanos",
  1465. input: &knownpb.Timestamp{Nanos: 1e7},
  1466. want: `"1970-01-01T00:00:00.010Z"`,
  1467. }, {
  1468. desc: "Timestamp max value",
  1469. input: &knownpb.Timestamp{Seconds: 253402300799, Nanos: 999999999},
  1470. want: `"9999-12-31T23:59:59.999999999Z"`,
  1471. }, {
  1472. desc: "Timestamp min value",
  1473. input: &knownpb.Timestamp{Seconds: -62135596800},
  1474. want: `"0001-01-01T00:00:00Z"`,
  1475. }, {
  1476. desc: "Timestamp with +secs out of range",
  1477. input: &knownpb.Timestamp{Seconds: 253402300800},
  1478. wantErr: true,
  1479. }, {
  1480. desc: "Timestamp with -secs out of range",
  1481. input: &knownpb.Timestamp{Seconds: -62135596801},
  1482. wantErr: true,
  1483. }, {
  1484. desc: "Timestamp with -nanos",
  1485. input: &knownpb.Timestamp{Nanos: -1},
  1486. wantErr: true,
  1487. }, {
  1488. desc: "Timestamp with +nanos out of range",
  1489. input: &knownpb.Timestamp{Nanos: 1e9},
  1490. wantErr: true,
  1491. }, {
  1492. desc: "FieldMask empty",
  1493. input: &knownpb.FieldMask{},
  1494. want: `""`,
  1495. }, {
  1496. desc: "FieldMask",
  1497. input: &knownpb.FieldMask{
  1498. Paths: []string{
  1499. "foo",
  1500. "foo_bar",
  1501. "foo.bar_qux",
  1502. "_foo",
  1503. },
  1504. },
  1505. want: `"foo,fooBar,foo.barQux,Foo"`,
  1506. }, {
  1507. desc: "FieldMask error 1",
  1508. input: &knownpb.FieldMask{
  1509. Paths: []string{"foo_"},
  1510. },
  1511. wantErr: true,
  1512. }, {
  1513. desc: "FieldMask error 2",
  1514. input: &knownpb.FieldMask{
  1515. Paths: []string{"foo__bar"},
  1516. },
  1517. wantErr: true,
  1518. }, {
  1519. desc: "Any empty",
  1520. input: &knownpb.Any{},
  1521. want: `{}`,
  1522. }, {
  1523. desc: "Any with non-custom message",
  1524. mo: jsonpb.MarshalOptions{
  1525. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1526. },
  1527. input: func() proto.Message {
  1528. m := &pb2.Nested{
  1529. OptString: scalar.String("embedded inside Any"),
  1530. OptNested: &pb2.Nested{
  1531. OptString: scalar.String("inception"),
  1532. },
  1533. }
  1534. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1535. if err != nil {
  1536. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1537. }
  1538. return &knownpb.Any{
  1539. TypeUrl: "foo/pb2.Nested",
  1540. Value: b,
  1541. }
  1542. }(),
  1543. want: `{
  1544. "@type": "foo/pb2.Nested",
  1545. "optString": "embedded inside Any",
  1546. "optNested": {
  1547. "optString": "inception"
  1548. }
  1549. }`,
  1550. }, {
  1551. desc: "Any with empty embedded message",
  1552. mo: jsonpb.MarshalOptions{
  1553. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1554. },
  1555. input: &knownpb.Any{TypeUrl: "foo/pb2.Nested"},
  1556. want: `{
  1557. "@type": "foo/pb2.Nested"
  1558. }`,
  1559. }, {
  1560. desc: "Any without registered type",
  1561. mo: jsonpb.MarshalOptions{Resolver: preg.NewTypes()},
  1562. input: &knownpb.Any{TypeUrl: "foo/pb2.Nested"},
  1563. wantErr: true,
  1564. }, {
  1565. desc: "Any with missing required error",
  1566. mo: jsonpb.MarshalOptions{
  1567. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1568. },
  1569. input: func() proto.Message {
  1570. m := &pb2.PartialRequired{
  1571. OptString: scalar.String("embedded inside Any"),
  1572. }
  1573. b, err := proto.MarshalOptions{
  1574. AllowPartial: true,
  1575. Deterministic: true,
  1576. }.Marshal(m)
  1577. if err != nil {
  1578. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1579. }
  1580. return &knownpb.Any{
  1581. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1582. Value: b,
  1583. }
  1584. }(),
  1585. want: `{
  1586. "@type": "pb2.PartialRequired",
  1587. "optString": "embedded inside Any"
  1588. }`,
  1589. wantErr: true,
  1590. }, {
  1591. desc: "Any with partial required and AllowPartial",
  1592. mo: jsonpb.MarshalOptions{
  1593. AllowPartial: true,
  1594. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1595. },
  1596. input: func() proto.Message {
  1597. m := &pb2.PartialRequired{
  1598. OptString: scalar.String("embedded inside Any"),
  1599. }
  1600. b, err := proto.MarshalOptions{
  1601. AllowPartial: true,
  1602. Deterministic: true,
  1603. }.Marshal(m)
  1604. if err != nil {
  1605. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1606. }
  1607. return &knownpb.Any{
  1608. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1609. Value: b,
  1610. }
  1611. }(),
  1612. want: `{
  1613. "@type": "pb2.PartialRequired",
  1614. "optString": "embedded inside Any"
  1615. }`,
  1616. }, {
  1617. desc: "Any with invalid UTF8",
  1618. mo: jsonpb.MarshalOptions{
  1619. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1620. },
  1621. input: func() proto.Message {
  1622. m := &pb2.Nested{
  1623. OptString: scalar.String("abc\xff"),
  1624. }
  1625. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1626. if err != nil {
  1627. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1628. }
  1629. return &knownpb.Any{
  1630. TypeUrl: "foo/pb2.Nested",
  1631. Value: b,
  1632. }
  1633. }(),
  1634. want: `{
  1635. "@type": "foo/pb2.Nested",
  1636. "optString": "` + "abc\xff" + `"
  1637. }`,
  1638. wantErr: true,
  1639. }, {
  1640. desc: "Any with invalid value",
  1641. mo: jsonpb.MarshalOptions{
  1642. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1643. },
  1644. input: &knownpb.Any{
  1645. TypeUrl: "foo/pb2.Nested",
  1646. Value: dhex("80"),
  1647. },
  1648. wantErr: true,
  1649. }, {
  1650. desc: "Any with BoolValue",
  1651. mo: jsonpb.MarshalOptions{
  1652. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{})),
  1653. },
  1654. input: func() proto.Message {
  1655. m := &knownpb.BoolValue{Value: true}
  1656. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1657. if err != nil {
  1658. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1659. }
  1660. return &knownpb.Any{
  1661. TypeUrl: "type.googleapis.com/google.protobuf.BoolValue",
  1662. Value: b,
  1663. }
  1664. }(),
  1665. want: `{
  1666. "@type": "type.googleapis.com/google.protobuf.BoolValue",
  1667. "value": true
  1668. }`,
  1669. }, {
  1670. desc: "Any with Empty",
  1671. mo: jsonpb.MarshalOptions{
  1672. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Empty{})),
  1673. },
  1674. input: func() proto.Message {
  1675. m := &knownpb.Empty{}
  1676. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1677. if err != nil {
  1678. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1679. }
  1680. return &knownpb.Any{
  1681. TypeUrl: "type.googleapis.com/google.protobuf.Empty",
  1682. Value: b,
  1683. }
  1684. }(),
  1685. want: `{
  1686. "@type": "type.googleapis.com/google.protobuf.Empty",
  1687. "value": {}
  1688. }`,
  1689. }, {
  1690. desc: "Any with StringValue containing invalid UTF8",
  1691. mo: jsonpb.MarshalOptions{
  1692. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{})),
  1693. },
  1694. input: func() proto.Message {
  1695. m := &knownpb.StringValue{Value: "abcd"}
  1696. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1697. if err != nil {
  1698. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1699. }
  1700. return &knownpb.Any{
  1701. TypeUrl: "google.protobuf.StringValue",
  1702. Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
  1703. }
  1704. }(),
  1705. want: `{
  1706. "@type": "google.protobuf.StringValue",
  1707. "value": "` + "abc\xff" + `"
  1708. }`,
  1709. wantErr: true,
  1710. }, {
  1711. desc: "Any with Int64Value",
  1712. mo: jsonpb.MarshalOptions{
  1713. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Int64Value{})),
  1714. },
  1715. input: func() proto.Message {
  1716. m := &knownpb.Int64Value{Value: 42}
  1717. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1718. if err != nil {
  1719. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1720. }
  1721. return &knownpb.Any{
  1722. TypeUrl: "google.protobuf.Int64Value",
  1723. Value: b,
  1724. }
  1725. }(),
  1726. want: `{
  1727. "@type": "google.protobuf.Int64Value",
  1728. "value": "42"
  1729. }`,
  1730. }, {
  1731. desc: "Any with Duration",
  1732. mo: jsonpb.MarshalOptions{
  1733. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Duration{})),
  1734. },
  1735. input: func() proto.Message {
  1736. m := &knownpb.Duration{}
  1737. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1738. if err != nil {
  1739. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1740. }
  1741. return &knownpb.Any{
  1742. TypeUrl: "type.googleapis.com/google.protobuf.Duration",
  1743. Value: b,
  1744. }
  1745. }(),
  1746. want: `{
  1747. "@type": "type.googleapis.com/google.protobuf.Duration",
  1748. "value": "0s"
  1749. }`,
  1750. }, {
  1751. desc: "Any with empty Value",
  1752. mo: jsonpb.MarshalOptions{
  1753. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
  1754. },
  1755. input: func() proto.Message {
  1756. m := &knownpb.Value{}
  1757. b, err := proto.Marshal(m)
  1758. if err != nil {
  1759. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1760. }
  1761. return &knownpb.Any{
  1762. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1763. Value: b,
  1764. }
  1765. }(),
  1766. wantErr: true,
  1767. }, {
  1768. desc: "Any with Value of StringValue",
  1769. mo: jsonpb.MarshalOptions{
  1770. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
  1771. },
  1772. input: func() proto.Message {
  1773. m := &knownpb.Value{Kind: &knownpb.Value_StringValue{"abcd"}}
  1774. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1775. if err != nil {
  1776. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1777. }
  1778. return &knownpb.Any{
  1779. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1780. Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
  1781. }
  1782. }(),
  1783. want: `{
  1784. "@type": "type.googleapis.com/google.protobuf.Value",
  1785. "value": "` + "abc\xff" + `"
  1786. }`,
  1787. wantErr: true,
  1788. }, {
  1789. desc: "Any with Value of NullValue",
  1790. mo: jsonpb.MarshalOptions{
  1791. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
  1792. },
  1793. input: func() proto.Message {
  1794. m := &knownpb.Value{Kind: &knownpb.Value_NullValue{}}
  1795. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1796. if err != nil {
  1797. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1798. }
  1799. return &knownpb.Any{
  1800. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1801. Value: b,
  1802. }
  1803. }(),
  1804. want: `{
  1805. "@type": "type.googleapis.com/google.protobuf.Value",
  1806. "value": null
  1807. }`,
  1808. }, {
  1809. desc: "Any with Struct",
  1810. mo: jsonpb.MarshalOptions{
  1811. Resolver: preg.NewTypes(
  1812. pimpl.Export{}.MessageTypeOf(&knownpb.Struct{}),
  1813. pimpl.Export{}.MessageTypeOf(&knownpb.Value{}),
  1814. pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{}),
  1815. pimpl.Export{}.EnumTypeOf(knownpb.NullValue_NULL_VALUE),
  1816. pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{}),
  1817. ),
  1818. },
  1819. input: func() proto.Message {
  1820. m := &knownpb.Struct{
  1821. Fields: map[string]*knownpb.Value{
  1822. "bool": {Kind: &knownpb.Value_BoolValue{true}},
  1823. "null": {Kind: &knownpb.Value_NullValue{}},
  1824. "string": {Kind: &knownpb.Value_StringValue{"hello"}},
  1825. "struct": {
  1826. Kind: &knownpb.Value_StructValue{
  1827. &knownpb.Struct{
  1828. Fields: map[string]*knownpb.Value{
  1829. "string": {Kind: &knownpb.Value_StringValue{"world"}},
  1830. },
  1831. },
  1832. },
  1833. },
  1834. },
  1835. }
  1836. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1837. if err != nil {
  1838. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1839. }
  1840. return &knownpb.Any{
  1841. TypeUrl: "google.protobuf.Struct",
  1842. Value: b,
  1843. }
  1844. }(),
  1845. want: `{
  1846. "@type": "google.protobuf.Struct",
  1847. "value": {
  1848. "bool": true,
  1849. "null": null,
  1850. "string": "hello",
  1851. "struct": {
  1852. "string": "world"
  1853. }
  1854. }
  1855. }`,
  1856. }, {
  1857. desc: "Any with missing type_url",
  1858. mo: jsonpb.MarshalOptions{
  1859. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{})),
  1860. },
  1861. input: func() proto.Message {
  1862. m := &knownpb.BoolValue{Value: true}
  1863. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1864. if err != nil {
  1865. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1866. }
  1867. return &knownpb.Any{
  1868. Value: b,
  1869. }
  1870. }(),
  1871. wantErr: true,
  1872. }, {
  1873. desc: "well known types as field values",
  1874. mo: jsonpb.MarshalOptions{
  1875. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Empty{})),
  1876. },
  1877. input: &pb2.KnownTypes{
  1878. OptBool: &knownpb.BoolValue{Value: false},
  1879. OptInt32: &knownpb.Int32Value{Value: 42},
  1880. OptInt64: &knownpb.Int64Value{Value: 42},
  1881. OptUint32: &knownpb.UInt32Value{Value: 42},
  1882. OptUint64: &knownpb.UInt64Value{Value: 42},
  1883. OptFloat: &knownpb.FloatValue{Value: 1.23},
  1884. OptDouble: &knownpb.DoubleValue{Value: 3.1415},
  1885. OptString: &knownpb.StringValue{Value: "hello"},
  1886. OptBytes: &knownpb.BytesValue{Value: []byte("hello")},
  1887. OptDuration: &knownpb.Duration{Seconds: 123},
  1888. OptTimestamp: &knownpb.Timestamp{Seconds: 1553036601},
  1889. OptStruct: &knownpb.Struct{
  1890. Fields: map[string]*knownpb.Value{
  1891. "string": {Kind: &knownpb.Value_StringValue{"hello"}},
  1892. },
  1893. },
  1894. OptList: &knownpb.ListValue{
  1895. Values: []*knownpb.Value{
  1896. {Kind: &knownpb.Value_NullValue{}},
  1897. {Kind: &knownpb.Value_StringValue{}},
  1898. {Kind: &knownpb.Value_StructValue{}},
  1899. {Kind: &knownpb.Value_ListValue{}},
  1900. },
  1901. },
  1902. OptValue: &knownpb.Value{
  1903. Kind: &knownpb.Value_StringValue{"world"},
  1904. },
  1905. OptEmpty: &knownpb.Empty{},
  1906. OptAny: &knownpb.Any{
  1907. TypeUrl: "google.protobuf.Empty",
  1908. },
  1909. OptFieldmask: &knownpb.FieldMask{
  1910. Paths: []string{"foo_bar", "bar_foo"},
  1911. },
  1912. },
  1913. want: `{
  1914. "optBool": false,
  1915. "optInt32": 42,
  1916. "optInt64": "42",
  1917. "optUint32": 42,
  1918. "optUint64": "42",
  1919. "optFloat": 1.23,
  1920. "optDouble": 3.1415,
  1921. "optString": "hello",
  1922. "optBytes": "aGVsbG8=",
  1923. "optDuration": "123s",
  1924. "optTimestamp": "2019-03-19T23:03:21Z",
  1925. "optStruct": {
  1926. "string": "hello"
  1927. },
  1928. "optList": [
  1929. null,
  1930. "",
  1931. {},
  1932. []
  1933. ],
  1934. "optValue": "world",
  1935. "optEmpty": {},
  1936. "optAny": {
  1937. "@type": "google.protobuf.Empty",
  1938. "value": {}
  1939. },
  1940. "optFieldmask": "fooBar,barFoo"
  1941. }`,
  1942. }}
  1943. for _, tt := range tests {
  1944. tt := tt
  1945. t.Run(tt.desc, func(t *testing.T) {
  1946. // Use 2-space indentation on all MarshalOptions.
  1947. tt.mo.Indent = " "
  1948. b, err := tt.mo.Marshal(tt.input)
  1949. if err != nil && !tt.wantErr {
  1950. t.Errorf("Marshal() returned error: %v\n", err)
  1951. }
  1952. if err == nil && tt.wantErr {
  1953. t.Errorf("Marshal() got nil error, want error\n")
  1954. }
  1955. got := string(b)
  1956. if got != tt.want {
  1957. t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
  1958. if diff := cmp.Diff(tt.want, got, splitLines); diff != "" {
  1959. t.Errorf("Marshal() diff -want +got\n%v\n", diff)
  1960. }
  1961. }
  1962. })
  1963. }
  1964. }