encode_test.go 39 KB

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