encode_test.go 46 KB

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