navree-openapi 1.4.0

Navree's OpenAPI 3.1 document model and the ToSchema trait and derive that describe Rust types.
Documentation
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! [`ToSchema`] and its implementations for the types of the standard library,
//! chrono, uuid and serde_json.

use std::borrow::{Cow, ToOwned};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::rc::Rc;
use std::sync::Arc;

use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeZone};
use serde_json::{Value, json};
use uuid::Uuid;

use crate::{Schema, Type};

/// A type described by a JSON Schema: `#[derive(ToSchema)]`, or by hand.
///
/// The derive reads the type's serde attributes, so the schema matches the JSON
/// serde reads and writes, and `#[schema(..)]` for what serde doesn't say.
/// Written by hand, [`schema`](ToSchema::schema) is the only method a type
/// needs:
///
/// ```
/// use navree_openapi::{Schema, ToSchema};
///
/// struct Zone(String);
///
/// impl ToSchema for Zone {
///     fn schema() -> Schema {
///         Schema::string().description("An IANA time zone name.")
///     }
/// }
///
/// assert_eq!(Zone::name(), "Zone");
/// ```
pub trait ToSchema {
    /// The name of the type's component schema: the last segment of its Rust
    /// path unless `#[schema(as = Name)]` says otherwise, followed by the names
    /// of its generic arguments (`Page<Note>` is `Page_Note`).
    fn name() -> Cow<'static, str> {
        let name = std::any::type_name::<Self>();
        let name = name.split('<').next().unwrap_or(name);
        Cow::Borrowed(name.rsplit("::").next().unwrap_or(name))
    }

    /// The type's schema.
    fn schema() -> Schema;

    /// What a field or an element of this type says: a `$ref` to the component,
    /// or for a type with no component of its own (`String`, `Vec<T>`) its
    /// schema.
    fn reference() -> Schema {
        Schema::reference_to(&Self::name())
    }

    /// Adds the component schemas [`schema`](ToSchema::schema) refers to.
    fn schemas(out: &mut Vec<(String, Schema)>) {
        let _ = out;
    }

    /// Adds the component schemas [`reference`](ToSchema::reference) refers to:
    /// this type's and what it refers to. A type already in `out` isn't walked
    /// again, so a type that contains itself ends. Another type of the same name
    /// is walked as well, even with the same schema, since what its `$ref`s mean
    /// may differ: a different schema of one name goes in beside the first, for
    /// the document to refuse.
    fn components(out: &mut Vec<(String, Schema)>) {
        let name = Self::name();
        let rust_type = std::any::type_name::<Self>();
        let schema = Schema {
            rust_type: crate::schema::RustType(Some(rust_type)),
            ..Self::schema()
        };
        let walked = out
            .iter()
            .any(|(existing, other)| *existing == name && other.rust_type.0 == Some(rust_type));
        if walked {
            let same = out
                .iter()
                .any(|(existing, other)| *existing == name && *other == schema);
            if !same {
                out.push((name.into_owned(), schema));
            }
            return;
        }
        out.push((name.into_owned(), schema));
        Self::schemas(out);
    }
}

/// The component schemas `T` refers to, one for each name: what a schema that
/// stands alone puts under `$defs`.
///
/// # Panics
///
/// When two different schemas have one name, since a `$ref` could only mean one
/// of them and a tool's caller or a model would be told the wrong shape. The
/// message names both types; `#[schema(as = Name)]` renames one.
///
/// ```
/// use navree_openapi::{ToSchema, components_of};
///
/// #[derive(ToSchema)]
/// #[allow(dead_code)]
/// struct Stage {
///     name: String,
/// }
///
/// #[derive(ToSchema)]
/// #[allow(dead_code)]
/// struct Deal {
///     stage: Stage,
/// }
///
/// assert!(components_of::<Deal>().contains_key("Stage"));
/// ```
pub fn components_of<T: ToSchema + ?Sized>() -> BTreeMap<String, Schema> {
    let mut referenced = Vec::new();
    T::schemas(&mut referenced);
    let mut components: BTreeMap<String, Schema> = BTreeMap::new();
    for (name, schema) in referenced {
        match components.get(&name) {
            Some(first) if *first == schema => {}
            Some(first) => panic!(
                "`{name}` names two different schemas under `{}`: `{}` and `{}`; give one of them another name with #[schema(as = Name)]",
                std::any::type_name::<T>(),
                first.rust_type.0.unwrap_or("a schema written by hand"),
                schema.rust_type.0.unwrap_or("a schema written by hand"),
            ),
            None => {
                components.insert(name, schema);
            }
        }
    }
    components
}

