diff --git a/README.md b/README.md index bfea89e..c09440a 100644 --- a/README.md +++ b/README.md @@ -209,5 +209,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). +- `either` - implements `JsonSchema` on [`Either`](https://github.com/bluss/either). - `impl_json_schema` - implements `JsonSchema` for Schemars types themselves diff --git a/docs/4-features.md b/docs/4-features.md index 6e810a0..d5a415c 100644 --- a/docs/4-features.md +++ b/docs/4-features.md @@ -16,15 +16,15 @@ schemars = { version = "0.6", features = ["chrono"] }
### impl_json_schema - Implements `JsonSchema` on Schemars types themselves. ### chrono - 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). +### either +Implements `JsonSchema` on [`Either`](https://github.com/bluss/either). +
diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index c91a8a9..4704737 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -19,6 +19,7 @@ serde_json = "1.0" chrono = { version = "0.4", default-features = false, optional = true } indexmap = { version = "1.2", optional = true } +either = { version = "1.3", optional = true } [dev-dependencies] pretty_assertions = "0.6.1" @@ -36,6 +37,10 @@ required-features = ["chrono"] name = "indexmap" required-features = ["indexmap"] +[[test]] +name = "either" +required-features = ["either"] + [[test]] name = "schema_for_schema" required-features = ["impl_json_schema"] diff --git a/schemars/src/json_schema_impls/either.rs b/schemars/src/json_schema_impls/either.rs new file mode 100644 index 0000000..1010bc4 --- /dev/null +++ b/schemars/src/json_schema_impls/either.rs @@ -0,0 +1,18 @@ +use crate::gen::SchemaGenerator; +use crate::schema::*; +use crate::JsonSchema; +use either::Either; + +impl JsonSchema for Either { + no_ref_schema!(); + + fn schema_name() -> String { + format!("Either_{}_or_{}", L::schema_name(), R::schema_name()) + } + + fn json_schema(gen: &mut SchemaGenerator) -> Schema { + let mut schema = SchemaObject::default(); + schema.subschemas().any_of = Some(vec![gen.subschema_for::(), gen.subschema_for::()]); + schema.into() + } +} diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index 389719c..9b5b07b 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -38,6 +38,8 @@ mod atomic; mod chrono; #[cfg(feature = "indexmap")] mod indexmap; +#[cfg(feature = "either")] +mod either; mod core; mod ffi; mod maps; diff --git a/schemars/tests/either.rs b/schemars/tests/either.rs new file mode 100644 index 0000000..16a2e59 --- /dev/null +++ b/schemars/tests/either.rs @@ -0,0 +1,8 @@ +mod util; +use either::Either; +use util::*; + +#[test] +fn either() -> TestResult { + test_default_generated_schema::>>("either") +} diff --git a/schemars/tests/expected/either.json b/schemars/tests/expected/either.json new file mode 100644 index 0000000..b028057 --- /dev/null +++ b/schemars/tests/expected/either.json @@ -0,0 +1,20 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Either_int32_or_Either_Boolean_or_Null", + "anyOf": [ + { + "type": "integer", + "format": "int32" + }, + { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "null" + } + ] + } + ] +} \ No newline at end of file diff --git a/schemars/tests/indexmap.rs b/schemars/tests/indexmap.rs index 1f0c70f..ad89cc7 100644 --- a/schemars/tests/indexmap.rs +++ b/schemars/tests/indexmap.rs @@ -10,6 +10,6 @@ struct IndexMapTypes { } #[test] -fn chrono_types() -> TestResult { +fn indexmap_types() -> TestResult { test_default_generated_schema::("indexmap") }