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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! A module containing default values filler.
use serde_json::{json, Value};

use crate::schema::{PrimitiveType, Schema};

// Recursively check if the object is empty
//
// If the child object is empty, parent object is considered as empty.
fn is_empty_object(value: &Value) -> bool {
    match value {
        Value::Object(map) => map.values().all(|x| is_empty_object(x)),
        _ => false,
    }
}

fn fill_object_defaults(schema: &Schema, data: &mut Value, include_optional: bool) {
    if data.is_null() {
        std::mem::replace(data, json!({}));
    }

    if let Some(data) = data.as_object_mut() {
        for property in schema.properties() {
            let name = property.name();

            if let Some(mut value) = data.get_mut(name) {
                fill_defaults(property.schema(), &mut value, include_optional);
            } else {
                // Fill defaults, but if the resulting object is empty, do not include it
                let mut value = Value::Null;
                fill_defaults(property.schema(), &mut value, include_optional);
                if !value.is_null() && !is_empty_object(&value) {
                    data.insert(name.to_string(), value);
                }
            }
        }
    }
}

fn fill_array_defaults(schema: &Schema, data: &mut Value, include_optional: bool) {
    if data.is_array() && schema.items().len() == 1 {
        // What we should do in case of multiple schemas? Partial object match?
        let schema = schema.items().first().unwrap();

        for mut item in data.as_array_mut().unwrap() {
            fill_defaults(&schema, &mut item, include_optional);
        }
    }
}

fn fill_primitive_defaults(schema: &Schema, data: &mut Value, include_optional: bool) {
    let required = schema.r#type().is_required();

    if let Some(default_value) = schema.r#default() {
        if data.is_null() && (include_optional || required) {
            std::mem::replace(data, default_value.clone());
        }
    }
}

fn fill_defaults(schema: &Schema, data: &mut Value, include_optional: bool) {
    match (schema.r#type().primitive_type(), schema.r#type().is_required()) {
        (PrimitiveType::Object, _) => fill_object_defaults(schema, data, include_optional),
        (PrimitiveType::Array, _) => fill_array_defaults(schema, data, include_optional),
        _ => fill_primitive_defaults(schema, data, include_optional),
    };
}

/// Fill default values from the schema
///
/// # Arguments
///
/// * `schema` - JellySchema
/// * `data` - JSON value to start with
/// * `include_optional` - if `false` only required properties are filled
pub fn fill_default_values(schema: &Schema, data: &mut Value, include_optional: bool) {
    fill_defaults(schema, data, include_optional);
    if data.is_null() {
        match schema.r#type().primitive_type() {
            PrimitiveType::Object => {
                std::mem::replace(data, json!({}));
            }
            PrimitiveType::Array => {
                std::mem::replace(data, json!([]));
            }
            _ => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn fill_all(schema: &str, input: Value) -> Value {
        let mut input = input;
        fill_default_values(&schema.parse::<Schema>().unwrap(), &mut input, true);
        input
    }

    fn fill_required(schema: &str, input: Value) -> Value {
        let mut input = input;
        fill_default_values(&schema.parse::<Schema>().unwrap(), &mut input, false);
        input
    }

    #[test]
    fn ensure_root_object_is_created() {
        let schema = r##"
            properties:
                - foo:
                    type: string
        "##;
        let input = Value::Null;
        let result = json!({});
        assert_eq!(fill_required(schema, input), result);
    }

    #[test]
    fn ensure_root_array_is_created() {
        let schema = r##"
            type: array
            items:
                type: string
        "##;
        let input = Value::Null;
        let result = json!([]);
        assert_eq!(fill_required(schema, input), result);
    }

    #[test]
    fn fill_nested_required() {
        let schema = r##"
            properties:
                - foo:
                    properties:
                        - bar:
                            type: string
                            default: baz
        "##;
        let input = Value::Null;
        let result = json!({"foo": {"bar": "baz"}});
        assert_eq!(fill_required(schema, input), result);
    }

    #[test]
    fn ignore_nested_optional() {
        let schema = r##"
            properties:
                - foo:
                    properties:
                        - bar:
                            type: string?
                            default: baz
        "##;
        let input = Value::Null;
        let result = json!({});
        assert_eq!(fill_required(schema, input), result);
    }

    #[test]
    fn fill_nested_optional() {
        let schema = r##"
            properties:
                - foo:
                    properties:
                        - bar:
                            type: string?
                            default: baz
        "##;
        let input = Value::Null;
        let result = json!({"foo": {"bar": "baz"}});
        assert_eq!(fill_all(schema, input), result);
    }

    #[test]
    fn do_not_remove_empty_nested_objects() {
        let schema = r##"
            properties:
                - foo:
                    properties:
                        - bar:
                            type: string?
                            default: baz
        "##;
        let input = json!({"foo": {}});
        let result = json!({"foo": {}});
        assert_eq!(fill_required(schema, input), result);
    }

    #[test]
    fn fill_required_array_item_properties() {
        let schema = r##"
            type: array
            items:
                properties:
                    - foo:
                        type: string
                        default: bar
        "##;
        let input = json!([{}, {"foo": "baz"}, {"bar": "baz"}]);
        let result = json!([{"foo": "bar"}, {"foo": "baz"}, {"foo": "bar", "bar": "baz"}]);
        assert_eq!(fill_required(schema, input), result);
    }

    #[test]
    fn fill_required_root_string() {
        let schema = r##"
            type: string
            default: foo
        "##;
        let input = json!(null);
        let result = json!("foo");
        assert_eq!(fill_required(schema, input), result);
    }

    #[test]
    fn object_emptiness() {
        assert!(!is_empty_object(&json!("foo")));
        assert!(!is_empty_object(&json!(123)));
        assert!(!is_empty_object(&json!(true)));
        assert!(!is_empty_object(&json!(false)));
        assert!(is_empty_object(&json!({})));
        assert!(!is_empty_object(&json!([])));
        assert!(!is_empty_object(&json!({"foo": {"bar": "baz"}})));
        assert!(!is_empty_object(&json!({"foo": "bar"})));
        assert!(is_empty_object(&json!({"foo": {"bar": {}}})));
    }
}