decode_test.go 64 KB

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