schemars/json_schema_impls/
maps.rs
1use crate::gen::SchemaGenerator;
2use crate::schema::*;
3use crate::JsonSchema;
4
5macro_rules! map_impl {
6 ($($desc:tt)+) => {
7 impl $($desc)+
8 where
9 V: JsonSchema,
10 {
11 no_ref_schema!();
12
13 fn schema_name() -> String {
14 format!("Map_of_{}", V::schema_name())
15 }
16
17 fn json_schema(gen: &mut SchemaGenerator) -> Schema {
18 let subschema = gen.subschema_for::<V>();
19 SchemaObject {
20 instance_type: Some(InstanceType::Object.into()),
21 object: Some(Box::new(ObjectValidation {
22 additional_properties: Some(Box::new(subschema)),
23 ..Default::default()
24 })),
25 ..Default::default()
26 }
27 .into()
28 }
29 }
30 };
31}
32
33map_impl!(<K, V> JsonSchema for std::collections::BTreeMap<K, V>);
34map_impl!(<K, V, H> JsonSchema for std::collections::HashMap<K, V, H>);