Implement JsonSchema on indexmap types
This commit is contained in:
parent
8d0ccc89db
commit
fd42debc4d
8 changed files with 60 additions and 1 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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"]
|
||||
|
|
8
schemars/src/json_schema_impls/indexmap.rs
Normal file
8
schemars/src/json_schema_impls/indexmap.rs
Normal 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>);
|
|
@ -6,7 +6,6 @@ macro_rules! map_impl {
|
|||
($($desc:tt)+) => {
|
||||
impl $($desc)+
|
||||
where
|
||||
K: Into<String>,
|
||||
V: JsonSchema,
|
||||
{
|
||||
no_ref_schema!();
|
||||
|
|
|
@ -36,6 +36,8 @@ mod array;
|
|||
mod atomic;
|
||||
#[cfg(feature = "chrono")]
|
||||
mod chrono;
|
||||
#[cfg(feature = "indexmap")]
|
||||
mod indexmap;
|
||||
mod core;
|
||||
mod ffi;
|
||||
mod maps;
|
||||
|
|
24
schemars/tests/expected/indexmap.json
Normal file
24
schemars/tests/expected/indexmap.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
15
schemars/tests/indexmap.rs
Normal file
15
schemars/tests/indexmap.rs
Normal 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")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue