Debian Codegraph @

tests/integration/structs.rs

raw symbols SCIP
1 use crate::prelude::*;
2
3 #[derive(JsonSchema, Deserialize, Serialize, Default)]
4 struct UnitStruct;
5
6 #[test]
7 fn unit() {
8 test!(UnitStruct)
9 .assert_snapshot()
10 .assert_allows_ser_roundtrip_default()
11 .assert_matches_de_roundtrip(arbitrary_values());
12 }
13
14 #[derive(JsonSchema, Deserialize, Serialize, Default)]
15 struct NormalStruct {
16 foo: String,
17 bar: bool,
18 }
19
20 #[test]
21 fn normal() {
22 test!(NormalStruct)
23 .assert_snapshot()
24 .assert_allows_ser_roundtrip_default()
25 .assert_allows_de_roundtrip([json!({
26 "foo": "",
27 "bar": true,
28 "unknown": 123
29 })])
30 .assert_matches_de_roundtrip(arbitrary_values());
31 }
32
33 #[derive(JsonSchema, Deserialize, Serialize, Default)]
34 struct NewType(String);
35
36 #[test]
37 fn newtype() {
38 test!(NewType)
39 .assert_snapshot()
40 .assert_allows_ser_roundtrip_default()
41 .assert_matches_de_roundtrip(arbitrary_values());
42 }
43
44 #[derive(JsonSchema, Deserialize, Serialize, Default)]
45 struct TupleStruct(String, bool);
46
47 #[test]
48 fn tuple() {
49 test!(TupleStruct)
50 .assert_snapshot()
51 .assert_allows_ser_roundtrip_default()
52 .assert_matches_de_roundtrip(arbitrary_values());
53 }
54
55 #[derive(JsonSchema, Deserialize, Serialize, Default)]
56 #[serde(rename_all = "camelCase")]
57 struct RenamedFields {
58 camel_case: i32,
59 #[serde(rename = "new_name")]
60 old_name: i32,
61 }
62
63 #[test]
64 fn renamed_fields() {
65 test!(RenamedFields)
66 .assert_snapshot()
67 .assert_allows_ser_roundtrip_default()
68 .assert_matches_de_roundtrip(arbitrary_values());
69 }
70
71 #[derive(JsonSchema, Deserialize, Serialize, Default)]
72 #[serde(deny_unknown_fields)]
73 struct DenyUnknownFields {
74 foo: String,
75 bar: bool,
76 }
77
78 #[test]
79 fn deny_unknown_fields() {
80 test!(DenyUnknownFields)
81 .assert_snapshot()
82 .assert_allows_ser_roundtrip_default()
83 .assert_rejects_de([json!({
84 "foo": "",
85 "bar": true,
86 "unknown": 123
87 })])
88 .assert_matches_de_roundtrip(arbitrary_values());
89 }
90
91 #[derive(JsonSchema, Deserialize, Serialize, Default)]
92 #[schemars(example = PropertyOrder::default(), extend("x-custom" = PropertyOrder::default()))]
93 struct PropertyOrder {
94 #[serde(rename = "$defs")]
95 pub defs: UnitStruct,
96 pub examples: UnitStruct,
97 pub properties: UnitStruct,
98 pub required: UnitStruct,
99 #[serde(rename = "$schema")]
100 pub schema: UnitStruct,
101 pub title: UnitStruct,
102 pub r#type: UnitStruct,
103 #[serde(rename = "x-custom")]
104 pub x_custom: UnitStruct,
105 }
106
107 #[cfg_attr(not(feature = "preserve_order"), ignore)]
108 #[test]
109 fn property_order() {
110 test!(PropertyOrder).assert_snapshot().custom(|schema, _| {
111 fn get_property_keys(value: &Value) -> Vec<&str> {
112 value
113 .as_object()
114 .expect("expected value to be an object")
115 .keys()
116 .map(String::as_str)
117 .collect()
118 }
119
120 let value = serde_json::to_value(schema).unwrap();
121
122 assert!(matches!(
123 get_property_keys(&value).as_slice(),
124 // order of `examples`, `required` and `x-custom` is unspecified
125 &["$schema", "title", "type", "properties", .., "$defs"]
126 ));
127
128 let field_order = &[
129 "$defs",
130 "examples",
131 "properties",
132 "required",
133 "$schema",
134 "title",
135 "type",
136 "x-custom",
137 ];
138
139 assert_eq!(
140 get_property_keys(&value["properties"]).as_slice(),
141 field_order,
142 );
143 assert_eq!(
144 get_property_keys(&value["examples"][0]).as_slice(),
145 field_order,
146 );
147 assert_eq!(
148 get_property_keys(&value["x-custom"]).as_slice(),
149 field_order,
150 );
151 });
152 }