diff --git a/Cargo.lock b/Cargo.lock index a6d809b6f..61ffbb6b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -522,6 +522,18 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "const-oid" version = "0.9.6" @@ -615,6 +627,7 @@ version = "2.2.0-rc.1" dependencies = [ "anyhow", "cosmwasm-schema-derive", + "insta", "schemars", "semver", "serde", @@ -1086,6 +1099,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "english-numbers" version = "0.3.3" @@ -1374,6 +1393,19 @@ dependencies = [ "hashbrown 0.14.5", ] +[[package]] +name = "insta" +version = "1.41.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e9ffc4d4892617c50a928c52b2961cb5174b6fc6ebf252b2fac9d21955c48b8" +dependencies = [ + "console", + "lazy_static", + "linked-hash-map", + "serde", + "similar", +] + [[package]] name = "is-terminal" version = "0.4.12" @@ -1460,6 +1492,12 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -2241,6 +2279,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" +[[package]] +name = "similar" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e" + [[package]] name = "smallvec" version = "1.13.2" diff --git a/packages/schema/Cargo.toml b/packages/schema/Cargo.toml index 0ea8618e7..54844b3c4 100644 --- a/packages/schema/Cargo.toml +++ b/packages/schema/Cargo.toml @@ -19,5 +19,6 @@ thiserror = "1.0.26" [dev-dependencies] anyhow = "1.0.57" +insta = { version = "1.41.1", features = ["json"] } semver = "1" tempfile = "3" diff --git a/packages/schema/src/schema_for.rs b/packages/schema/src/schema_for.rs index ab36df1c2..a8684b598 100644 --- a/packages/schema/src/schema_for.rs +++ b/packages/schema/src/schema_for.rs @@ -30,6 +30,10 @@ macro_rules! schema_for { $crate::schemars::visit::visit_schema_object(self, schema); if let Some(ref mut validation) = schema.object { + if validation.additional_properties.is_some() { + return; + } + validation.additional_properties = Some(Box::new(false.into())); } } diff --git a/packages/schema/tests/idl.rs b/packages/schema/tests/idl.rs index 57d1a3a0b..490b01b9c 100644 --- a/packages/schema/tests/idl.rs +++ b/packages/schema/tests/idl.rs @@ -32,6 +32,18 @@ pub struct MigrateMsg { pub cap: u128, } +#[cw_serde] +pub struct MapMsg { + btree: std::collections::BTreeMap, + hash: std::collections::HashMap, +} + +#[test] +fn assert_maps_generate_correctly() { + let schema = cosmwasm_schema::schema_for!(MapMsg); + insta::assert_json_snapshot!(schema); +} + #[test] fn unknown_fields_explicitly_allowed() { let json = serde_json::json!({ diff --git a/packages/schema/tests/snapshots/idl__assert_maps_generate_correctly.snap b/packages/schema/tests/snapshots/idl__assert_maps_generate_correctly.snap new file mode 100644 index 000000000..629148a4e --- /dev/null +++ b/packages/schema/tests/snapshots/idl__assert_maps_generate_correctly.snap @@ -0,0 +1,32 @@ +--- +source: packages/schema/tests/idl.rs +expression: schema +--- +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MapMsg", + "type": "object", + "required": [ + "btree", + "hash" + ], + "properties": { + "btree": { + "type": "object", + "additionalProperties": { + "type": "integer", + "format": "uint32", + "minimum": 0.0 + } + }, + "hash": { + "type": "object", + "additionalProperties": { + "type": "integer", + "format": "uint32", + "minimum": 0.0 + } + } + }, + "additionalProperties": false +}