decode_test.go 67 KB

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