decode_test.go 63 KB

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