encode_test.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  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. for _, tt := range tests {
  1893. tt := tt
  1894. if tt.skip {
  1895. continue
  1896. }
  1897. t.Run(tt.desc, func(t *testing.T) {
  1898. // Use 2-space indentation on all MarshalOptions.
  1899. tt.mo.Indent = " "
  1900. b, err := tt.mo.Marshal(tt.input)
  1901. if err != nil && !tt.wantErr {
  1902. t.Errorf("Marshal() returned error: %v\n", err)
  1903. }
  1904. if err == nil && tt.wantErr {
  1905. t.Errorf("Marshal() got nil error, want error\n")
  1906. }
  1907. got := string(b)
  1908. if got != tt.want {
  1909. t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
  1910. if diff := cmp.Diff(tt.want, got); diff != "" {
  1911. t.Errorf("Marshal() diff -want +got\n%v\n", diff)
  1912. }
  1913. }
  1914. })
  1915. }
  1916. }