/// `#[schema(value_type = Object)]`: `{"type": "object"}`, whatever the field's
/// Rust type is.
#[derive(Clone, Copy, Debug)]
pub struct Object;

/// A type with no component: fields inline its schema.
macro_rules! inline_schema {
    ($(impl[$($generics:tt)*] $ty:ty => $schema:expr;)+) => {$(
        impl<$($generics)*> ToSchema for $ty {
            fn schema() -> Schema {
                $schema
            }

            fn reference() -> Schema {
                Self::schema()
            }

            fn components(_out: &mut Vec<(String, Schema)>) {}
        }
    )+};
}

fn integer(format: Option<&str>, unsigned: bool) -> Schema {
    Schema {
        format: format.map(str::to_owned),
        minimum: unsigned.then(|| json!(0)),
        ..Schema::of_type(Type::Integer)
    }
}

fn number(format: &str) -> Schema {
    Schema {
        format: Some(format.to_owned()),
        ..Schema::of_type(Type::Number)
    }
}

fn formatted(format: &str) -> Schema {
    Schema {
        format: Some(format.to_owned()),
        ..Schema::string()
    }
}

inline_schema! {
    impl[] String => Schema::string();
    impl[] str => Schema::string();
    impl[] char => Schema::string();
    impl[] bool => Schema::of_type(Type::Boolean);
    impl[] i8 => integer(Some("int32"), false);
    impl[] i16 => integer(Some("int32"), false);
    impl[] i32 => integer(Some("int32"), false);
    impl[] i64 => integer(Some("int64"), false);
    impl[] i128 => integer(None, false);
    impl[] isize => integer(None, false);
    impl[] u8 => integer(Some("int32"), true);
    impl[] u16 => integer(Some("int32"), true);
    impl[] u32 => integer(Some("int32"), true);
    impl[] u64 => integer(Some("int64"), true);
    impl[] u128 => integer(None, true);
    impl[] usize => integer(None, true);
    impl[] f32 => number("float");
    impl[] f64 => number("double");
    impl[] Uuid => formatted("uuid");
    impl[Tz: TimeZone] DateTime<Tz> => formatted("date-time");
    impl[] NaiveDateTime => formatted("date-time");
    impl[] NaiveDate => formatted("date");
    impl[] NaiveTime => Schema::string();
    impl[] Value => Schema::default();
    impl[] serde_json::Map<String, Value> => Schema::map(Schema::string(), Schema::default());
    impl[] Object => Schema::object();
}

impl ToSchema for () {
    fn name() -> Cow<'static, str> {
        Cow::Borrowed("TupleUnit")
    }

    fn schema() -> Schema {
        Schema::default().default_value(Value::Null)
    }

    fn reference() -> Schema {
        Self::schema()
    }

    fn components(_out: &mut Vec<(String, Schema)>) {}
}

/// A wrapper that serializes as `T`: its schema and what it refers to are `T`'s.
macro_rules! transparent {
    ($(impl[$($generics:tt)*] $ty:ty => $inner:ty;)+) => {$(
        impl<$($generics)*> ToSchema for $ty {
            fn name() -> Cow<'static, str> {
                <$inner>::name()
            }

            fn schema() -> Schema {
                <$inner>::schema()
            }

            fn reference() -> Schema {
                <$inner>::reference()
            }

            fn schemas(out: &mut Vec<(String, Schema)>) {
                <$inner>::schemas(out);
            }

            fn components(out: &mut Vec<(String, Schema)>) {
                <$inner>::components(out);
            }
        }
    )+};
}

