Implement JsonSchema on indexmap types

This commit is contained in:
Graham Esau 2019-12-27 21:44:50 +00:00
parent 8d0ccc89db
commit fd42debc4d
8 changed files with 60 additions and 1 deletions

View file

@ -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

View file

@ -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).
</div>

View file

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

View file

@ -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!((<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>);

View file

@ -6,7 +6,6 @@ macro_rules! map_impl {
($($desc:tt)+) => {
impl $($desc)+
where
K: Into<String>,
V: JsonSchema,
{
no_ref_schema!();

View file

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

View file

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

View file

@ -0,0 +1,15 @@
mod util;
use indexmap::{IndexMap, IndexSet};
use schemars::JsonSchema;
use util::*;
#[derive(Debug, JsonSchema)]
struct IndexMapTypes {
map: IndexMap<i32, bool>,
set: IndexSet<isize>,
}
#[test]
fn chrono_types() -> TestResult {
test_default_generated_schema::<IndexMapTypes>("indexmap")
}