1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use openapiv3::{
    AdditionalProperties, ObjectType, ReferenceOr, Schema, SchemaData, SchemaKind, Type,
};

use crate::ToSchema;

macro_rules! map_impl {
    ($($desc:tt)+) => {
        impl $($desc)+
        where
            V: ToSchema
        {
            fn schema() -> Schema {
                let value = V::schema();
                let title = value.schema_data.title.as_deref().unwrap_or("Unknown");
                let title = format!("Map<String, {}>", title);

                let ty = ObjectType {
                    additional_properties: Some(AdditionalProperties::Schema(Box::new(ReferenceOr::Item(value)))),
                    ..Default::default()
                };

                Schema {
                    schema_data: SchemaData {
                        title: Some(title),
                        ..Default::default()
                    },
                    schema_kind: SchemaKind::Type(Type::Object(ty)),
                }
            }
        }
    };
}

map_impl!(<K, V> ToSchema for std::collections::BTreeMap<K, V>);
map_impl!(<K, V, S> ToSchema for std::collections::HashMap<K, V, S>);
map_impl!(<K, V, S> ToSchema for indexmap::map::IndexMap<K, V, S>);