decode_test.go 47 KB

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