transparent! {
    impl[T: ToSchema + ?Sized] Box<T> => T;
    impl[T: ToSchema + ?Sized] Rc<T> => T;
    impl[T: ToSchema + ?Sized] Arc<T> => T;
    impl['a, T: ToSchema + ToOwned + ?Sized] Cow<'a, T> => T;
}

/// A reference is the referent's.
macro_rules! reference {
    ($(impl[$($generics:tt)*] $ty:ty;)+) => {$(
        impl<$($generics)*> ToSchema for $ty {
            fn name() -> Cow<'static, str> {
                T::name()
            }

            fn schema() -> Schema {
                T::schema()
            }

            fn reference() -> Schema {
                T::reference()
            }

            fn schemas(out: &mut Vec<(String, Schema)>) {
                T::schemas(out);
            }

            fn components(out: &mut Vec<(String, Schema)>) {
                T::components(out);
            }
        }
    )+};
}

reference! {
    impl['a, T: ToSchema + ?Sized] &'a T;
    impl['a, T: ToSchema + ?Sized] &'a mut T;
}

/// `Option<T>` on its own is `{"oneOf": [{"type": "null"}, T's schema]}`; as a
/// field it's what `T` says, nullable.
impl<T: ToSchema> ToSchema for Option<T> {
    fn name() -> Cow<'static, str> {
        Cow::Owned(format!("Option_{}", T::name()))
    }

    fn schema() -> Schema {
        Schema::one_of(vec![Schema::null(), T::schema()])
    }

    fn reference() -> Schema {
        T::reference().nullable()
    }

    fn schemas(out: &mut Vec<(String, Schema)>) {
        T::schemas(out);
    }

    fn components(out: &mut Vec<(String, Schema)>) {
        T::components(out);
    }
}

/// A list: `{"type": "array", "items": ..}`, with `T`'s schema on its own and
/// what `T` says as a field.
macro_rules! list {
    ($(impl[$($generics:tt)*] $ty:ty => $unique:expr;)+) => {$(
        impl<$($generics)*> ToSchema for $ty {
            fn name() -> Cow<'static, str> {
                let list = if ($unique as Option<bool>).is_some() { "Set" } else { "Vec" };
                Cow::Owned(format!("{list}_{}", T::name()))
            }

            fn schema() -> Schema {
                Schema {
                    unique_items: $unique,
                    ..Schema::array(T::schema())
                }
            }

            fn reference() -> Schema {
                Schema {
                    unique_items: $unique,
                    ..Schema::array(T::reference())
                }
            }

            fn schemas(out: &mut Vec<(String, Schema)>) {
                T::schemas(out);
            }

            fn components(out: &mut Vec<(String, Schema)>) {
                T::components(out);
            }
        }
    )+};
}

list! {
    impl[T: ToSchema] Vec<T> => None;
    impl[T: ToSchema] [T] => None;
    impl[T: ToSchema, const N: usize] [T; N] => None;
    impl[T: ToSchema] std::collections::LinkedList<T> => None;
    impl[T: ToSchema] std::collections::VecDeque<T> => None;
    impl[T: ToSchema, S] HashSet<T, S> => Some(true);
    impl[T: ToSchema] BTreeSet<T> => Some(true);
}

/// A map: `{"type": "object", "additionalProperties": .., "propertyNames": ..}`,
/// with `V`'s schema on its own and what `V` says as a field.
macro_rules! map {
    ($(impl[$($generics:tt)*] $ty:ty;)+) => {$(
        impl<$($generics)*> ToSchema for $ty {
            fn name() -> Cow<'static, str> {
                Cow::Owned(format!("Map_{}_{}", K::name(), V::name()))
            }

            fn schema() -> Schema {
                Schema::map(K::schema(), V::schema())
            }

            fn reference() -> Schema {
                Schema::map(K::reference(), V::reference())
            }

            fn schemas(out: &mut Vec<(String, Schema)>) {
                K::schemas(out);
                V::schemas(out);
            }

            fn components(out: &mut Vec<(String, Schema)>) {
                K::components(out);
                V::components(out);
            }
        }
    )+};
}

