encode_test.go 50 KB

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