decode_test.go 63 KB

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