encode_test.go 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  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",
  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. }, {
  1581. desc: "Any with partial required and AllowPartial",
  1582. mo: protojson.MarshalOptions{
  1583. AllowPartial: true,
  1584. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1585. },
  1586. input: func() proto.Message {
  1587. m := &pb2.PartialRequired{
  1588. OptString: scalar.String("embedded inside Any"),
  1589. }
  1590. b, err := proto.MarshalOptions{
  1591. AllowPartial: true,
  1592. Deterministic: true,
  1593. }.Marshal(m)
  1594. if err != nil {
  1595. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1596. }
  1597. return &anypb.Any{
  1598. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1599. Value: b,
  1600. }
  1601. }(),
  1602. want: `{
  1603. "@type": "pb2.PartialRequired",
  1604. "optString": "embedded inside Any"
  1605. }`,
  1606. }, {
  1607. desc: "Any with invalid UTF8",
  1608. mo: protojson.MarshalOptions{
  1609. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1610. },
  1611. input: func() proto.Message {
  1612. m := &pb2.Nested{
  1613. OptString: scalar.String("abc\xff"),
  1614. }
  1615. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1616. if err != nil {
  1617. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1618. }
  1619. return &anypb.Any{
  1620. TypeUrl: "foo/pb2.Nested",
  1621. Value: b,
  1622. }
  1623. }(),
  1624. want: `{
  1625. "@type": "foo/pb2.Nested",
  1626. "optString": "` + "abc\xff" + `"
  1627. }`,
  1628. wantErr: true,
  1629. }, {
  1630. desc: "Any with invalid value",
  1631. mo: protojson.MarshalOptions{
  1632. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1633. },
  1634. input: &anypb.Any{
  1635. TypeUrl: "foo/pb2.Nested",
  1636. Value: dhex("80"),
  1637. },
  1638. wantErr: true,
  1639. }, {
  1640. desc: "Any with BoolValue",
  1641. mo: protojson.MarshalOptions{
  1642. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{})),
  1643. },
  1644. input: func() proto.Message {
  1645. m := &wrapperspb.BoolValue{Value: true}
  1646. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1647. if err != nil {
  1648. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1649. }
  1650. return &anypb.Any{
  1651. TypeUrl: "type.googleapis.com/google.protobuf.BoolValue",
  1652. Value: b,
  1653. }
  1654. }(),
  1655. want: `{
  1656. "@type": "type.googleapis.com/google.protobuf.BoolValue",
  1657. "value": true
  1658. }`,
  1659. }, {
  1660. desc: "Any with Empty",
  1661. mo: protojson.MarshalOptions{
  1662. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
  1663. },
  1664. input: func() proto.Message {
  1665. m := &emptypb.Empty{}
  1666. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1667. if err != nil {
  1668. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1669. }
  1670. return &anypb.Any{
  1671. TypeUrl: "type.googleapis.com/google.protobuf.Empty",
  1672. Value: b,
  1673. }
  1674. }(),
  1675. want: `{
  1676. "@type": "type.googleapis.com/google.protobuf.Empty",
  1677. "value": {}
  1678. }`,
  1679. }, {
  1680. desc: "Any with StringValue containing invalid UTF8",
  1681. mo: protojson.MarshalOptions{
  1682. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{})),
  1683. },
  1684. input: func() proto.Message {
  1685. m := &wrapperspb.StringValue{Value: "abcd"}
  1686. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1687. if err != nil {
  1688. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1689. }
  1690. return &anypb.Any{
  1691. TypeUrl: "google.protobuf.StringValue",
  1692. Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
  1693. }
  1694. }(),
  1695. want: `{
  1696. "@type": "google.protobuf.StringValue",
  1697. "value": "` + "abc\xff" + `"
  1698. }`,
  1699. wantErr: true,
  1700. }, {
  1701. desc: "Any with Int64Value",
  1702. mo: protojson.MarshalOptions{
  1703. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.Int64Value{})),
  1704. },
  1705. input: func() proto.Message {
  1706. m := &wrapperspb.Int64Value{Value: 42}
  1707. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1708. if err != nil {
  1709. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1710. }
  1711. return &anypb.Any{
  1712. TypeUrl: "google.protobuf.Int64Value",
  1713. Value: b,
  1714. }
  1715. }(),
  1716. want: `{
  1717. "@type": "google.protobuf.Int64Value",
  1718. "value": "42"
  1719. }`,
  1720. }, {
  1721. desc: "Any with Duration",
  1722. mo: protojson.MarshalOptions{
  1723. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&durationpb.Duration{})),
  1724. },
  1725. input: func() proto.Message {
  1726. m := &durationpb.Duration{}
  1727. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1728. if err != nil {
  1729. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1730. }
  1731. return &anypb.Any{
  1732. TypeUrl: "type.googleapis.com/google.protobuf.Duration",
  1733. Value: b,
  1734. }
  1735. }(),
  1736. want: `{
  1737. "@type": "type.googleapis.com/google.protobuf.Duration",
  1738. "value": "0s"
  1739. }`,
  1740. }, {
  1741. desc: "Any with empty Value",
  1742. mo: protojson.MarshalOptions{
  1743. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  1744. },
  1745. input: func() proto.Message {
  1746. m := &structpb.Value{}
  1747. b, err := proto.Marshal(m)
  1748. if err != nil {
  1749. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1750. }
  1751. return &anypb.Any{
  1752. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1753. Value: b,
  1754. }
  1755. }(),
  1756. wantErr: true,
  1757. }, {
  1758. desc: "Any with Value of StringValue",
  1759. mo: protojson.MarshalOptions{
  1760. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  1761. },
  1762. input: func() proto.Message {
  1763. m := &structpb.Value{Kind: &structpb.Value_StringValue{"abcd"}}
  1764. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1765. if err != nil {
  1766. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1767. }
  1768. return &anypb.Any{
  1769. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1770. Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
  1771. }
  1772. }(),
  1773. want: `{
  1774. "@type": "type.googleapis.com/google.protobuf.Value",
  1775. "value": "` + "abc\xff" + `"
  1776. }`,
  1777. wantErr: true,
  1778. }, {
  1779. desc: "Any with Value of NullValue",
  1780. mo: protojson.MarshalOptions{
  1781. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  1782. },
  1783. input: func() proto.Message {
  1784. m := &structpb.Value{Kind: &structpb.Value_NullValue{}}
  1785. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1786. if err != nil {
  1787. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1788. }
  1789. return &anypb.Any{
  1790. TypeUrl: "type.googleapis.com/google.protobuf.Value",
  1791. Value: b,
  1792. }
  1793. }(),
  1794. want: `{
  1795. "@type": "type.googleapis.com/google.protobuf.Value",
  1796. "value": null
  1797. }`,
  1798. }, {
  1799. desc: "Any with Struct",
  1800. mo: protojson.MarshalOptions{
  1801. Resolver: preg.NewTypes(
  1802. pimpl.Export{}.MessageTypeOf(&structpb.Struct{}),
  1803. pimpl.Export{}.MessageTypeOf(&structpb.Value{}),
  1804. pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{}),
  1805. pimpl.Export{}.EnumTypeOf(structpb.NullValue_NULL_VALUE),
  1806. pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{}),
  1807. ),
  1808. },
  1809. input: func() proto.Message {
  1810. m := &structpb.Struct{
  1811. Fields: map[string]*structpb.Value{
  1812. "bool": {Kind: &structpb.Value_BoolValue{true}},
  1813. "null": {Kind: &structpb.Value_NullValue{}},
  1814. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  1815. "struct": {
  1816. Kind: &structpb.Value_StructValue{
  1817. &structpb.Struct{
  1818. Fields: map[string]*structpb.Value{
  1819. "string": {Kind: &structpb.Value_StringValue{"world"}},
  1820. },
  1821. },
  1822. },
  1823. },
  1824. },
  1825. }
  1826. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1827. if err != nil {
  1828. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1829. }
  1830. return &anypb.Any{
  1831. TypeUrl: "google.protobuf.Struct",
  1832. Value: b,
  1833. }
  1834. }(),
  1835. want: `{
  1836. "@type": "google.protobuf.Struct",
  1837. "value": {
  1838. "bool": true,
  1839. "null": null,
  1840. "string": "hello",
  1841. "struct": {
  1842. "string": "world"
  1843. }
  1844. }
  1845. }`,
  1846. }, {
  1847. desc: "Any with missing type_url",
  1848. mo: protojson.MarshalOptions{
  1849. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{})),
  1850. },
  1851. input: func() proto.Message {
  1852. m := &wrapperspb.BoolValue{Value: true}
  1853. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1854. if err != nil {
  1855. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1856. }
  1857. return &anypb.Any{
  1858. Value: b,
  1859. }
  1860. }(),
  1861. wantErr: true,
  1862. }, {
  1863. desc: "well known types as field values",
  1864. mo: protojson.MarshalOptions{
  1865. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
  1866. },
  1867. input: &pb2.KnownTypes{
  1868. OptBool: &wrapperspb.BoolValue{Value: false},
  1869. OptInt32: &wrapperspb.Int32Value{Value: 42},
  1870. OptInt64: &wrapperspb.Int64Value{Value: 42},
  1871. OptUint32: &wrapperspb.UInt32Value{Value: 42},
  1872. OptUint64: &wrapperspb.UInt64Value{Value: 42},
  1873. OptFloat: &wrapperspb.FloatValue{Value: 1.23},
  1874. OptDouble: &wrapperspb.DoubleValue{Value: 3.1415},
  1875. OptString: &wrapperspb.StringValue{Value: "hello"},
  1876. OptBytes: &wrapperspb.BytesValue{Value: []byte("hello")},
  1877. OptDuration: &durationpb.Duration{Seconds: 123},
  1878. OptTimestamp: &timestamppb.Timestamp{Seconds: 1553036601},
  1879. OptStruct: &structpb.Struct{
  1880. Fields: map[string]*structpb.Value{
  1881. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  1882. },
  1883. },
  1884. OptList: &structpb.ListValue{
  1885. Values: []*structpb.Value{
  1886. {Kind: &structpb.Value_NullValue{}},
  1887. {Kind: &structpb.Value_StringValue{}},
  1888. {Kind: &structpb.Value_StructValue{}},
  1889. {Kind: &structpb.Value_ListValue{}},
  1890. },
  1891. },
  1892. OptValue: &structpb.Value{
  1893. Kind: &structpb.Value_StringValue{"world"},
  1894. },
  1895. OptEmpty: &emptypb.Empty{},
  1896. OptAny: &anypb.Any{
  1897. TypeUrl: "google.protobuf.Empty",
  1898. },
  1899. OptFieldmask: &fieldmaskpb.FieldMask{
  1900. Paths: []string{"foo_bar", "bar_foo"},
  1901. },
  1902. },
  1903. want: `{
  1904. "optBool": false,
  1905. "optInt32": 42,
  1906. "optInt64": "42",
  1907. "optUint32": 42,
  1908. "optUint64": "42",
  1909. "optFloat": 1.23,
  1910. "optDouble": 3.1415,
  1911. "optString": "hello",
  1912. "optBytes": "aGVsbG8=",
  1913. "optDuration": "123s",
  1914. "optTimestamp": "2019-03-19T23:03:21Z",
  1915. "optStruct": {
  1916. "string": "hello"
  1917. },
  1918. "optList": [
  1919. null,
  1920. "",
  1921. {},
  1922. []
  1923. ],
  1924. "optValue": "world",
  1925. "optEmpty": {},
  1926. "optAny": {
  1927. "@type": "google.protobuf.Empty",
  1928. "value": {}
  1929. },
  1930. "optFieldmask": "fooBar,barFoo"
  1931. }`,
  1932. }}
  1933. for _, tt := range tests {
  1934. tt := tt
  1935. t.Run(tt.desc, func(t *testing.T) {
  1936. // Use 2-space indentation on all MarshalOptions.
  1937. tt.mo.Indent = " "
  1938. b, err := tt.mo.Marshal(tt.input)
  1939. if err != nil && !tt.wantErr {
  1940. t.Errorf("Marshal() returned error: %v\n", err)
  1941. }
  1942. if err == nil && tt.wantErr {
  1943. t.Errorf("Marshal() got nil error, want error\n")
  1944. }
  1945. got := string(b)
  1946. if got != tt.want {
  1947. t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
  1948. if diff := cmp.Diff(tt.want, got, splitLines); diff != "" {
  1949. t.Errorf("Marshal() diff -want +got\n%v\n", diff)
  1950. }
  1951. }
  1952. })
  1953. }
  1954. }