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