diff --git a/README.md b/README.md index afb7d2b..bfea89e 100644 --- a/README.md +++ b/README.md @@ -208,5 +208,6 @@ fn main() { ## Feature Flags - `chrono` - implements `JsonSchema` for all [Chrono](https://github.com/chronotope/chrono) types which are serializable by Serde. +- `indexmap` - implements `JsonSchema` on `IndexMap` and `IndexSet` from [indexmap](https://github.com/bluss/indexmap). - `impl_json_schema` - implements `JsonSchema` for Schemars types themselves diff --git a/docs/4-features.md b/docs/4-features.md index 93bb7b9..6e810a0 100644 --- a/docs/4-features.md +++ b/docs/4-features.md @@ -23,4 +23,8 @@ Implements `JsonSchema` on Schemars types themselves. Implements `JsonSchema` on all [Chrono](https://github.com/chronotope/chrono) types which are serializable by Serde. +### indexmap + +Implements `JsonSchema` on `IndexMap` and `IndexSet` from [indexmap](https://github.com/bluss/indexmap). + diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index 53b0f70..c91a8a9 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -16,7 +16,9 @@ build = "build.rs" schemars_derive = { version = "=0.6.3", path = "../schemars_derive" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" + chrono = { version = "0.4", default-features = false, optional = true } +indexmap = { version = "1.2", optional = true } [dev-dependencies] pretty_assertions = "0.6.1" @@ -30,6 +32,10 @@ derive_json_schema = ["impl_json_schema"] name = "chrono" required-features = ["chrono"] +[[test]] +name = "indexmap" +required-features = ["indexmap"] + [[test]] name = "schema_for_schema" required-features = ["impl_json_schema"] diff --git a/schemars/src/json_schema_impls/indexmap.rs b/schemars/src/json_schema_impls/indexmap.rs new file mode 100644 index 0000000..3bd3e54 --- /dev/null +++ b/schemars/src/json_schema_impls/indexmap.rs @@ -0,0 +1,8 @@ +use crate::gen::SchemaGenerator; +use crate::schema::*; +use crate::JsonSchema; +use indexmap::{IndexMap, IndexSet}; +use std::collections::{HashMap, HashSet}; + +forward_impl!(( JsonSchema for IndexMap) => HashMap); +forward_impl!(( JsonSchema for IndexSet) => HashSet); diff --git a/schemars/src/json_schema_impls/maps.rs b/schemars/src/json_schema_impls/maps.rs index c109b00..73f3313 100644 --- a/schemars/src/json_schema_impls/maps.rs +++ b/schemars/src/json_schema_impls/maps.rs @@ -6,7 +6,6 @@ macro_rules! map_impl { ($($desc:tt)+) => { impl $($desc)+ where - K: Into, V: JsonSchema, { no_ref_schema!(); diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index 8db5fd6..389719c 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -36,6 +36,8 @@ mod array; mod atomic; #[cfg(feature = "chrono")] mod chrono; +#[cfg(feature = "indexmap")] +mod indexmap; mod core; mod ffi; mod maps; diff --git a/schemars/tests/expected/indexmap.json b/schemars/tests/expected/indexmap.json new file mode 100644 index 0000000..e9d4df4 --- /dev/null +++ b/schemars/tests/expected/indexmap.json @@ -0,0 +1,24 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "IndexMapTypes", + "type": "object", + "required": [ + "map", + "set" + ], + "properties": { + "map": { + "type": "object", + "additionalProperties": { + "type": "boolean" + } + }, + "set": { + "type": "array", + "items": { + "type": "integer", + "format": "int" + } + } + } +} \ No newline at end of file diff --git a/schemars/tests/indexmap.rs b/schemars/tests/indexmap.rs new file mode 100644 index 0000000..1f0c70f --- /dev/null +++ b/schemars/tests/indexmap.rs @@ -0,0 +1,15 @@ +mod util; +use indexmap::{IndexMap, IndexSet}; +use schemars::JsonSchema; +use util::*; + +#[derive(Debug, JsonSchema)] +struct IndexMapTypes { + map: IndexMap, + set: IndexSet, +} + +#[test] +fn chrono_types() -> TestResult { + test_default_generated_schema::("indexmap") +}