encode_test.go 50 KB

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