From dc26305878d1447d0ecb3429e7c4e7ec9a769d48 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Thu, 12 Dec 2024 14:02:57 +0100 Subject: [PATCH 1/4] Only set unset `additionalProperties` fields to `false` --- Cargo.lock | 46 ++++++++++++++++++- packages/schema/Cargo.toml | 1 + packages/schema/src/schema_for.rs | 4 ++ packages/schema/tests/idl.rs | 12 +++++ .../idl__assert_maps_generate_correctly.snap | 32 +++++++++++++ 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 packages/schema/tests/snapshots/idl__assert_maps_generate_correctly.snap diff --git a/Cargo.lock b/Cargo.lock index a6d809b6fc..8dc897bfc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "Inflector" @@ -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 0ea8618e7b..54844b3c45 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 ab36df1c25..a8684b5984 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 57d1a3a0b8..490b01b9c1 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 0000000000..629148a4e5 --- /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 +} From 86c5223e2d1658bc063c28cc52be7fda9eb1947b Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Thu, 12 Dec 2024 14:12:38 +0100 Subject: [PATCH 2/4] I'm an expert. I'm allowed to do this --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 8dc897bfc5..61ffbb6b5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 4 +version = 3 [[package]] name = "Inflector" From 90193e6aa681b5da5f83f5f5b0a7de7ff0214628 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Mon, 6 Jan 2025 15:51:07 +0100 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f2dfddecc..7bef1f3e2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,10 @@ and this project adheres to - cosmwasm-vm: Automatically derive cache version from function hashes and the Wasmer version ([#2250]) +## Fixed + +- cosmwasm-schema: The schema export now doesn't overwrite existing `additionalProperties` values anymore ([#2310]) + [#2118]: https://github.com/CosmWasm/cosmwasm/pull/2118 [#2211]: https://github.com/CosmWasm/cosmwasm/issues/2211 [#2246]: https://github.com/CosmWasm/cosmwasm/pull/2246 @@ -76,6 +80,7 @@ and this project adheres to [#2255]: https://github.com/CosmWasm/cosmwasm/pull/2255 [#2260]: https://github.com/CosmWasm/cosmwasm/pull/2260 [#2250]: https://github.com/CosmWasm/cosmwasm/pull/2250 +[#2310]: https://github.com/CosmWasm/cosmwasm/pull/2310 ## [2.1.3] - 2024-08-08 From a0f2cdf8753fb0d2c6f9f89b2f803d816a1e0eb1 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 6 Jan 2025 14:59:13 +0000 Subject: [PATCH 4/4] [autofix.ci] apply automated fixes --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bef1f3e2b..c421100b18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,7 +71,8 @@ and this project adheres to ## Fixed -- cosmwasm-schema: The schema export now doesn't overwrite existing `additionalProperties` values anymore ([#2310]) +- cosmwasm-schema: The schema export now doesn't overwrite existing + `additionalProperties` values anymore ([#2310]) [#2118]: https://github.com/CosmWasm/cosmwasm/pull/2118 [#2211]: https://github.com/CosmWasm/cosmwasm/issues/2211