Implement JsonSchema on Either

This commit is contained in:
Graham Esau 2019-12-27 22:16:01 +00:00
parent fd42debc4d
commit fbd019baae
8 changed files with 58 additions and 4 deletions

View file

@ -209,5 +209,6 @@ fn main() {
## Feature Flags ## Feature Flags
- `chrono` - implements `JsonSchema` for all [Chrono](https://github.com/chronotope/chrono) types which are serializable by Serde. - `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). - `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 - `impl_json_schema` - implements `JsonSchema` for Schemars types themselves

View file

@ -16,15 +16,15 @@ schemars = { version = "0.6", features = ["chrono"] }
<div class="indented"> <div class="indented">
### impl_json_schema ### impl_json_schema
Implements `JsonSchema` on Schemars types themselves. Implements `JsonSchema` on Schemars types themselves.
### chrono ### chrono
Implements `JsonSchema` on all [Chrono](https://github.com/chronotope/chrono) types which are serializable by Serde. Implements `JsonSchema` on all [Chrono](https://github.com/chronotope/chrono) types which are serializable by Serde.
### indexmap ### indexmap
Implements `JsonSchema` on `IndexMap` and `IndexSet` from [indexmap](https://github.com/bluss/indexmap). Implements `JsonSchema` on `IndexMap` and `IndexSet` from [indexmap](https://github.com/bluss/indexmap).
### either
Implements `JsonSchema` on [`Either`](https://github.com/bluss/either).
</div> </div>

View file

@ -19,6 +19,7 @@ serde_json = "1.0"
chrono = { version = "0.4", default-features = false, optional = true } chrono = { version = "0.4", default-features = false, optional = true }
indexmap = { version = "1.2", optional = true } indexmap = { version = "1.2", optional = true }
either = { version = "1.3", optional = true }
[dev-dependencies] [dev-dependencies]
pretty_assertions = "0.6.1" pretty_assertions = "0.6.1"
@ -36,6 +37,10 @@ required-features = ["chrono"]
name = "indexmap" name = "indexmap"
required-features = ["indexmap"] required-features = ["indexmap"]
[[test]]
name = "either"
required-features = ["either"]
[[test]] [[test]]
name = "schema_for_schema" name = "schema_for_schema"
required-features = ["impl_json_schema"] required-features = ["impl_json_schema"]

View file

@ -0,0 +1,18 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use either::Either;
impl<L: JsonSchema, R: JsonSchema> JsonSchema for Either<L, R> {
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::<L>(), gen.subschema_for::<R>()]);
schema.into()
}
}

View file

@ -38,6 +38,8 @@ mod atomic;
mod chrono; mod chrono;
#[cfg(feature = "indexmap")] #[cfg(feature = "indexmap")]
mod indexmap; mod indexmap;
#[cfg(feature = "either")]
mod either;
mod core; mod core;
mod ffi; mod ffi;
mod maps; mod maps;

8
schemars/tests/either.rs Normal file
View file

@ -0,0 +1,8 @@
mod util;
use either::Either;
use util::*;
#[test]
fn either() -> TestResult {
test_default_generated_schema::<Either<i32, Either<bool, ()>>>("either")
}

View file

@ -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"
}
]
}
]
}

View file

@ -10,6 +10,6 @@ struct IndexMapTypes {
} }
#[test] #[test]
fn chrono_types() -> TestResult { fn indexmap_types() -> TestResult {
test_default_generated_schema::<IndexMapTypes>("indexmap") test_default_generated_schema::<IndexMapTypes>("indexmap")
} }