predawn_schema/impls/
map.rs

1use std::{borrow::Cow, collections::BTreeMap};
2
3use openapiv3::{AdditionalProperties, ObjectType, Schema, SchemaData, SchemaKind, Type};
4
5use crate::ToSchema;
6
7macro_rules! map_impl {
8    ($($desc:tt)+) => {
9        impl $($desc)+
10        where
11            V: ToSchema
12        {
13            fn title() -> Cow<'static, str> {
14                format!("Map<String, {}>", V::title()).into()
15            }
16
17            fn schema(schemas: &mut BTreeMap<String, Schema>, schemas_in_progress: &mut Vec<String>) -> Schema {
18                let ty = ObjectType {
19                    additional_properties: Some(AdditionalProperties::Schema(Box::new(V::schema_ref(schemas, schemas_in_progress)))),
20                    ..Default::default()
21                };
22
23                Schema {
24                    schema_data: SchemaData {
25                        title: Some(Self::title().into()),
26                        ..Default::default()
27                    },
28                    schema_kind: SchemaKind::Type(Type::Object(ty)),
29                }
30            }
31        }
32    };
33}
34
35map_impl!(<K, V> ToSchema for std::collections::BTreeMap<K, V>);
36map_impl!(<K, V, S> ToSchema for std::collections::HashMap<K, V, S>);
37map_impl!(<K, V, S> ToSchema for indexmap::map::IndexMap<K, V, S>);