decode_test.go 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  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: "proto group name",
  469. inputMessage: &pb2.Nests{},
  470. inputText: `{
  471. "OptGroup": {"optString": "hello"},
  472. "RptGroup": [{"rptString": ["goodbye"]}]
  473. }`,
  474. wantMessage: &pb2.Nests{
  475. Optgroup: &pb2.Nests_OptGroup{OptString: proto.String("hello")},
  476. Rptgroup: []*pb2.Nests_RptGroup{{RptString: []string{"goodbye"}}},
  477. },
  478. }, {
  479. desc: "json_name",
  480. inputMessage: &pb3.JSONNames{},
  481. inputText: `{
  482. "foo_bar": "json_name used"
  483. }`,
  484. wantMessage: &pb3.JSONNames{
  485. SString: "json_name used",
  486. },
  487. }, {
  488. desc: "camelCase name",
  489. inputMessage: &pb3.JSONNames{},
  490. inputText: `{
  491. "sString": "camelcase used"
  492. }`,
  493. wantErr: true,
  494. }, {
  495. desc: "proto name and json_name",
  496. inputMessage: &pb3.JSONNames{},
  497. inputText: `{
  498. "foo_bar": "json_name used",
  499. "s_string": "proto name used"
  500. }`,
  501. wantErr: true,
  502. }, {
  503. desc: "duplicate field names",
  504. inputMessage: &pb3.JSONNames{},
  505. inputText: `{
  506. "foo_bar": "one",
  507. "foo_bar": "two",
  508. }`,
  509. wantErr: true,
  510. }, {
  511. desc: "null message",
  512. inputMessage: &pb2.Nests{},
  513. inputText: "null",
  514. wantErr: true,
  515. }, {
  516. desc: "proto2 nested message not set",
  517. inputMessage: &pb2.Nests{},
  518. inputText: "{}",
  519. wantMessage: &pb2.Nests{},
  520. }, {
  521. desc: "proto2 nested message set to null",
  522. inputMessage: &pb2.Nests{},
  523. inputText: `{
  524. "optNested": null,
  525. "optgroup": null
  526. }`,
  527. wantMessage: &pb2.Nests{},
  528. }, {
  529. desc: "proto2 nested message set to empty",
  530. inputMessage: &pb2.Nests{},
  531. inputText: `{
  532. "optNested": {},
  533. "optgroup": {}
  534. }`,
  535. wantMessage: &pb2.Nests{
  536. OptNested: &pb2.Nested{},
  537. Optgroup: &pb2.Nests_OptGroup{},
  538. },
  539. }, {
  540. desc: "proto2 nested messages",
  541. inputMessage: &pb2.Nests{},
  542. inputText: `{
  543. "optNested": {
  544. "optString": "nested message",
  545. "optNested": {
  546. "optString": "another nested message"
  547. }
  548. }
  549. }`,
  550. wantMessage: &pb2.Nests{
  551. OptNested: &pb2.Nested{
  552. OptString: proto.String("nested message"),
  553. OptNested: &pb2.Nested{
  554. OptString: proto.String("another nested message"),
  555. },
  556. },
  557. },
  558. }, {
  559. desc: "proto2 groups",
  560. inputMessage: &pb2.Nests{},
  561. inputText: `{
  562. "optgroup": {
  563. "optString": "inside a group",
  564. "optNested": {
  565. "optString": "nested message inside a group"
  566. },
  567. "optnestedgroup": {
  568. "optFixed32": 47
  569. }
  570. }
  571. }`,
  572. wantMessage: &pb2.Nests{
  573. Optgroup: &pb2.Nests_OptGroup{
  574. OptString: proto.String("inside a group"),
  575. OptNested: &pb2.Nested{
  576. OptString: proto.String("nested message inside a group"),
  577. },
  578. Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
  579. OptFixed32: proto.Uint32(47),
  580. },
  581. },
  582. },
  583. }, {
  584. desc: "proto3 nested message not set",
  585. inputMessage: &pb3.Nests{},
  586. inputText: "{}",
  587. wantMessage: &pb3.Nests{},
  588. }, {
  589. desc: "proto3 nested message set to null",
  590. inputMessage: &pb3.Nests{},
  591. inputText: `{"sNested": null}`,
  592. wantMessage: &pb3.Nests{},
  593. }, {
  594. desc: "proto3 nested message set to empty",
  595. inputMessage: &pb3.Nests{},
  596. inputText: `{"sNested": {}}`,
  597. wantMessage: &pb3.Nests{
  598. SNested: &pb3.Nested{},
  599. },
  600. }, {
  601. desc: "proto3 nested message",
  602. inputMessage: &pb3.Nests{},
  603. inputText: `{
  604. "sNested": {
  605. "sString": "nested message",
  606. "sNested": {
  607. "sString": "another nested message"
  608. }
  609. }
  610. }`,
  611. wantMessage: &pb3.Nests{
  612. SNested: &pb3.Nested{
  613. SString: "nested message",
  614. SNested: &pb3.Nested{
  615. SString: "another nested message",
  616. },
  617. },
  618. },
  619. }, {
  620. desc: "message set to non-message",
  621. inputMessage: &pb3.Nests{},
  622. inputText: `"not valid"`,
  623. wantErr: true,
  624. }, {
  625. desc: "nested message set to non-message",
  626. inputMessage: &pb3.Nests{},
  627. inputText: `{"sNested": true}`,
  628. wantErr: true,
  629. }, {
  630. desc: "oneof not set",
  631. inputMessage: &pb3.Oneofs{},
  632. inputText: "{}",
  633. wantMessage: &pb3.Oneofs{},
  634. }, {
  635. desc: "oneof set to empty string",
  636. inputMessage: &pb3.Oneofs{},
  637. inputText: `{"oneofString": ""}`,
  638. wantMessage: &pb3.Oneofs{
  639. Union: &pb3.Oneofs_OneofString{},
  640. },
  641. }, {
  642. desc: "oneof set to string",
  643. inputMessage: &pb3.Oneofs{},
  644. inputText: `{"oneofString": "hello"}`,
  645. wantMessage: &pb3.Oneofs{
  646. Union: &pb3.Oneofs_OneofString{
  647. OneofString: "hello",
  648. },
  649. },
  650. }, {
  651. desc: "oneof set to enum",
  652. inputMessage: &pb3.Oneofs{},
  653. inputText: `{"oneofEnum": "ZERO"}`,
  654. wantMessage: &pb3.Oneofs{
  655. Union: &pb3.Oneofs_OneofEnum{
  656. OneofEnum: pb3.Enum_ZERO,
  657. },
  658. },
  659. }, {
  660. desc: "oneof set to empty message",
  661. inputMessage: &pb3.Oneofs{},
  662. inputText: `{"oneofNested": {}}`,
  663. wantMessage: &pb3.Oneofs{
  664. Union: &pb3.Oneofs_OneofNested{
  665. OneofNested: &pb3.Nested{},
  666. },
  667. },
  668. }, {
  669. desc: "oneof set to message",
  670. inputMessage: &pb3.Oneofs{},
  671. inputText: `{
  672. "oneofNested": {
  673. "sString": "nested message"
  674. }
  675. }`,
  676. wantMessage: &pb3.Oneofs{
  677. Union: &pb3.Oneofs_OneofNested{
  678. OneofNested: &pb3.Nested{
  679. SString: "nested message",
  680. },
  681. },
  682. },
  683. }, {
  684. desc: "oneof set to more than one field",
  685. inputMessage: &pb3.Oneofs{},
  686. inputText: `{
  687. "oneofEnum": "ZERO",
  688. "oneofString": "hello"
  689. }`,
  690. wantErr: true,
  691. }, {
  692. desc: "oneof set to null and value",
  693. inputMessage: &pb3.Oneofs{},
  694. inputText: `{
  695. "oneofEnum": "ZERO",
  696. "oneofString": null
  697. }`,
  698. wantMessage: &pb3.Oneofs{
  699. Union: &pb3.Oneofs_OneofEnum{
  700. OneofEnum: pb3.Enum_ZERO,
  701. },
  702. },
  703. }, {
  704. desc: "repeated null fields",
  705. inputMessage: &pb2.Repeats{},
  706. inputText: `{
  707. "rptString": null,
  708. "rptInt32" : null,
  709. "rptFloat" : null,
  710. "rptBytes" : null
  711. }`,
  712. wantMessage: &pb2.Repeats{},
  713. }, {
  714. desc: "repeated scalars",
  715. inputMessage: &pb2.Repeats{},
  716. inputText: `{
  717. "rptString": ["hello", "world"],
  718. "rptInt32" : [-1, 0, 1],
  719. "rptBool" : [false, true]
  720. }`,
  721. wantMessage: &pb2.Repeats{
  722. RptString: []string{"hello", "world"},
  723. RptInt32: []int32{-1, 0, 1},
  724. RptBool: []bool{false, true},
  725. },
  726. }, {
  727. desc: "repeated enums",
  728. inputMessage: &pb2.Enums{},
  729. inputText: `{
  730. "rptEnum" : ["TEN", 1, 42],
  731. "rptNestedEnum": ["DOS", 2, -47]
  732. }`,
  733. wantMessage: &pb2.Enums{
  734. RptEnum: []pb2.Enum{pb2.Enum_TEN, pb2.Enum_ONE, 42},
  735. RptNestedEnum: []pb2.Enums_NestedEnum{pb2.Enums_DOS, pb2.Enums_DOS, -47},
  736. },
  737. }, {
  738. desc: "repeated messages",
  739. inputMessage: &pb2.Nests{},
  740. inputText: `{
  741. "rptNested": [
  742. {
  743. "optString": "repeat nested one"
  744. },
  745. {
  746. "optString": "repeat nested two",
  747. "optNested": {
  748. "optString": "inside repeat nested two"
  749. }
  750. },
  751. {}
  752. ]
  753. }`,
  754. wantMessage: &pb2.Nests{
  755. RptNested: []*pb2.Nested{
  756. {
  757. OptString: proto.String("repeat nested one"),
  758. },
  759. {
  760. OptString: proto.String("repeat nested two"),
  761. OptNested: &pb2.Nested{
  762. OptString: proto.String("inside repeat nested two"),
  763. },
  764. },
  765. {},
  766. },
  767. },
  768. }, {
  769. desc: "repeated groups",
  770. inputMessage: &pb2.Nests{},
  771. inputText: `{
  772. "rptgroup": [
  773. {
  774. "rptString": ["hello", "world"]
  775. },
  776. {}
  777. ]
  778. }
  779. `,
  780. wantMessage: &pb2.Nests{
  781. Rptgroup: []*pb2.Nests_RptGroup{
  782. {
  783. RptString: []string{"hello", "world"},
  784. },
  785. {},
  786. },
  787. },
  788. }, {
  789. desc: "repeated string contains invalid UTF8",
  790. inputMessage: &pb2.Repeats{},
  791. inputText: `{"rptString": ["` + "abc\xff" + `"]}`,
  792. wantErr: true,
  793. }, {
  794. desc: "repeated messages contain invalid UTF8",
  795. inputMessage: &pb2.Nests{},
  796. inputText: `{"rptNested": [{"optString": "` + "abc\xff" + `"}]}`,
  797. wantErr: true,
  798. }, {
  799. desc: "repeated scalars contain invalid type",
  800. inputMessage: &pb2.Repeats{},
  801. inputText: `{"rptString": ["hello", null, "world"]}`,
  802. wantErr: true,
  803. }, {
  804. desc: "repeated messages contain invalid type",
  805. inputMessage: &pb2.Nests{},
  806. inputText: `{"rptNested": [{}, null]}`,
  807. wantErr: true,
  808. }, {
  809. desc: "map fields 1",
  810. inputMessage: &pb3.Maps{},
  811. inputText: `{
  812. "int32ToStr": {
  813. "-101": "-101",
  814. "0" : "zero",
  815. "255" : "0xff"
  816. },
  817. "boolToUint32": {
  818. "false": 101,
  819. "true" : "42"
  820. }
  821. }`,
  822. wantMessage: &pb3.Maps{
  823. Int32ToStr: map[int32]string{
  824. -101: "-101",
  825. 0xff: "0xff",
  826. 0: "zero",
  827. },
  828. BoolToUint32: map[bool]uint32{
  829. true: 42,
  830. false: 101,
  831. },
  832. },
  833. }, {
  834. desc: "map fields 2",
  835. inputMessage: &pb3.Maps{},
  836. inputText: `{
  837. "uint64ToEnum": {
  838. "1" : "ONE",
  839. "2" : 2,
  840. "10": 101
  841. }
  842. }`,
  843. wantMessage: &pb3.Maps{
  844. Uint64ToEnum: map[uint64]pb3.Enum{
  845. 1: pb3.Enum_ONE,
  846. 2: pb3.Enum_TWO,
  847. 10: 101,
  848. },
  849. },
  850. }, {
  851. desc: "map fields 3",
  852. inputMessage: &pb3.Maps{},
  853. inputText: `{
  854. "strToNested": {
  855. "nested_one": {
  856. "sString": "nested in a map"
  857. },
  858. "nested_two": {}
  859. }
  860. }`,
  861. wantMessage: &pb3.Maps{
  862. StrToNested: map[string]*pb3.Nested{
  863. "nested_one": {
  864. SString: "nested in a map",
  865. },
  866. "nested_two": {},
  867. },
  868. },
  869. }, {
  870. desc: "map fields 4",
  871. inputMessage: &pb3.Maps{},
  872. inputText: `{
  873. "strToOneofs": {
  874. "nested": {
  875. "oneofNested": {
  876. "sString": "nested oneof in map field value"
  877. }
  878. },
  879. "string": {
  880. "oneofString": "hello"
  881. }
  882. }
  883. }`,
  884. wantMessage: &pb3.Maps{
  885. StrToOneofs: map[string]*pb3.Oneofs{
  886. "string": {
  887. Union: &pb3.Oneofs_OneofString{
  888. OneofString: "hello",
  889. },
  890. },
  891. "nested": {
  892. Union: &pb3.Oneofs_OneofNested{
  893. OneofNested: &pb3.Nested{
  894. SString: "nested oneof in map field value",
  895. },
  896. },
  897. },
  898. },
  899. },
  900. }, {
  901. desc: "map contains duplicate keys",
  902. inputMessage: &pb3.Maps{},
  903. inputText: `{
  904. "int32ToStr": {
  905. "0": "cero",
  906. "0": "zero"
  907. }
  908. }
  909. `,
  910. wantErr: true,
  911. }, {
  912. desc: "map key empty string",
  913. inputMessage: &pb3.Maps{},
  914. inputText: `{
  915. "strToNested": {
  916. "": {}
  917. }
  918. }`,
  919. wantMessage: &pb3.Maps{
  920. StrToNested: map[string]*pb3.Nested{
  921. "": {},
  922. },
  923. },
  924. }, {
  925. desc: "map contains invalid key 1",
  926. inputMessage: &pb3.Maps{},
  927. inputText: `{
  928. "int32ToStr": {
  929. "invalid": "cero"
  930. }`,
  931. wantErr: true,
  932. }, {
  933. desc: "map contains invalid key 2",
  934. inputMessage: &pb3.Maps{},
  935. inputText: `{
  936. "int32ToStr": {
  937. "1.02": "float"
  938. }`,
  939. wantErr: true,
  940. }, {
  941. desc: "map contains invalid key 3",
  942. inputMessage: &pb3.Maps{},
  943. inputText: `{
  944. "int32ToStr": {
  945. "2147483648": "exceeds 32-bit integer max limit"
  946. }`,
  947. wantErr: true,
  948. }, {
  949. desc: "map contains invalid key 4",
  950. inputMessage: &pb3.Maps{},
  951. inputText: `{
  952. "uint64ToEnum": {
  953. "-1": 0
  954. }
  955. }`,
  956. wantErr: true,
  957. }, {
  958. desc: "map contains invalid value",
  959. inputMessage: &pb3.Maps{},
  960. inputText: `{
  961. "int32ToStr": {
  962. "101": true
  963. }`,
  964. wantErr: true,
  965. }, {
  966. desc: "map contains null for scalar value",
  967. inputMessage: &pb3.Maps{},
  968. inputText: `{
  969. "int32ToStr": {
  970. "101": null
  971. }`,
  972. wantErr: true,
  973. }, {
  974. desc: "map contains null for message value",
  975. inputMessage: &pb3.Maps{},
  976. inputText: `{
  977. "strToNested": {
  978. "hello": null
  979. }
  980. }`,
  981. wantErr: true,
  982. }, {
  983. desc: "map contains contains message value with invalid UTF8",
  984. inputMessage: &pb3.Maps{},
  985. inputText: `{
  986. "strToNested": {
  987. "hello": {
  988. "sString": "` + "abc\xff" + `"
  989. }
  990. }
  991. }`,
  992. wantErr: true,
  993. }, {
  994. desc: "map key contains invalid UTF8",
  995. inputMessage: &pb3.Maps{},
  996. inputText: `{
  997. "strToNested": {
  998. "` + "abc\xff" + `": {}
  999. }
  1000. }`,
  1001. wantErr: true,
  1002. }, {
  1003. desc: "required fields not set",
  1004. inputMessage: &pb2.Requireds{},
  1005. wantErr: true,
  1006. }, {
  1007. desc: "required field set",
  1008. inputMessage: &pb2.PartialRequired{},
  1009. inputText: `{
  1010. "reqString": "this is required"
  1011. }`,
  1012. wantMessage: &pb2.PartialRequired{
  1013. ReqString: proto.String("this is required"),
  1014. },
  1015. }, {
  1016. desc: "required fields partially set",
  1017. inputMessage: &pb2.Requireds{},
  1018. inputText: `{
  1019. "reqBool": false,
  1020. "reqSfixed64": 42,
  1021. "reqString": "hello",
  1022. "reqEnum": "ONE"
  1023. }`,
  1024. wantMessage: &pb2.Requireds{
  1025. ReqBool: proto.Bool(false),
  1026. ReqSfixed64: proto.Int64(42),
  1027. ReqString: proto.String("hello"),
  1028. ReqEnum: pb2.Enum_ONE.Enum(),
  1029. },
  1030. wantErr: true,
  1031. }, {
  1032. desc: "required fields partially set with AllowPartial",
  1033. umo: protojson.UnmarshalOptions{AllowPartial: true},
  1034. inputMessage: &pb2.Requireds{},
  1035. inputText: `{
  1036. "reqBool": false,
  1037. "reqSfixed64": 42,
  1038. "reqString": "hello",
  1039. "reqEnum": "ONE"
  1040. }`,
  1041. wantMessage: &pb2.Requireds{
  1042. ReqBool: proto.Bool(false),
  1043. ReqSfixed64: proto.Int64(42),
  1044. ReqString: proto.String("hello"),
  1045. ReqEnum: pb2.Enum_ONE.Enum(),
  1046. },
  1047. }, {
  1048. desc: "required fields all set",
  1049. inputMessage: &pb2.Requireds{},
  1050. inputText: `{
  1051. "reqBool": false,
  1052. "reqSfixed64": 42,
  1053. "reqDouble": 1.23,
  1054. "reqString": "hello",
  1055. "reqEnum": "ONE",
  1056. "reqNested": {}
  1057. }`,
  1058. wantMessage: &pb2.Requireds{
  1059. ReqBool: proto.Bool(false),
  1060. ReqSfixed64: proto.Int64(42),
  1061. ReqDouble: proto.Float64(1.23),
  1062. ReqString: proto.String("hello"),
  1063. ReqEnum: pb2.Enum_ONE.Enum(),
  1064. ReqNested: &pb2.Nested{},
  1065. },
  1066. }, {
  1067. desc: "indirect required field",
  1068. inputMessage: &pb2.IndirectRequired{},
  1069. inputText: `{
  1070. "optNested": {}
  1071. }`,
  1072. wantMessage: &pb2.IndirectRequired{
  1073. OptNested: &pb2.NestedWithRequired{},
  1074. },
  1075. wantErr: true,
  1076. }, {
  1077. desc: "indirect required field with AllowPartial",
  1078. umo: protojson.UnmarshalOptions{AllowPartial: true},
  1079. inputMessage: &pb2.IndirectRequired{},
  1080. inputText: `{
  1081. "optNested": {}
  1082. }`,
  1083. wantMessage: &pb2.IndirectRequired{
  1084. OptNested: &pb2.NestedWithRequired{},
  1085. },
  1086. }, {
  1087. desc: "indirect required field in repeated",
  1088. inputMessage: &pb2.IndirectRequired{},
  1089. inputText: `{
  1090. "rptNested": [
  1091. {"reqString": "one"},
  1092. {}
  1093. ]
  1094. }`,
  1095. wantMessage: &pb2.IndirectRequired{
  1096. RptNested: []*pb2.NestedWithRequired{
  1097. {
  1098. ReqString: proto.String("one"),
  1099. },
  1100. {},
  1101. },
  1102. },
  1103. wantErr: true,
  1104. }, {
  1105. desc: "indirect required field in repeated with AllowPartial",
  1106. umo: protojson.UnmarshalOptions{AllowPartial: true},
  1107. inputMessage: &pb2.IndirectRequired{},
  1108. inputText: `{
  1109. "rptNested": [
  1110. {"reqString": "one"},
  1111. {}
  1112. ]
  1113. }`,
  1114. wantMessage: &pb2.IndirectRequired{
  1115. RptNested: []*pb2.NestedWithRequired{
  1116. {
  1117. ReqString: proto.String("one"),
  1118. },
  1119. {},
  1120. },
  1121. },
  1122. }, {
  1123. desc: "indirect required field in map",
  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: proto.String("here"),
  1138. },
  1139. },
  1140. },
  1141. wantErr: true,
  1142. }, {
  1143. desc: "indirect required field in map with AllowPartial",
  1144. umo: protojson.UnmarshalOptions{AllowPartial: true},
  1145. inputMessage: &pb2.IndirectRequired{},
  1146. inputText: `{
  1147. "strToNested": {
  1148. "missing": {},
  1149. "contains": {
  1150. "reqString": "here"
  1151. }
  1152. }
  1153. }`,
  1154. wantMessage: &pb2.IndirectRequired{
  1155. StrToNested: map[string]*pb2.NestedWithRequired{
  1156. "missing": &pb2.NestedWithRequired{},
  1157. "contains": &pb2.NestedWithRequired{
  1158. ReqString: proto.String("here"),
  1159. },
  1160. },
  1161. },
  1162. }, {
  1163. desc: "indirect required field in oneof",
  1164. inputMessage: &pb2.IndirectRequired{},
  1165. inputText: `{
  1166. "oneofNested": {}
  1167. }`,
  1168. wantMessage: &pb2.IndirectRequired{
  1169. Union: &pb2.IndirectRequired_OneofNested{
  1170. OneofNested: &pb2.NestedWithRequired{},
  1171. },
  1172. },
  1173. wantErr: true,
  1174. }, {
  1175. desc: "indirect required field in oneof with AllowPartial",
  1176. umo: protojson.UnmarshalOptions{AllowPartial: true},
  1177. inputMessage: &pb2.IndirectRequired{},
  1178. inputText: `{
  1179. "oneofNested": {}
  1180. }`,
  1181. wantMessage: &pb2.IndirectRequired{
  1182. Union: &pb2.IndirectRequired_OneofNested{
  1183. OneofNested: &pb2.NestedWithRequired{},
  1184. },
  1185. },
  1186. }, {
  1187. desc: "extensions of non-repeated fields",
  1188. inputMessage: &pb2.Extensions{},
  1189. inputText: `{
  1190. "optString": "non-extension field",
  1191. "optBool": true,
  1192. "optInt32": 42,
  1193. "[pb2.opt_ext_bool]": true,
  1194. "[pb2.opt_ext_nested]": {
  1195. "optString": "nested in an extension",
  1196. "optNested": {
  1197. "optString": "another nested in an extension"
  1198. }
  1199. },
  1200. "[pb2.opt_ext_string]": "extension field",
  1201. "[pb2.opt_ext_enum]": "TEN"
  1202. }`,
  1203. wantMessage: func() proto.Message {
  1204. m := &pb2.Extensions{
  1205. OptString: proto.String("non-extension field"),
  1206. OptBool: proto.Bool(true),
  1207. OptInt32: proto.Int32(42),
  1208. }
  1209. proto.SetExtension(m, pb2.E_OptExtBool, true)
  1210. proto.SetExtension(m, pb2.E_OptExtString, "extension field")
  1211. proto.SetExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
  1212. proto.SetExtension(m, pb2.E_OptExtNested, &pb2.Nested{
  1213. OptString: proto.String("nested in an extension"),
  1214. OptNested: &pb2.Nested{
  1215. OptString: proto.String("another nested in an extension"),
  1216. },
  1217. })
  1218. return m
  1219. }(),
  1220. }, {
  1221. desc: "extensions of repeated fields",
  1222. inputMessage: &pb2.Extensions{},
  1223. inputText: `{
  1224. "[pb2.rpt_ext_enum]": ["TEN", 101, "ONE"],
  1225. "[pb2.rpt_ext_fixed32]": [42, 47],
  1226. "[pb2.rpt_ext_nested]": [
  1227. {"optString": "one"},
  1228. {"optString": "two"},
  1229. {"optString": "three"}
  1230. ]
  1231. }`,
  1232. wantMessage: func() proto.Message {
  1233. m := &pb2.Extensions{}
  1234. proto.SetExtension(m, pb2.E_RptExtEnum, []pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  1235. proto.SetExtension(m, pb2.E_RptExtFixed32, []uint32{42, 47})
  1236. proto.SetExtension(m, pb2.E_RptExtNested, []*pb2.Nested{
  1237. &pb2.Nested{OptString: proto.String("one")},
  1238. &pb2.Nested{OptString: proto.String("two")},
  1239. &pb2.Nested{OptString: proto.String("three")},
  1240. })
  1241. return m
  1242. }(),
  1243. }, {
  1244. desc: "extensions of non-repeated fields in another message",
  1245. inputMessage: &pb2.Extensions{},
  1246. inputText: `{
  1247. "[pb2.ExtensionsContainer.opt_ext_bool]": true,
  1248. "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN",
  1249. "[pb2.ExtensionsContainer.opt_ext_nested]": {
  1250. "optString": "nested in an extension",
  1251. "optNested": {
  1252. "optString": "another nested in an extension"
  1253. }
  1254. },
  1255. "[pb2.ExtensionsContainer.opt_ext_string]": "extension field"
  1256. }`,
  1257. wantMessage: func() proto.Message {
  1258. m := &pb2.Extensions{}
  1259. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
  1260. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
  1261. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
  1262. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
  1263. OptString: proto.String("nested in an extension"),
  1264. OptNested: &pb2.Nested{
  1265. OptString: proto.String("another nested in an extension"),
  1266. },
  1267. })
  1268. return m
  1269. }(),
  1270. }, {
  1271. desc: "extensions of repeated fields in another message",
  1272. inputMessage: &pb2.Extensions{},
  1273. inputText: `{
  1274. "optString": "non-extension field",
  1275. "optBool": true,
  1276. "optInt32": 42,
  1277. "[pb2.ExtensionsContainer.rpt_ext_nested]": [
  1278. {"optString": "one"},
  1279. {"optString": "two"},
  1280. {"optString": "three"}
  1281. ],
  1282. "[pb2.ExtensionsContainer.rpt_ext_enum]": ["TEN", 101, "ONE"],
  1283. "[pb2.ExtensionsContainer.rpt_ext_string]": ["hello", "world"]
  1284. }`,
  1285. wantMessage: func() proto.Message {
  1286. m := &pb2.Extensions{
  1287. OptString: proto.String("non-extension field"),
  1288. OptBool: proto.Bool(true),
  1289. OptInt32: proto.Int32(42),
  1290. }
  1291. proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, []pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  1292. proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtString, []string{"hello", "world"})
  1293. proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtNested, []*pb2.Nested{
  1294. &pb2.Nested{OptString: proto.String("one")},
  1295. &pb2.Nested{OptString: proto.String("two")},
  1296. &pb2.Nested{OptString: proto.String("three")},
  1297. })
  1298. return m
  1299. }(),
  1300. }, {
  1301. desc: "invalid extension field name",
  1302. inputMessage: &pb2.Extensions{},
  1303. inputText: `{ "[pb2.invalid_message_field]": true }`,
  1304. wantErr: true,
  1305. }, {
  1306. desc: "extensions of repeated field contains null",
  1307. inputMessage: &pb2.Extensions{},
  1308. inputText: `{
  1309. "[pb2.ExtensionsContainer.rpt_ext_nested]": [
  1310. {"optString": "one"},
  1311. null,
  1312. {"optString": "three"}
  1313. ],
  1314. }`,
  1315. wantErr: true,
  1316. }, {
  1317. desc: "MessageSet",
  1318. inputMessage: &pb2.MessageSet{},
  1319. inputText: `{
  1320. "[pb2.MessageSetExtension]": {
  1321. "optString": "a messageset extension"
  1322. },
  1323. "[pb2.MessageSetExtension.ext_nested]": {
  1324. "optString": "just a regular extension"
  1325. },
  1326. "[pb2.MessageSetExtension.not_message_set_extension]": {
  1327. "optString": "not a messageset extension"
  1328. }
  1329. }`,
  1330. wantMessage: func() proto.Message {
  1331. m := &pb2.MessageSet{}
  1332. proto.SetExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
  1333. OptString: proto.String("a messageset extension"),
  1334. })
  1335. proto.SetExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
  1336. OptString: proto.String("not a messageset extension"),
  1337. })
  1338. proto.SetExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
  1339. OptString: proto.String("just a regular extension"),
  1340. })
  1341. return m
  1342. }(),
  1343. skip: !flags.ProtoLegacy,
  1344. }, {
  1345. desc: "not real MessageSet 1",
  1346. inputMessage: &pb2.FakeMessageSet{},
  1347. inputText: `{
  1348. "[pb2.FakeMessageSetExtension.message_set_extension]": {
  1349. "optString": "not a messageset extension"
  1350. }
  1351. }`,
  1352. wantMessage: func() proto.Message {
  1353. m := &pb2.FakeMessageSet{}
  1354. proto.SetExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1355. OptString: proto.String("not a messageset extension"),
  1356. })
  1357. return m
  1358. }(),
  1359. skip: !flags.ProtoLegacy,
  1360. }, {
  1361. desc: "not real MessageSet 2",
  1362. inputMessage: &pb2.FakeMessageSet{},
  1363. inputText: `{
  1364. "[pb2.FakeMessageSetExtension]": {
  1365. "optString": "not a messageset extension"
  1366. }
  1367. }`,
  1368. wantErr: true,
  1369. skip: !flags.ProtoLegacy,
  1370. }, {
  1371. desc: "not real MessageSet 3",
  1372. inputMessage: &pb2.MessageSet{},
  1373. inputText: `{
  1374. "[pb2.message_set_extension]": {
  1375. "optString": "another not a messageset extension"
  1376. }
  1377. }`,
  1378. wantMessage: func() proto.Message {
  1379. m := &pb2.MessageSet{}
  1380. proto.SetExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1381. OptString: proto.String("another not a messageset extension"),
  1382. })
  1383. return m
  1384. }(),
  1385. skip: !flags.ProtoLegacy,
  1386. }, {
  1387. desc: "Empty",
  1388. inputMessage: &emptypb.Empty{},
  1389. inputText: `{}`,
  1390. wantMessage: &emptypb.Empty{},
  1391. }, {
  1392. desc: "Empty contains unknown",
  1393. inputMessage: &emptypb.Empty{},
  1394. inputText: `{"unknown": null}`,
  1395. wantErr: true,
  1396. }, {
  1397. desc: "BoolValue false",
  1398. inputMessage: &wrapperspb.BoolValue{},
  1399. inputText: `false`,
  1400. wantMessage: &wrapperspb.BoolValue{},
  1401. }, {
  1402. desc: "BoolValue true",
  1403. inputMessage: &wrapperspb.BoolValue{},
  1404. inputText: `true`,
  1405. wantMessage: &wrapperspb.BoolValue{Value: true},
  1406. }, {
  1407. desc: "BoolValue invalid value",
  1408. inputMessage: &wrapperspb.BoolValue{},
  1409. inputText: `{}`,
  1410. wantErr: true,
  1411. }, {
  1412. desc: "Int32Value",
  1413. inputMessage: &wrapperspb.Int32Value{},
  1414. inputText: `42`,
  1415. wantMessage: &wrapperspb.Int32Value{Value: 42},
  1416. }, {
  1417. desc: "Int32Value in JSON string",
  1418. inputMessage: &wrapperspb.Int32Value{},
  1419. inputText: `"1.23e3"`,
  1420. wantMessage: &wrapperspb.Int32Value{Value: 1230},
  1421. }, {
  1422. desc: "Int64Value",
  1423. inputMessage: &wrapperspb.Int64Value{},
  1424. inputText: `"42"`,
  1425. wantMessage: &wrapperspb.Int64Value{Value: 42},
  1426. }, {
  1427. desc: "UInt32Value",
  1428. inputMessage: &wrapperspb.UInt32Value{},
  1429. inputText: `42`,
  1430. wantMessage: &wrapperspb.UInt32Value{Value: 42},
  1431. }, {
  1432. desc: "UInt64Value",
  1433. inputMessage: &wrapperspb.UInt64Value{},
  1434. inputText: `"42"`,
  1435. wantMessage: &wrapperspb.UInt64Value{Value: 42},
  1436. }, {
  1437. desc: "FloatValue",
  1438. inputMessage: &wrapperspb.FloatValue{},
  1439. inputText: `1.02`,
  1440. wantMessage: &wrapperspb.FloatValue{Value: 1.02},
  1441. }, {
  1442. desc: "FloatValue exceeds max limit",
  1443. inputMessage: &wrapperspb.FloatValue{},
  1444. inputText: `1.23+40`,
  1445. wantErr: true,
  1446. }, {
  1447. desc: "FloatValue Infinity",
  1448. inputMessage: &wrapperspb.FloatValue{},
  1449. inputText: `"-Infinity"`,
  1450. wantMessage: &wrapperspb.FloatValue{Value: float32(math.Inf(-1))},
  1451. }, {
  1452. desc: "DoubleValue",
  1453. inputMessage: &wrapperspb.DoubleValue{},
  1454. inputText: `1.02`,
  1455. wantMessage: &wrapperspb.DoubleValue{Value: 1.02},
  1456. }, {
  1457. desc: "DoubleValue Infinity",
  1458. inputMessage: &wrapperspb.DoubleValue{},
  1459. inputText: `"Infinity"`,
  1460. wantMessage: &wrapperspb.DoubleValue{Value: math.Inf(+1)},
  1461. }, {
  1462. desc: "StringValue empty",
  1463. inputMessage: &wrapperspb.StringValue{},
  1464. inputText: `""`,
  1465. wantMessage: &wrapperspb.StringValue{},
  1466. }, {
  1467. desc: "StringValue",
  1468. inputMessage: &wrapperspb.StringValue{},
  1469. inputText: `"谷歌"`,
  1470. wantMessage: &wrapperspb.StringValue{Value: "谷歌"},
  1471. }, {
  1472. desc: "StringValue with invalid UTF8 error",
  1473. inputMessage: &wrapperspb.StringValue{},
  1474. inputText: "\"abc\xff\"",
  1475. wantErr: true,
  1476. }, {
  1477. desc: "StringValue field with invalid UTF8 error",
  1478. inputMessage: &pb2.KnownTypes{},
  1479. inputText: "{\n \"optString\": \"abc\xff\"\n}",
  1480. wantErr: true,
  1481. }, {
  1482. desc: "NullValue field with JSON null",
  1483. inputMessage: &pb2.KnownTypes{},
  1484. inputText: `{
  1485. "optNull": null
  1486. }`,
  1487. wantMessage: &pb2.KnownTypes{OptNull: new(structpb.NullValue)},
  1488. }, {
  1489. desc: "NullValue field with string",
  1490. inputMessage: &pb2.KnownTypes{},
  1491. inputText: `{
  1492. "optNull": "NULL_VALUE"
  1493. }`,
  1494. wantMessage: &pb2.KnownTypes{OptNull: new(structpb.NullValue)},
  1495. }, {
  1496. desc: "BytesValue",
  1497. inputMessage: &wrapperspb.BytesValue{},
  1498. inputText: `"aGVsbG8="`,
  1499. wantMessage: &wrapperspb.BytesValue{Value: []byte("hello")},
  1500. }, {
  1501. desc: "Value null",
  1502. inputMessage: &structpb.Value{},
  1503. inputText: `null`,
  1504. wantMessage: &structpb.Value{Kind: &structpb.Value_NullValue{}},
  1505. }, {
  1506. desc: "Value field null",
  1507. inputMessage: &pb2.KnownTypes{},
  1508. inputText: `{
  1509. "optValue": null
  1510. }`,
  1511. wantMessage: &pb2.KnownTypes{
  1512. OptValue: &structpb.Value{Kind: &structpb.Value_NullValue{}},
  1513. },
  1514. }, {
  1515. desc: "Value bool",
  1516. inputMessage: &structpb.Value{},
  1517. inputText: `false`,
  1518. wantMessage: &structpb.Value{Kind: &structpb.Value_BoolValue{}},
  1519. }, {
  1520. desc: "Value field bool",
  1521. inputMessage: &pb2.KnownTypes{},
  1522. inputText: `{
  1523. "optValue": true
  1524. }`,
  1525. wantMessage: &pb2.KnownTypes{
  1526. OptValue: &structpb.Value{Kind: &structpb.Value_BoolValue{true}},
  1527. },
  1528. }, {
  1529. desc: "Value number",
  1530. inputMessage: &structpb.Value{},
  1531. inputText: `1.02`,
  1532. wantMessage: &structpb.Value{Kind: &structpb.Value_NumberValue{1.02}},
  1533. }, {
  1534. desc: "Value field number",
  1535. inputMessage: &pb2.KnownTypes{},
  1536. inputText: `{
  1537. "optValue": 1.02
  1538. }`,
  1539. wantMessage: &pb2.KnownTypes{
  1540. OptValue: &structpb.Value{Kind: &structpb.Value_NumberValue{1.02}},
  1541. },
  1542. }, {
  1543. desc: "Value string",
  1544. inputMessage: &structpb.Value{},
  1545. inputText: `"hello"`,
  1546. wantMessage: &structpb.Value{Kind: &structpb.Value_StringValue{"hello"}},
  1547. }, {
  1548. desc: "Value string with invalid UTF8",
  1549. inputMessage: &structpb.Value{},
  1550. inputText: "\"\xff\"",
  1551. wantErr: true,
  1552. }, {
  1553. desc: "Value field string",
  1554. inputMessage: &pb2.KnownTypes{},
  1555. inputText: `{
  1556. "optValue": "NaN"
  1557. }`,
  1558. wantMessage: &pb2.KnownTypes{
  1559. OptValue: &structpb.Value{Kind: &structpb.Value_StringValue{"NaN"}},
  1560. },
  1561. }, {
  1562. desc: "Value field string with invalid UTF8",
  1563. inputMessage: &pb2.KnownTypes{},
  1564. inputText: `{
  1565. "optValue": "` + "\xff" + `"
  1566. }`,
  1567. wantErr: true,
  1568. }, {
  1569. desc: "Value empty struct",
  1570. inputMessage: &structpb.Value{},
  1571. inputText: `{}`,
  1572. wantMessage: &structpb.Value{
  1573. Kind: &structpb.Value_StructValue{
  1574. &structpb.Struct{Fields: map[string]*structpb.Value{}},
  1575. },
  1576. },
  1577. }, {
  1578. desc: "Value struct",
  1579. inputMessage: &structpb.Value{},
  1580. inputText: `{
  1581. "string": "hello",
  1582. "number": 123,
  1583. "null": null,
  1584. "bool": false,
  1585. "struct": {
  1586. "string": "world"
  1587. },
  1588. "list": []
  1589. }`,
  1590. wantMessage: &structpb.Value{
  1591. Kind: &structpb.Value_StructValue{
  1592. &structpb.Struct{
  1593. Fields: map[string]*structpb.Value{
  1594. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  1595. "number": {Kind: &structpb.Value_NumberValue{123}},
  1596. "null": {Kind: &structpb.Value_NullValue{}},
  1597. "bool": {Kind: &structpb.Value_BoolValue{false}},
  1598. "struct": {
  1599. Kind: &structpb.Value_StructValue{
  1600. &structpb.Struct{
  1601. Fields: map[string]*structpb.Value{
  1602. "string": {Kind: &structpb.Value_StringValue{"world"}},
  1603. },
  1604. },
  1605. },
  1606. },
  1607. "list": {
  1608. Kind: &structpb.Value_ListValue{&structpb.ListValue{}},
  1609. },
  1610. },
  1611. },
  1612. },
  1613. },
  1614. }, {
  1615. desc: "Value struct with invalid UTF8 string",
  1616. inputMessage: &structpb.Value{},
  1617. inputText: "{\"string\": \"abc\xff\"}",
  1618. wantErr: true,
  1619. }, {
  1620. desc: "Value field struct",
  1621. inputMessage: &pb2.KnownTypes{},
  1622. inputText: `{
  1623. "optValue": {
  1624. "string": "hello"
  1625. }
  1626. }`,
  1627. wantMessage: &pb2.KnownTypes{
  1628. OptValue: &structpb.Value{
  1629. Kind: &structpb.Value_StructValue{
  1630. &structpb.Struct{
  1631. Fields: map[string]*structpb.Value{
  1632. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  1633. },
  1634. },
  1635. },
  1636. },
  1637. },
  1638. }, {
  1639. desc: "Value empty list",
  1640. inputMessage: &structpb.Value{},
  1641. inputText: `[]`,
  1642. wantMessage: &structpb.Value{
  1643. Kind: &structpb.Value_ListValue{
  1644. &structpb.ListValue{Values: []*structpb.Value{}},
  1645. },
  1646. },
  1647. }, {
  1648. desc: "Value list",
  1649. inputMessage: &structpb.Value{},
  1650. inputText: `[
  1651. "string",
  1652. 123,
  1653. null,
  1654. true,
  1655. {},
  1656. [
  1657. "string",
  1658. 1.23,
  1659. null,
  1660. false
  1661. ]
  1662. ]`,
  1663. wantMessage: &structpb.Value{
  1664. Kind: &structpb.Value_ListValue{
  1665. &structpb.ListValue{
  1666. Values: []*structpb.Value{
  1667. {Kind: &structpb.Value_StringValue{"string"}},
  1668. {Kind: &structpb.Value_NumberValue{123}},
  1669. {Kind: &structpb.Value_NullValue{}},
  1670. {Kind: &structpb.Value_BoolValue{true}},
  1671. {Kind: &structpb.Value_StructValue{&structpb.Struct{}}},
  1672. {
  1673. Kind: &structpb.Value_ListValue{
  1674. &structpb.ListValue{
  1675. Values: []*structpb.Value{
  1676. {Kind: &structpb.Value_StringValue{"string"}},
  1677. {Kind: &structpb.Value_NumberValue{1.23}},
  1678. {Kind: &structpb.Value_NullValue{}},
  1679. {Kind: &structpb.Value_BoolValue{false}},
  1680. },
  1681. },
  1682. },
  1683. },
  1684. },
  1685. },
  1686. },
  1687. },
  1688. }, {
  1689. desc: "Value list with invalid UTF8 string",
  1690. inputMessage: &structpb.Value{},
  1691. inputText: "[\"abc\xff\"]",
  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. wantErr: true,
  1700. }, {
  1701. desc: "Duration empty string",
  1702. inputMessage: &durationpb.Duration{},
  1703. inputText: `""`,
  1704. wantErr: true,
  1705. }, {
  1706. desc: "Duration with secs",
  1707. inputMessage: &durationpb.Duration{},
  1708. inputText: `"3s"`,
  1709. wantMessage: &durationpb.Duration{Seconds: 3},
  1710. }, {
  1711. desc: "Duration with escaped unicode",
  1712. inputMessage: &durationpb.Duration{},
  1713. inputText: `"\u0033s"`,
  1714. wantMessage: &durationpb.Duration{Seconds: 3},
  1715. }, {
  1716. desc: "Duration with -secs",
  1717. inputMessage: &durationpb.Duration{},
  1718. inputText: `"-3s"`,
  1719. wantMessage: &durationpb.Duration{Seconds: -3},
  1720. }, {
  1721. desc: "Duration with plus sign",
  1722. inputMessage: &durationpb.Duration{},
  1723. inputText: `"+3s"`,
  1724. wantMessage: &durationpb.Duration{Seconds: 3},
  1725. }, {
  1726. desc: "Duration with nanos",
  1727. inputMessage: &durationpb.Duration{},
  1728. inputText: `"0.001s"`,
  1729. wantMessage: &durationpb.Duration{Nanos: 1e6},
  1730. }, {
  1731. desc: "Duration with -nanos",
  1732. inputMessage: &durationpb.Duration{},
  1733. inputText: `"-0.001s"`,
  1734. wantMessage: &durationpb.Duration{Nanos: -1e6},
  1735. }, {
  1736. desc: "Duration with -nanos",
  1737. inputMessage: &durationpb.Duration{},
  1738. inputText: `"-.001s"`,
  1739. wantMessage: &durationpb.Duration{Nanos: -1e6},
  1740. }, {
  1741. desc: "Duration with +nanos",
  1742. inputMessage: &durationpb.Duration{},
  1743. inputText: `"+.001s"`,
  1744. wantMessage: &durationpb.Duration{Nanos: 1e6},
  1745. }, {
  1746. desc: "Duration with -secs -nanos",
  1747. inputMessage: &durationpb.Duration{},
  1748. inputText: `"-123.000000450s"`,
  1749. wantMessage: &durationpb.Duration{Seconds: -123, Nanos: -450},
  1750. }, {
  1751. desc: "Duration with large secs",
  1752. inputMessage: &durationpb.Duration{},
  1753. inputText: `"10000000000.000000001s"`,
  1754. wantMessage: &durationpb.Duration{Seconds: 1e10, Nanos: 1},
  1755. }, {
  1756. desc: "Duration with decimal without fractional",
  1757. inputMessage: &durationpb.Duration{},
  1758. inputText: `"3.s"`,
  1759. wantMessage: &durationpb.Duration{Seconds: 3},
  1760. }, {
  1761. desc: "Duration with decimal without integer",
  1762. inputMessage: &durationpb.Duration{},
  1763. inputText: `"0.5s"`,
  1764. wantMessage: &durationpb.Duration{Nanos: 5e8},
  1765. }, {
  1766. desc: "Duration max value",
  1767. inputMessage: &durationpb.Duration{},
  1768. inputText: `"315576000000.999999999s"`,
  1769. wantMessage: &durationpb.Duration{Seconds: 315576000000, Nanos: 999999999},
  1770. }, {
  1771. desc: "Duration min value",
  1772. inputMessage: &durationpb.Duration{},
  1773. inputText: `"-315576000000.999999999s"`,
  1774. wantMessage: &durationpb.Duration{Seconds: -315576000000, Nanos: -999999999},
  1775. }, {
  1776. desc: "Duration with +secs out of range",
  1777. inputMessage: &durationpb.Duration{},
  1778. inputText: `"315576000001s"`,
  1779. wantErr: true,
  1780. }, {
  1781. desc: "Duration with -secs out of range",
  1782. inputMessage: &durationpb.Duration{},
  1783. inputText: `"-315576000001s"`,
  1784. wantErr: true,
  1785. }, {
  1786. desc: "Duration with nanos beyond 9 digits",
  1787. inputMessage: &durationpb.Duration{},
  1788. inputText: `"0.1000000000s"`,
  1789. wantErr: true,
  1790. }, {
  1791. desc: "Duration without suffix s",
  1792. inputMessage: &durationpb.Duration{},
  1793. inputText: `"123"`,
  1794. wantErr: true,
  1795. }, {
  1796. desc: "Duration invalid signed fraction",
  1797. inputMessage: &durationpb.Duration{},
  1798. inputText: `"123.+123s"`,
  1799. wantErr: true,
  1800. }, {
  1801. desc: "Duration invalid multiple .",
  1802. inputMessage: &durationpb.Duration{},
  1803. inputText: `"123.123.s"`,
  1804. wantErr: true,
  1805. }, {
  1806. desc: "Duration invalid integer",
  1807. inputMessage: &durationpb.Duration{},
  1808. inputText: `"01s"`,
  1809. wantErr: true,
  1810. }, {
  1811. desc: "Timestamp zero",
  1812. inputMessage: &timestamppb.Timestamp{},
  1813. inputText: `"1970-01-01T00:00:00Z"`,
  1814. wantMessage: &timestamppb.Timestamp{},
  1815. }, {
  1816. desc: "Timestamp with tz adjustment",
  1817. inputMessage: &timestamppb.Timestamp{},
  1818. inputText: `"1970-01-01T00:00:00+01:00"`,
  1819. wantMessage: &timestamppb.Timestamp{Seconds: -3600},
  1820. }, {
  1821. desc: "Timestamp UTC",
  1822. inputMessage: &timestamppb.Timestamp{},
  1823. inputText: `"2019-03-19T23:03:21Z"`,
  1824. wantMessage: &timestamppb.Timestamp{Seconds: 1553036601},
  1825. }, {
  1826. desc: "Timestamp with escaped unicode",
  1827. inputMessage: &timestamppb.Timestamp{},
  1828. inputText: `"2019-0\u0033-19T23:03:21Z"`,
  1829. wantMessage: &timestamppb.Timestamp{Seconds: 1553036601},
  1830. }, {
  1831. desc: "Timestamp with nanos",
  1832. inputMessage: &timestamppb.Timestamp{},
  1833. inputText: `"2019-03-19T23:03:21.000000001Z"`,
  1834. wantMessage: &timestamppb.Timestamp{Seconds: 1553036601, Nanos: 1},
  1835. }, {
  1836. desc: "Timestamp max value",
  1837. inputMessage: &timestamppb.Timestamp{},
  1838. inputText: `"9999-12-31T23:59:59.999999999Z"`,
  1839. wantMessage: &timestamppb.Timestamp{Seconds: 253402300799, Nanos: 999999999},
  1840. }, {
  1841. desc: "Timestamp above max value",
  1842. inputMessage: &timestamppb.Timestamp{},
  1843. inputText: `"9999-12-31T23:59:59-01:00"`,
  1844. wantErr: true,
  1845. }, {
  1846. desc: "Timestamp min value",
  1847. inputMessage: &timestamppb.Timestamp{},
  1848. inputText: `"0001-01-01T00:00:00Z"`,
  1849. wantMessage: &timestamppb.Timestamp{Seconds: -62135596800},
  1850. }, {
  1851. desc: "Timestamp below min value",
  1852. inputMessage: &timestamppb.Timestamp{},
  1853. inputText: `"0001-01-01T00:00:00+01:00"`,
  1854. wantErr: true,
  1855. }, {
  1856. desc: "Timestamp with nanos beyond 9 digits",
  1857. inputMessage: &timestamppb.Timestamp{},
  1858. inputText: `"1970-01-01T00:00:00.0000000001Z"`,
  1859. wantErr: true,
  1860. }, {
  1861. desc: "FieldMask empty",
  1862. inputMessage: &fieldmaskpb.FieldMask{},
  1863. inputText: `""`,
  1864. wantMessage: &fieldmaskpb.FieldMask{Paths: []string{}},
  1865. }, {
  1866. desc: "FieldMask",
  1867. inputMessage: &fieldmaskpb.FieldMask{},
  1868. inputText: `"foo,fooBar , foo.barQux ,Foo"`,
  1869. wantMessage: &fieldmaskpb.FieldMask{
  1870. Paths: []string{
  1871. "foo",
  1872. "foo_bar",
  1873. "foo.bar_qux",
  1874. "_foo",
  1875. },
  1876. },
  1877. }, {
  1878. desc: "FieldMask field",
  1879. inputMessage: &pb2.KnownTypes{},
  1880. inputText: `{
  1881. "optFieldmask": "foo, qux.fooBar"
  1882. }`,
  1883. wantMessage: &pb2.KnownTypes{
  1884. OptFieldmask: &fieldmaskpb.FieldMask{
  1885. Paths: []string{
  1886. "foo",
  1887. "qux.foo_bar",
  1888. },
  1889. },
  1890. },
  1891. }, {
  1892. desc: "Any empty",
  1893. inputMessage: &anypb.Any{},
  1894. inputText: `{}`,
  1895. wantMessage: &anypb.Any{},
  1896. }, {
  1897. desc: "Any with non-custom message",
  1898. umo: protojson.UnmarshalOptions{
  1899. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1900. },
  1901. inputMessage: &anypb.Any{},
  1902. inputText: `{
  1903. "@type": "foo/pb2.Nested",
  1904. "optString": "embedded inside Any",
  1905. "optNested": {
  1906. "optString": "inception"
  1907. }
  1908. }`,
  1909. wantMessage: func() proto.Message {
  1910. m := &pb2.Nested{
  1911. OptString: proto.String("embedded inside Any"),
  1912. OptNested: &pb2.Nested{
  1913. OptString: proto.String("inception"),
  1914. },
  1915. }
  1916. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1917. if err != nil {
  1918. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1919. }
  1920. return &anypb.Any{
  1921. TypeUrl: "foo/pb2.Nested",
  1922. Value: b,
  1923. }
  1924. }(),
  1925. }, {
  1926. desc: "Any with empty embedded message",
  1927. umo: protojson.UnmarshalOptions{
  1928. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1929. },
  1930. inputMessage: &anypb.Any{},
  1931. inputText: `{"@type": "foo/pb2.Nested"}`,
  1932. wantMessage: &anypb.Any{TypeUrl: "foo/pb2.Nested"},
  1933. }, {
  1934. desc: "Any without registered type",
  1935. umo: protojson.UnmarshalOptions{Resolver: preg.NewTypes()},
  1936. inputMessage: &anypb.Any{},
  1937. inputText: `{"@type": "foo/pb2.Nested"}`,
  1938. wantErr: true,
  1939. }, {
  1940. desc: "Any with missing required",
  1941. umo: protojson.UnmarshalOptions{
  1942. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1943. },
  1944. inputMessage: &anypb.Any{},
  1945. inputText: `{
  1946. "@type": "pb2.PartialRequired",
  1947. "optString": "embedded inside Any"
  1948. }`,
  1949. wantMessage: func() proto.Message {
  1950. m := &pb2.PartialRequired{
  1951. OptString: proto.String("embedded inside Any"),
  1952. }
  1953. b, err := proto.MarshalOptions{
  1954. Deterministic: true,
  1955. AllowPartial: true,
  1956. }.Marshal(m)
  1957. if err != nil {
  1958. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1959. }
  1960. return &anypb.Any{
  1961. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1962. Value: b,
  1963. }
  1964. }(),
  1965. }, {
  1966. desc: "Any with partial required and AllowPartial",
  1967. umo: protojson.UnmarshalOptions{
  1968. AllowPartial: true,
  1969. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1970. },
  1971. inputMessage: &anypb.Any{},
  1972. inputText: `{
  1973. "@type": "pb2.PartialRequired",
  1974. "optString": "embedded inside Any"
  1975. }`,
  1976. wantMessage: func() proto.Message {
  1977. m := &pb2.PartialRequired{
  1978. OptString: proto.String("embedded inside Any"),
  1979. }
  1980. b, err := proto.MarshalOptions{
  1981. Deterministic: true,
  1982. AllowPartial: true,
  1983. }.Marshal(m)
  1984. if err != nil {
  1985. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1986. }
  1987. return &anypb.Any{
  1988. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1989. Value: b,
  1990. }
  1991. }(),
  1992. }, {
  1993. desc: "Any with invalid UTF8",
  1994. umo: protojson.UnmarshalOptions{
  1995. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1996. },
  1997. inputMessage: &anypb.Any{},
  1998. inputText: `{
  1999. "optString": "` + "abc\xff" + `",
  2000. "@type": "foo/pb2.Nested"
  2001. }`,
  2002. wantErr: true,
  2003. }, {
  2004. desc: "Any with BoolValue",
  2005. umo: protojson.UnmarshalOptions{
  2006. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{})),
  2007. },
  2008. inputMessage: &anypb.Any{},
  2009. inputText: `{
  2010. "@type": "type.googleapis.com/google.protobuf.BoolValue",
  2011. "value": true
  2012. }`,
  2013. wantMessage: func() proto.Message {
  2014. m := &wrapperspb.BoolValue{Value: true}
  2015. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  2016. if err != nil {
  2017. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  2018. }
  2019. return &anypb.Any{
  2020. TypeUrl: "type.googleapis.com/google.protobuf.BoolValue",
  2021. Value: b,
  2022. }
  2023. }(),
  2024. }, {
  2025. desc: "Any with Empty",
  2026. umo: protojson.UnmarshalOptions{
  2027. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
  2028. },
  2029. inputMessage: &anypb.Any{},
  2030. inputText: `{
  2031. "value": {},
  2032. "@type": "type.googleapis.com/google.protobuf.Empty"
  2033. }`,
  2034. wantMessage: &anypb.Any{
  2035. TypeUrl: "type.googleapis.com/google.protobuf.Empty",
  2036. },
  2037. }, {
  2038. desc: "Any with missing Empty",
  2039. umo: protojson.UnmarshalOptions{
  2040. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
  2041. },
  2042. inputMessage: &anypb.Any{},
  2043. inputText: `{
  2044. "@type": "type.googleapis.com/google.protobuf.Empty"
  2045. }`,
  2046. wantErr: true,
  2047. }, {
  2048. desc: "Any with StringValue containing invalid UTF8",
  2049. umo: protojson.UnmarshalOptions{
  2050. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{})),
  2051. },
  2052. inputMessage: &anypb.Any{},
  2053. inputText: `{
  2054. "@type": "google.protobuf.StringValue",
  2055. "value": "` + "abc\xff" + `"
  2056. }`,
  2057. wantErr: true,
  2058. }, {
  2059. desc: "Any with Int64Value",
  2060. umo: protojson.UnmarshalOptions{
  2061. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.Int64Value{})),
  2062. },
  2063. inputMessage: &anypb.Any{},
  2064. inputText: `{
  2065. "@type": "google.protobuf.Int64Value",
  2066. "value": "42"
  2067. }`,
  2068. wantMessage: func() proto.Message {
  2069. m := &wrapperspb.Int64Value{Value: 42}
  2070. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  2071. if err != nil {
  2072. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  2073. }
  2074. return &anypb.Any{
  2075. TypeUrl: "google.protobuf.Int64Value",
  2076. Value: b,
  2077. }
  2078. }(),
  2079. }, {
  2080. desc: "Any with invalid Int64Value",
  2081. umo: protojson.UnmarshalOptions{
  2082. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.Int64Value{})),
  2083. },
  2084. inputMessage: &anypb.Any{},
  2085. inputText: `{
  2086. "@type": "google.protobuf.Int64Value",
  2087. "value": "forty-two"
  2088. }`,
  2089. wantErr: true,
  2090. }, {
  2091. desc: "Any with invalid UInt64Value",
  2092. umo: protojson.UnmarshalOptions{
  2093. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.UInt64Value{})),
  2094. },
  2095. inputMessage: &anypb.Any{},
  2096. inputText: `{
  2097. "@type": "google.protobuf.UInt64Value",
  2098. "value": -42
  2099. }`,
  2100. wantErr: true,
  2101. }, {
  2102. desc: "Any with Duration",
  2103. umo: protojson.UnmarshalOptions{
  2104. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&durationpb.Duration{})),
  2105. },
  2106. inputMessage: &anypb.Any{},
  2107. inputText: `{
  2108. "@type": "type.googleapis.com/google.protobuf.Duration",
  2109. "value": "0s"
  2110. }`,
  2111. wantMessage: func() proto.Message {
  2112. m := &durationpb.Duration{}
  2113. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  2114. if err != nil {
  2115. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  2116. }
  2117. return &anypb.Any{
  2118. TypeUrl: "type.googleapis.com/google.protobuf.Duration",
  2119. Value: b,
  2120. }
  2121. }(),
  2122. }, {
  2123. desc: "Any with Value of StringValue",
  2124. umo: protojson.UnmarshalOptions{
  2125. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  2126. },
  2127. inputMessage: &anypb.Any{},
  2128. inputText: `{
  2129. "@type": "google.protobuf.Value",
  2130. "value": "` + "abc\xff" + `"
  2131. }`,
  2132. wantErr: true,
  2133. }, {
  2134. desc: "Any with Value of NullValue",
  2135. umo: protojson.UnmarshalOptions{
  2136. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&structpb.Value{})),
  2137. },
  2138. inputMessage: &anypb.Any{},
  2139. inputText: `{
  2140. "@type": "google.protobuf.Value",
  2141. "value": null
  2142. }`,
  2143. wantMessage: func() proto.Message {
  2144. m := &structpb.Value{Kind: &structpb.Value_NullValue{}}
  2145. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  2146. if err != nil {
  2147. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  2148. }
  2149. return &anypb.Any{
  2150. TypeUrl: "google.protobuf.Value",
  2151. Value: b,
  2152. }
  2153. }(),
  2154. }, {
  2155. desc: "Any with Struct",
  2156. umo: protojson.UnmarshalOptions{
  2157. Resolver: preg.NewTypes(
  2158. pimpl.Export{}.MessageTypeOf(&structpb.Struct{}),
  2159. pimpl.Export{}.MessageTypeOf(&structpb.Value{}),
  2160. pimpl.Export{}.MessageTypeOf(&wrapperspb.BoolValue{}),
  2161. pimpl.Export{}.EnumTypeOf(structpb.NullValue_NULL_VALUE),
  2162. pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{}),
  2163. ),
  2164. },
  2165. inputMessage: &anypb.Any{},
  2166. inputText: `{
  2167. "@type": "google.protobuf.Struct",
  2168. "value": {
  2169. "bool": true,
  2170. "null": null,
  2171. "string": "hello",
  2172. "struct": {
  2173. "string": "world"
  2174. }
  2175. }
  2176. }`,
  2177. wantMessage: func() proto.Message {
  2178. m := &structpb.Struct{
  2179. Fields: map[string]*structpb.Value{
  2180. "bool": {Kind: &structpb.Value_BoolValue{true}},
  2181. "null": {Kind: &structpb.Value_NullValue{}},
  2182. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  2183. "struct": {
  2184. Kind: &structpb.Value_StructValue{
  2185. &structpb.Struct{
  2186. Fields: map[string]*structpb.Value{
  2187. "string": {Kind: &structpb.Value_StringValue{"world"}},
  2188. },
  2189. },
  2190. },
  2191. },
  2192. },
  2193. }
  2194. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  2195. if err != nil {
  2196. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  2197. }
  2198. return &anypb.Any{
  2199. TypeUrl: "google.protobuf.Struct",
  2200. Value: b,
  2201. }
  2202. }(),
  2203. }, {
  2204. desc: "Any with missing @type",
  2205. umo: protojson.UnmarshalOptions{},
  2206. inputMessage: &anypb.Any{},
  2207. inputText: `{
  2208. "value": {}
  2209. }`,
  2210. wantErr: true,
  2211. }, {
  2212. desc: "Any with empty @type",
  2213. inputMessage: &anypb.Any{},
  2214. inputText: `{
  2215. "@type": ""
  2216. }`,
  2217. wantErr: true,
  2218. }, {
  2219. desc: "Any with duplicate @type",
  2220. umo: protojson.UnmarshalOptions{
  2221. Resolver: preg.NewTypes(
  2222. pimpl.Export{}.MessageTypeOf(&pb2.Nested{}),
  2223. pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{}),
  2224. ),
  2225. },
  2226. inputMessage: &anypb.Any{},
  2227. inputText: `{
  2228. "@type": "google.protobuf.StringValue",
  2229. "value": "hello",
  2230. "@type": "pb2.Nested"
  2231. }`,
  2232. wantErr: true,
  2233. }, {
  2234. desc: "Any with duplicate value",
  2235. umo: protojson.UnmarshalOptions{
  2236. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{})),
  2237. },
  2238. inputMessage: &anypb.Any{},
  2239. inputText: `{
  2240. "@type": "google.protobuf.StringValue",
  2241. "value": "hello",
  2242. "value": "world"
  2243. }`,
  2244. wantErr: true,
  2245. }, {
  2246. desc: "Any with unknown field",
  2247. umo: protojson.UnmarshalOptions{
  2248. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  2249. },
  2250. inputMessage: &anypb.Any{},
  2251. inputText: `{
  2252. "@type": "pb2.Nested",
  2253. "optString": "hello",
  2254. "unknown": "world"
  2255. }`,
  2256. wantErr: true,
  2257. }, {
  2258. desc: "Any with embedded type containing Any",
  2259. umo: protojson.UnmarshalOptions{
  2260. Resolver: preg.NewTypes(
  2261. pimpl.Export{}.MessageTypeOf(&pb2.KnownTypes{}),
  2262. pimpl.Export{}.MessageTypeOf(&anypb.Any{}),
  2263. pimpl.Export{}.MessageTypeOf(&wrapperspb.StringValue{}),
  2264. ),
  2265. },
  2266. inputMessage: &anypb.Any{},
  2267. inputText: `{
  2268. "@type": "pb2.KnownTypes",
  2269. "optAny": {
  2270. "@type": "google.protobuf.StringValue",
  2271. "value": "` + "abc\xff" + `"
  2272. }
  2273. }`,
  2274. wantErr: true,
  2275. }, {
  2276. desc: "well known types as field values",
  2277. umo: protojson.UnmarshalOptions{
  2278. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
  2279. },
  2280. inputMessage: &pb2.KnownTypes{},
  2281. inputText: `{
  2282. "optBool": false,
  2283. "optInt32": 42,
  2284. "optInt64": "42",
  2285. "optUint32": 42,
  2286. "optUint64": "42",
  2287. "optFloat": 1.23,
  2288. "optDouble": 3.1415,
  2289. "optString": "hello",
  2290. "optBytes": "aGVsbG8=",
  2291. "optDuration": "123s",
  2292. "optTimestamp": "2019-03-19T23:03:21Z",
  2293. "optStruct": {
  2294. "string": "hello"
  2295. },
  2296. "optList": [
  2297. null,
  2298. "",
  2299. {},
  2300. []
  2301. ],
  2302. "optValue": "world",
  2303. "optEmpty": {},
  2304. "optAny": {
  2305. "@type": "google.protobuf.Empty",
  2306. "value": {}
  2307. },
  2308. "optFieldmask": "fooBar,barFoo"
  2309. }`,
  2310. wantMessage: &pb2.KnownTypes{
  2311. OptBool: &wrapperspb.BoolValue{Value: false},
  2312. OptInt32: &wrapperspb.Int32Value{Value: 42},
  2313. OptInt64: &wrapperspb.Int64Value{Value: 42},
  2314. OptUint32: &wrapperspb.UInt32Value{Value: 42},
  2315. OptUint64: &wrapperspb.UInt64Value{Value: 42},
  2316. OptFloat: &wrapperspb.FloatValue{Value: 1.23},
  2317. OptDouble: &wrapperspb.DoubleValue{Value: 3.1415},
  2318. OptString: &wrapperspb.StringValue{Value: "hello"},
  2319. OptBytes: &wrapperspb.BytesValue{Value: []byte("hello")},
  2320. OptDuration: &durationpb.Duration{Seconds: 123},
  2321. OptTimestamp: &timestamppb.Timestamp{Seconds: 1553036601},
  2322. OptStruct: &structpb.Struct{
  2323. Fields: map[string]*structpb.Value{
  2324. "string": {Kind: &structpb.Value_StringValue{"hello"}},
  2325. },
  2326. },
  2327. OptList: &structpb.ListValue{
  2328. Values: []*structpb.Value{
  2329. {Kind: &structpb.Value_NullValue{}},
  2330. {Kind: &structpb.Value_StringValue{}},
  2331. {
  2332. Kind: &structpb.Value_StructValue{
  2333. &structpb.Struct{Fields: map[string]*structpb.Value{}},
  2334. },
  2335. },
  2336. {
  2337. Kind: &structpb.Value_ListValue{
  2338. &structpb.ListValue{Values: []*structpb.Value{}},
  2339. },
  2340. },
  2341. },
  2342. },
  2343. OptValue: &structpb.Value{
  2344. Kind: &structpb.Value_StringValue{"world"},
  2345. },
  2346. OptEmpty: &emptypb.Empty{},
  2347. OptAny: &anypb.Any{
  2348. TypeUrl: "google.protobuf.Empty",
  2349. },
  2350. OptFieldmask: &fieldmaskpb.FieldMask{
  2351. Paths: []string{"foo_bar", "bar_foo"},
  2352. },
  2353. },
  2354. }, {
  2355. desc: "DiscardUnknown: regular messages",
  2356. umo: protojson.UnmarshalOptions{DiscardUnknown: true},
  2357. inputMessage: &pb3.Nests{},
  2358. inputText: `{
  2359. "sNested": {
  2360. "unknown": {
  2361. "foo": 1,
  2362. "bar": [1, 2, 3]
  2363. }
  2364. },
  2365. "unknown": "not known"
  2366. }`,
  2367. wantMessage: &pb3.Nests{SNested: &pb3.Nested{}},
  2368. }, {
  2369. desc: "DiscardUnknown: repeated",
  2370. umo: protojson.UnmarshalOptions{DiscardUnknown: true},
  2371. inputMessage: &pb2.Nests{},
  2372. inputText: `{
  2373. "rptNested": [
  2374. {"unknown": "blah"},
  2375. {"optString": "hello"}
  2376. ]
  2377. }`,
  2378. wantMessage: &pb2.Nests{
  2379. RptNested: []*pb2.Nested{
  2380. {},
  2381. {OptString: proto.String("hello")},
  2382. },
  2383. },
  2384. }, {
  2385. desc: "DiscardUnknown: map",
  2386. umo: protojson.UnmarshalOptions{DiscardUnknown: true},
  2387. inputMessage: &pb3.Maps{},
  2388. inputText: `{
  2389. "strToNested": {
  2390. "nested_one": {
  2391. "unknown": "what you see is not"
  2392. }
  2393. }
  2394. }`,
  2395. wantMessage: &pb3.Maps{
  2396. StrToNested: map[string]*pb3.Nested{
  2397. "nested_one": {},
  2398. },
  2399. },
  2400. }, {
  2401. desc: "DiscardUnknown: extension",
  2402. umo: protojson.UnmarshalOptions{DiscardUnknown: true},
  2403. inputMessage: &pb2.Extensions{},
  2404. inputText: `{
  2405. "[pb2.opt_ext_nested]": {
  2406. "unknown": []
  2407. }
  2408. }`,
  2409. wantMessage: func() proto.Message {
  2410. m := &pb2.Extensions{}
  2411. proto.SetExtension(m, pb2.E_OptExtNested, &pb2.Nested{})
  2412. return m
  2413. }(),
  2414. }, {
  2415. desc: "DiscardUnknown: Empty",
  2416. umo: protojson.UnmarshalOptions{DiscardUnknown: true},
  2417. inputMessage: &emptypb.Empty{},
  2418. inputText: `{"unknown": "something"}`,
  2419. wantMessage: &emptypb.Empty{},
  2420. }, {
  2421. desc: "DiscardUnknown: Any without type",
  2422. umo: protojson.UnmarshalOptions{DiscardUnknown: true},
  2423. inputMessage: &anypb.Any{},
  2424. inputText: `{
  2425. "value": {"foo": "bar"},
  2426. "unknown": true
  2427. }`,
  2428. wantMessage: &anypb.Any{},
  2429. }, {
  2430. desc: "DiscardUnknown: Any",
  2431. umo: protojson.UnmarshalOptions{
  2432. DiscardUnknown: true,
  2433. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  2434. },
  2435. inputMessage: &anypb.Any{},
  2436. inputText: `{
  2437. "@type": "foo/pb2.Nested",
  2438. "unknown": "none"
  2439. }`,
  2440. wantMessage: &anypb.Any{
  2441. TypeUrl: "foo/pb2.Nested",
  2442. },
  2443. }, {
  2444. desc: "DiscardUnknown: Any with Empty",
  2445. umo: protojson.UnmarshalOptions{
  2446. DiscardUnknown: true,
  2447. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&emptypb.Empty{})),
  2448. },
  2449. inputMessage: &anypb.Any{},
  2450. inputText: `{
  2451. "@type": "type.googleapis.com/google.protobuf.Empty",
  2452. "value": {"unknown": 47}
  2453. }`,
  2454. wantMessage: &anypb.Any{
  2455. TypeUrl: "type.googleapis.com/google.protobuf.Empty",
  2456. },
  2457. }, {
  2458. desc: "weak fields",
  2459. inputMessage: &testpb.TestWeak{},
  2460. inputText: `{"weak_message1":{"a":1}}`,
  2461. wantMessage: func() *testpb.TestWeak {
  2462. m := new(testpb.TestWeak)
  2463. m.SetWeakMessage1(&weakpb.WeakImportMessage1{A: proto.Int32(1)})
  2464. return m
  2465. }(),
  2466. skip: !flags.ProtoLegacy,
  2467. }, {
  2468. desc: "weak fields; unknown field",
  2469. inputMessage: &testpb.TestWeak{},
  2470. inputText: `{"weak_message1":{"a":1}, "weak_message2":{"a":1}}`,
  2471. wantErr: true, // weak_message2 is unknown since the package containing it is not imported
  2472. skip: !flags.ProtoLegacy,
  2473. }}
  2474. for _, tt := range tests {
  2475. tt := tt
  2476. if tt.skip {
  2477. continue
  2478. }
  2479. t.Run(tt.desc, func(t *testing.T) {
  2480. err := tt.umo.Unmarshal([]byte(tt.inputText), tt.inputMessage)
  2481. if err != nil && !tt.wantErr {
  2482. t.Errorf("Unmarshal() returned error: %v\n\n", err)
  2483. }
  2484. if err == nil && tt.wantErr {
  2485. t.Error("Unmarshal() got nil error, want error\n\n")
  2486. }
  2487. if tt.wantMessage != nil && !proto.Equal(tt.inputMessage, tt.wantMessage) {
  2488. t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", tt.inputMessage, tt.wantMessage)
  2489. }
  2490. })
  2491. }
  2492. }