Debian Codegraph @

tests/integration/settings.rs

raw symbols SCIP
1 use crate::prelude::*;
2 use pretty_assertions::assert_eq;
3 use schemars::{generate::SchemaSettings, Schema};
4
5 #[derive(JsonSchema, Deserialize, Serialize, Default)]
6 #[schemars(inline)]
7 pub struct OuterStruct {
8 #[schemars(extend("examples" = [8, null]))]
9 maybe_int: Option<i32>,
10 values: serde_json::Map<String, Value>,
11 value: Value,
12 inner: InnerEnum,
13 maybe_inner: Option<InnerEnum>,
14 tuples: Vec<(u8, i64)>,
15 }
16
17 #[derive(JsonSchema, Deserialize, Serialize, Default)]
18 pub enum InnerEnum {
19 #[default]
20 UndocumentedUnit1,
21 UndocumentedUnit2,
22 /// This is a documented unit variant
23 DocumentedUnit,
24 ValueNewType(Value),
25 }
26
27 #[test]
28 fn draft07() {
29 test!(OuterStruct, SchemaSettings::draft07())
30 .assert_snapshot()
31 .assert_allows_ser_roundtrip_default()
32 .assert_matches_de_roundtrip(arbitrary_values());
33 }
34
35 #[test]
36 fn draft2019_09() {
37 test!(OuterStruct, SchemaSettings::draft2019_09())
38 .assert_snapshot()
39 .assert_allows_ser_roundtrip_default()
40 .assert_matches_de_roundtrip(arbitrary_values());
41 }
42
43 #[test]
44 fn draft2020_12() {
45 test!(OuterStruct, SchemaSettings::draft2020_12())
46 .assert_snapshot()
47 .assert_allows_ser_roundtrip_default()
48 .assert_matches_de_roundtrip(arbitrary_values());
49 }
50
51 #[test]
52 fn openapi3() {
53 let mut settings = SchemaSettings::openapi3();
54 // Hack to apply recursive transforms to schemas at components.schemas:
55 // First, move them to $defs, then run the transforms, then move them back again.
56 settings.transforms.insert(
57 0,
58 Box::new(|s: &mut Schema| {
59 let defs = s.pointer_mut("/components/schemas").unwrap().take();
60 s.insert("$defs".to_owned(), defs);
61 }),
62 );
63 settings.transforms.push(Box::new(|s: &mut Schema| {
64 *s.pointer_mut("/components/schemas").unwrap() = s.remove("$defs").unwrap();
65 }));
66
67 test!(OuterStruct, settings.clone()).assert_snapshot();
68
69 // Ensure that `take_definitions()` applies transforms correctly
70
71 let gen1 = settings.into_generator();
72 let definitions1 =
73 &Value::from(gen1.into_root_schema_for::<OuterStruct>())["components"]["schemas"];
74
75 let mut gen2 = SchemaSettings::openapi3().into_generator();
76 gen2.subschema_for::<OuterStruct>();
77 let definitions2 = Value::Object(gen2.take_definitions(true));
78
79 assert_eq!(definitions1, &definitions2);
80 }