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 std::collections::BTreeMap;

use openapiv3::{AdditionalProperties, ObjectType, Schema, SchemaData, SchemaKind, Type};

use crate::ToSchema;

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

                let ty = ObjectType {
                    additional_properties: Some(AdditionalProperties::Schema(Box::new(V::schema_ref(schemas, schemas_in_progress)))),
                    ..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>);