Implement JsonSchema on Either
This commit is contained in:
parent
fd42debc4d
commit
fbd019baae
8 changed files with 58 additions and 4 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -16,15 +16,15 @@ schemars = { version = "0.6", features = ["chrono"] }
|
|||
<div class="indented">
|
||||
|
||||
### 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).
|
||||
|
||||
</div>
|
||||
|
|
|
@ -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"]
|
||||
|
|
18
schemars/src/json_schema_impls/either.rs
Normal file
18
schemars/src/json_schema_impls/either.rs
Normal 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()
|
||||
}
|
||||
}
|
|
@ -38,6 +38,8 @@ mod atomic;
|
|||
mod chrono;
|
||||
#[cfg(feature = "indexmap")]
|
||||
mod indexmap;
|
||||
#[cfg(feature = "either")]
|
||||
mod either;
|
||||
mod core;
|
||||
mod ffi;
|
||||
mod maps;
|
||||
|
|
8
schemars/tests/either.rs
Normal file
8
schemars/tests/either.rs
Normal 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")
|
||||
}
|
20
schemars/tests/expected/either.json
Normal file
20
schemars/tests/expected/either.json
Normal 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -10,6 +10,6 @@ struct IndexMapTypes {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn chrono_types() -> TestResult {
|
||||
fn indexmap_types() -> TestResult {
|
||||
test_default_generated_schema::<IndexMapTypes>("indexmap")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue