decode_test.go 63 KB

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