From 67d302e4f0e15a28535e6b73d325683cb0180f59 Mon Sep 17 00:00:00 2001 From: Ranadeep Biswas Date: Mon, 20 Nov 2023 15:38:25 +0100 Subject: [PATCH] test for failure when deserialize bigint to int in enum --- itf/tests/regression.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/itf/tests/regression.rs b/itf/tests/regression.rs index f37bfe0..9491d43 100644 --- a/itf/tests/regression.rs +++ b/itf/tests/regression.rs @@ -136,6 +136,35 @@ fn test_bigint_to_int() { assert!(itf::from_value::(itf).is_ok()); } +#[test] +fn test_enum_deserialization_failure() { + let itf = serde_json::json!({ + "_foo": {"#bigint": "1"}, + "_bar": "", + "typ": "Foo", + }); + + #[derive(Deserialize, Debug)] + #[serde(tag = "typ")] + enum FooBarInt { + // try to deserialize _foo as i64, instead of BigInt + Foo { _foo: i64 }, + Bar { _bar: String }, + } + + assert!(itf::from_value::(itf.clone()).is_err()); + + #[derive(Deserialize, Debug)] + #[serde(tag = "typ")] + enum FooBarBigInt { + // can deserialize _foo to BigInt + Foo { _foo: num_bigint::BigInt }, + Bar { _bar: String }, + } + + assert!(itf::from_value::(itf).is_ok()); +} + #[test] fn test_complete() { use std::collections::{BTreeSet, HashMap, HashSet};