map! {
    impl[K: ToSchema, V: ToSchema] BTreeMap<K, V>;
    impl[K: ToSchema, V: ToSchema, S] HashMap<K, V, S>;
    impl[K: ToSchema, V: ToSchema, S] indexmap::IndexMap<K, V, S>;
}

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

    use super::*;

    struct Deal;

    impl ToSchema for Deal {
        fn schema() -> Schema {
            Schema::object()
        }
    }

    fn value<T: ToSchema + ?Sized>() -> Value {
        T::reference().to_value()
    }

    #[test]
    fn primitives_are_inline() {
        assert_eq!(value::<String>(), json!({"type": "string"}));
        assert_eq!(value::<&'static str>(), json!({"type": "string"}));
        assert_eq!(value::<Cow<'static, str>>(), json!({"type": "string"}));
        assert_eq!(
            value::<u64>(),
            json!({"type": "integer", "format": "int64", "minimum": 0})
        );
        assert_eq!(
            value::<i32>(),
            json!({"type": "integer", "format": "int32"})
        );
        assert_eq!(value::<usize>(), json!({"type": "integer", "minimum": 0}));
        assert_eq!(
            value::<f64>(),
            json!({"type": "number", "format": "double"})
        );
        assert_eq!(value::<Uuid>(), json!({"type": "string", "format": "uuid"}));
        assert_eq!(
            value::<DateTime<chrono::Utc>>(),
            json!({"type": "string", "format": "date-time"})
        );
        assert_eq!(value::<Value>(), json!({}));
    }

    #[test]
    fn options_are_nullable() {
        assert_eq!(
            value::<Option<bool>>(),
            json!({"type": ["boolean", "null"]})
        );
        assert_eq!(
            value::<Option<Option<String>>>(),
            json!({"type": ["string", "null"]})
        );
        assert_eq!(
            value::<Option<Deal>>(),
            json!({"oneOf": [{"$ref": "#/components/schemas/Deal"}, {"type": "null"}]})
        );
        assert_eq!(value::<Option<Value>>(), json!({}));
    }

    #[test]
    fn lists_and_maps_compose() {
        assert_eq!(
            value::<Vec<Deal>>(),
            json!({"type": "array", "items": {"$ref": "#/components/schemas/Deal"}})
        );
        assert_eq!(
            value::<BTreeSet<String>>(),
            json!({"type": "array", "items": {"type": "string"}, "uniqueItems": true})
        );
        assert_eq!(
            value::<BTreeMap<String, Deal>>(),
            json!({
                "type": "object",
                "additionalProperties": {"$ref": "#/components/schemas/Deal"},
                "propertyNames": {"type": "string"},
            })
        );
    }

    #[test]
    fn components_reach_through_wrappers_and_stop_at_primitives() {
        let mut out = Vec::new();
        <Option<Vec<BTreeMap<String, Box<Deal>>>>>::components(&mut out);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].0, "Deal");
        let mut out = Vec::new();
        <Vec<String>>::components(&mut out);
        assert!(out.is_empty());
    }

    #[test]
    fn a_type_containing_itself_ends() {
        struct Tree;
        impl ToSchema for Tree {
            fn schema() -> Schema {
                Schema::object().property("children", <Vec<Tree>>::reference(), true)
            }
            fn schemas(out: &mut Vec<(String, Schema)>) {
                <Vec<Tree>>::components(out);
            }
        }
        let mut out = Vec::new();
        Tree::components(&mut out);
        assert_eq!(out.len(), 1);
    }
}