Repository: GREsau/schemars
Pull request #240 (2023-08-27 21:54:11)

Add support for indexmap v2
Supersedes #226




Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index c86e2ab..d37634e 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -7,16 +7,16 @@ jobs:
     runs-on: ubuntu-latest
     env:
       # work-around https://github.com/rust-lang/cargo/issues/10303
-      CARGO_NET_GIT_FETCH_WITH_CLI: ${{ matrix.rust == '1.56.0' }}
+      CARGO_NET_GIT_FETCH_WITH_CLI: ${{ matrix.rust == '1.60.0' }}
     strategy:
       matrix:
         rust:
-          - 1.56.0
+          - 1.60.0
           - stable
           - beta
           - nightly
         include:
-          - rust: 1.56.0
+          - rust: 1.60.0
             test_features: "--features impl_json_schema"
             allow_failure: false
           - rust: stable
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 061a4a5..8d65444 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,10 +6,11 @@
 
 - Implement `JsonSchema` for `semver::Version` (https://github.com/GREsau/schemars/pull/195 / https://github.com/GREsau/schemars/pull/238)
 - Include const generics in generated schema names (https://github.com/GREsau/schemars/pull/179 / https://github.com/GREsau/schemars/pull/239)
+- Implement `JsonSchema` for types from indexmap v2 (https://github.com/GREsau/schemars/pull/226 / https://github.com/GREsau/schemars/pull/240)
 
 ### Changed:
 
-- Minimum supported rust version is now 1.56.0
+- Minimum supported rust version is now 1.60.0
 
 ## [0.8.12] - 2023-02-26
 
diff --git a/README.md b/README.md
index 8e5a39c..52b368f 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@
 [![CI Build](https://img.shields.io/github/actions/workflow/status/GREsau/schemars/ci.yml?branch=master&logo=GitHub)](https://github.com/GREsau/schemars/actions)
 [![Crates.io](https://img.shields.io/crates/v/schemars)](https://crates.io/crates/schemars)
 [![Docs](https://docs.rs/schemars/badge.svg)](https://docs.rs/schemars)
-[![MSRV 1.56+](https://img.shields.io/badge/schemars-rustc_1.56+-lightgray.svg)](https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html)
+[![MSRV 1.60+](https://img.shields.io/badge/schemars-rustc_1.60+-lightgray.svg)](https://blog.rust-lang.org/2022/04/07/Rust-1.60.0.html)
 
 Generate JSON Schema documents from Rust code
 
@@ -258,6 +258,7 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
 
 - `chrono` - [chrono](https://crates.io/crates/chrono) (^0.4)
 - `indexmap1` - [indexmap](https://crates.io/crates/indexmap) (^1.2)
+- `indexmap2` - [indexmap](https://crates.io/crates/indexmap) (^2.0)
 - `either` - [either](https://crates.io/crates/either) (^1.3)
 - `uuid08` - [uuid](https://crates.io/crates/uuid) (^0.8)
 - `uuid1` - [uuid](https://crates.io/crates/uuid) (^1.0)
diff --git a/docs/4-features.md b/docs/4-features.md
index 5112773..9c7e0a5 100644
--- a/docs/4-features.md
+++ b/docs/4-features.md
@@ -6,13 +6,16 @@ permalink: /features/
 ---
 
 # Feature Flags and Optional Dependencies
+
 - `derive` (enabled by default) - provides `#[derive(JsonSchema)]` macro
 - `impl_json_schema` - implements `JsonSchema` for Schemars types themselves
 - `preserve_order` - keep the order of struct fields in `Schema` and `SchemaObject`
 
 Schemars can implement `JsonSchema` on types from several popular crates, enabled via feature flags (dependency versions are shown in brackets):
+
 - `chrono` - [chrono](https://crates.io/crates/chrono) (^0.4)
 - `indexmap1` - [indexmap](https://crates.io/crates/indexmap) (^1.2)
+- `indexmap2` - [indexmap](https://crates.io/crates/indexmap) (^2.0)
 - `either` - [either](https://crates.io/crates/either) (^1.3)
 - `uuid08` - [uuid](https://crates.io/crates/uuid) (^0.8)
 - `uuid1` - [uuid](https://crates.io/crates/uuid) (^1.0)
diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml
index aaf7b3d..8470a24 100644
--- a/schemars/Cargo.toml
+++ b/schemars/Cargo.toml
@@ -11,7 +11,7 @@ readme = "README.md"
 keywords = ["rust", "json-schema", "serde"]
 categories = ["encoding"]
 build = "build.rs"
-rust-version = "1.56"
+rust-version = "1.60"
 
 [dependencies]
 schemars_derive = { version = "=0.8.12", optional = true, path = "../schemars_derive" }
@@ -21,6 +21,7 @@ dyn-clone = "1.0"
 
 chrono = { version = "0.4", default-features = false, optional = true }
 indexmap = { version = "1.2", features = ["serde-1"], optional = true }
+indexmap2 = { version = "2.0", features = ["serde"], optional = true, package = "indexmap" }
 either = { version = "1.3", default-features = false, optional = true }
 uuid08 = { version = "0.8", default-features = false, optional = true, package = "uuid" }
 uuid1 = { version = "1.0", default-features = false, optional = true, package = "uuid" }
@@ -69,6 +70,10 @@ required-features = ["chrono"]
 name = "indexmap"
 required-features = ["indexmap"]
 
+[[test]]
+name = "indexmap2"
+required-features = ["indexmap2"]
+
 [[test]]
 name = "either"
 required-features = ["either"]
diff --git a/schemars/src/json_schema_impls/indexmap2.rs b/schemars/src/json_schema_impls/indexmap2.rs
new file mode 100644
index 0000000..44eeedb
--- /dev/null
+++ b/schemars/src/json_schema_impls/indexmap2.rs
@@ -0,0 +1,8 @@
+use crate::gen::SchemaGenerator;
+use crate::schema::*;
+use crate::JsonSchema;
+use indexmap2::{IndexMap, IndexSet};
+use std::collections::{HashMap, HashSet};
+
+forward_impl!((<K, V: JsonSchema, H> JsonSchema for IndexMap<K, V, H>) => HashMap<K, V, H>);
+forward_impl!((<T: JsonSchema, H> JsonSchema for IndexSet<T, H>) => HashSet<T, H>);
diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs
index d96bf38..22d1c46 100644
--- a/schemars/src/json_schema_impls/mod.rs
+++ b/schemars/src/json_schema_impls/mod.rs
@@ -56,6 +56,8 @@ mod enumset;
 mod ffi;
 #[cfg(feature = "indexmap")]
 mod indexmap;
+#[cfg(feature = "indexmap2")]
+mod indexmap2;
 mod maps;
 mod nonzero_signed;
 mod nonzero_unsigned;
diff --git a/schemars_derive/Cargo.toml b/schemars_derive/Cargo.toml
index 8bb235e..0b34e49 100644
--- a/schemars_derive/Cargo.toml
+++ b/schemars_derive/Cargo.toml
@@ -9,7 +9,7 @@ edition = "2021"
 license = "MIT"
 readme = "README.md"
 keywords = ["rust", "json-schema", "serde"]
-rust-version = "1.56"
+rust-version = "1.60"
 
 [lib]
 proc-macro = true


Test patch:
diff --git a/schemars/tests/indexmap2.rs b/schemars/tests/indexmap2.rs
new file mode 100644
index 0000000..efc77dd
--- /dev/null
+++ b/schemars/tests/indexmap2.rs
@@ -0,0 +1,16 @@
+mod util;
+use indexmap2::{IndexMap, IndexSet};
+use schemars::JsonSchema;
+use util::*;
+
+#[allow(dead_code)]
+#[derive(JsonSchema)]
+struct IndexMapTypes {
+    map: IndexMap<i32, bool>,
+    set: IndexSet<isize>,
+}
+
+#[test]
+fn indexmap_types() -> TestResult {
+    test_default_generated_schema::<IndexMapTypes>("indexmap")
+}