decode_test.go 67 KB

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