schemars/
_private.rs

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use crate::gen::SchemaGenerator;
use crate::schema::{InstanceType, ObjectValidation, Schema, SchemaObject};
use crate::{JsonSchema, Map, Set};
use serde::Serialize;
use serde_json::Value;

// Helper for generating schemas for flattened `Option` fields.
pub fn json_schema_for_flatten<T: ?Sized + JsonSchema>(
    gen: &mut SchemaGenerator,
    required: bool,
) -> Schema {
    let mut schema = T::_schemars_private_non_optional_json_schema(gen);

    if T::_schemars_private_is_option() && !required {
        if let Schema::Object(SchemaObject {
            object: Some(ref mut object_validation),
            ..
        }) = schema
        {
            object_validation.required.clear();
        }
    }

    schema
}

/// Hack to simulate specialization:
/// `MaybeSerializeWrapper(x).maybe_to_value()` will resolve to either
/// - The inherent method `MaybeSerializeWrapper::maybe_to_value(...)` if x is `Serialize`
/// - The trait method `NoSerialize::maybe_to_value(...)` from the blanket impl otherwise
#[doc(hidden)]
#[macro_export]
macro_rules! _schemars_maybe_to_value {
    ($expression:expr) => {{
        #[allow(unused_imports)]
        use $crate::_private::{MaybeSerializeWrapper, NoSerialize as _};

        MaybeSerializeWrapper($expression).maybe_to_value()
    }};
}

pub struct MaybeSerializeWrapper<T>(pub T);

pub trait NoSerialize: Sized {
    fn maybe_to_value(self) -> Option<Value> {
        None
    }
}

impl<T> NoSerialize for T {}

impl<T: Serialize> MaybeSerializeWrapper<T> {
    pub fn maybe_to_value(self) -> Option<Value> {
        serde_json::value::to_value(self.0).ok()
    }
}

/// Create a schema for a unit enum
pub fn new_unit_enum(variant: &str) -> Schema {
    Schema::Object(SchemaObject {
        instance_type: Some(InstanceType::String.into()),
        enum_values: Some(vec![variant.into()]),
        ..SchemaObject::default()
    })
}

/// Create a schema for an externally tagged enum
pub fn new_externally_tagged_enum(variant: &str, sub_schema: Schema) -> Schema {
    Schema::Object(SchemaObject {
        instance_type: Some(InstanceType::Object.into()),
        object: Some(Box::new(ObjectValidation {
            properties: {
                let mut props = Map::new();
                props.insert(variant.to_owned(), sub_schema);
                props
            },
            required: {
                let mut required = Set::new();
                required.insert(variant.to_owned());
                required
            },
            // Externally tagged variants must prohibit additional
            // properties irrespective of the disposition of
            // `deny_unknown_fields`. If additional properties were allowed
            // one could easily construct an object that validated against
            // multiple variants since here it's the properties rather than
            // the values of a property that distingish between variants.
            additional_properties: Some(Box::new(false.into())),
            ..Default::default()
        })),
        ..SchemaObject::default()
    })
}

/// Create a schema for an internally tagged enum
pub fn new_internally_tagged_enum(
    tag_name: &str,
    variant: &str,
    deny_unknown_fields: bool,
) -> Schema {
    let tag_schema = Schema::Object(SchemaObject {
        instance_type: Some(InstanceType::String.into()),
        enum_values: Some(vec![variant.into()]),
        ..Default::default()
    });
    Schema::Object(SchemaObject {
        instance_type: Some(InstanceType::Object.into()),
        object: Some(Box::new(ObjectValidation {
            properties: {
                let mut props = Map::new();
                props.insert(tag_name.to_owned(), tag_schema);
                props
            },
            required: {
                let mut required = Set::new();
                required.insert(tag_name.to_owned());
                required
            },
            additional_properties: deny_unknown_fields.then(|| Box::new(false.into())),
            ..Default::default()
        })),
        ..SchemaObject::default()
    })
}

pub fn insert_object_property<T: ?Sized + JsonSchema>(
    obj: &mut ObjectValidation,
    key: &str,
    has_default: bool,
    required: bool,
    schema: Schema,
) {
    obj.properties.insert(key.to_owned(), schema);
    if !has_default && (required || !T::_schemars_private_is_option()) {
        obj.required.insert(key.to_owned());
    }
}

pub mod metadata {
    use crate::Schema;
    use serde_json::Value;

    macro_rules! add_metadata_fn {
        ($method:ident, $name:ident, $ty:ty) => {
            pub fn $method(schema: Schema, $name: impl Into<$ty>) -> Schema {
                let value = $name.into();
                if value == <$ty>::default() {
                    schema
                } else {
                    let mut schema_obj = schema.into_object();
                    schema_obj.metadata().$name = value.into();
                    Schema::Object(schema_obj)
                }
            }
        };
    }

    add_metadata_fn!(add_description, description, String);
    add_metadata_fn!(add_id, id, String);
    add_metadata_fn!(add_title, title, String);
    add_metadata_fn!(add_deprecated, deprecated, bool);
    add_metadata_fn!(add_read_only, read_only, bool);
    add_metadata_fn!(add_write_only, write_only, bool);
    add_metadata_fn!(add_default, default, Option<Value>);

    pub fn add_examples<I: IntoIterator<Item = Value>>(schema: Schema, examples: I) -> Schema {
        let mut schema_obj = schema.into_object();
        schema_obj.metadata().examples.extend(examples);
        Schema::Object(schema_obj)
    }
}