decode_test.go 64 KB

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