Debian Codegraph @

tests/integration/enums_flattened.rs

raw symbols SCIP
1 use crate::prelude::*;
2 use schemars::generate::SchemaSettings;
3
4 #[derive(JsonSchema, Deserialize, Serialize)]
5 struct EmptyStruct {}
6
7 #[derive(JsonSchema, Deserialize, Serialize)]
8 enum External1 {
9 Unit1,
10 Unit2,
11 }
12
13 #[derive(JsonSchema, Deserialize, Serialize)]
14 enum External2 {
15 Unit3,
16 ValueNewType(Value),
17 }
18
19 #[derive(JsonSchema, Deserialize, Serialize)]
20 enum External3 {
21 Int(u32),
22 Tuple(u8, bool),
23 }
24
25 #[derive(JsonSchema, Deserialize, Serialize)]
26 enum External4 {
27 StructNewType(EmptyStruct),
28 Struct { foo: i32, bar: bool },
29 }
30
31 #[derive(JsonSchema, Deserialize, Serialize)]
32 struct ExternalContainer {
33 f: f32,
34 #[serde(flatten)]
35 e1: External1,
36 #[serde(flatten)]
37 e2: External2,
38 #[serde(flatten)]
39 e3: External3,
40 #[serde(flatten)]
41 e4: External4,
42 }
43
44 impl ExternalContainer {
45 fn values() -> impl IntoIterator<Item = Self> {
46 [
47 Self {
48 f: 1.23,
49 e1: External1::Unit1,
50 e2: External2::Unit3,
51 e3: External3::Int(123),
52 e4: External4::StructNewType(EmptyStruct {}),
53 },
54 Self {
55 f: 9.87,
56 e1: External1::Unit2,
57 e2: External2::ValueNewType(json!({"key": "value"})),
58 e3: External3::Tuple(0, true),
59 e4: External4::Struct { foo: 1, bar: true },
60 },
61 ]
62 }
63 }
64
65 #[derive(JsonSchema, Deserialize, Serialize)]
66 #[serde(deny_unknown_fields)]
67 struct ExternalContainerDenyUnknownFields {
68 f: f32,
69 #[serde(flatten)]
70 e1: External1,
71 #[serde(flatten)]
72 e2: External2,
73 #[serde(flatten)]
74 e3: External3,
75 #[serde(flatten)]
76 e4: External4,
77 }
78
79 impl ExternalContainerDenyUnknownFields {
80 fn values() -> impl IntoIterator<Item = Self> {
81 [
82 Self {
83 f: 1.23,
84 e1: External1::Unit1,
85 e2: External2::Unit3,
86 e3: External3::Int(123),
87 e4: External4::StructNewType(EmptyStruct {}),
88 },
89 Self {
90 f: 9.87,
91 e1: External1::Unit2,
92 e2: External2::ValueNewType(json!({"key": "value"})),
93 e3: External3::Tuple(0, true),
94 e4: External4::Struct { foo: 1, bar: true },
95 },
96 ]
97 }
98 }
99
100 fn external_container_json_with_extra_field() -> Value {
101 json!({
102 "f": 1.23,
103 "Unit1": null,
104 "Unit3": null,
105 "Int": 123,
106 "StructNewType": {},
107 "extra": null
108 })
109 }
110
111 #[test]
112 fn external_enums_flattened() {
113 test!(ExternalContainer)
114 .assert_snapshot()
115 .assert_allows_ser_roundtrip(ExternalContainer::values())
116 .assert_matches_de_roundtrip(arbitrary_values())
117 .assert_allows_de_roundtrip([external_container_json_with_extra_field()]);
118 }
119
120 #[test]
121 fn external_enums_flattened_deny_unknown_fields() {
122 test!(ExternalContainerDenyUnknownFields)
123 .assert_snapshot()
124 .assert_allows_ser_roundtrip(ExternalContainerDenyUnknownFields::values())
125 .assert_matches_de_roundtrip(arbitrary_values())
126 .assert_rejects_de([external_container_json_with_extra_field()]);
127 }
128
129 #[test]
130 fn external_enums_flattened_deny_unknown_fields_draft07() {
131 test!(
132 ExternalContainerDenyUnknownFields,
133 SchemaSettings::draft07()
134 )
135 .assert_snapshot()
136 .assert_allows_ser_roundtrip(ExternalContainerDenyUnknownFields::values())
137 .assert_matches_de_roundtrip(arbitrary_values())
138 .assert_rejects_de([external_container_json_with_extra_field()]);
139 }
140
141 #[derive(JsonSchema, Deserialize, Serialize)]
142 #[serde(tag = "tag1")]
143 enum Internal1 {
144 Unit1,
145 Unit2,
146 }
147
148 #[derive(JsonSchema, Deserialize, Serialize)]
149 #[serde(tag = "tag2")]
150 enum Internal2 {
151 Unit3,
152 ValueNewType(Value),
153 }
154
155 #[derive(JsonSchema, Deserialize, Serialize)]
156 #[serde(tag = "tag3")]
157 enum Internal3 {
158 StructNewType(EmptyStruct),
159 Struct { foo: i32, bar: bool },
160 }
161
162 #[derive(JsonSchema, Deserialize, Serialize)]
163 struct InternalContainer {
164 f: f32,
165 #[serde(flatten)]
166 e1: Internal1,
167 #[serde(flatten)]
168 e2: Internal2,
169 #[serde(flatten)]
170 e3: Internal3,
171 }
172
173 impl InternalContainer {
174 fn values() -> impl IntoIterator<Item = Self> {
175 [
176 Self {
177 f: 1.23,
178 e1: Internal1::Unit1,
179 e2: Internal2::Unit3,
180 e3: Internal3::StructNewType(EmptyStruct {}),
181 },
182 Self {
183 f: 9.87,
184 e1: Internal1::Unit2,
185 e2: Internal2::ValueNewType(json!({"key": "value"})),
186 e3: Internal3::Struct { foo: 1, bar: true },
187 },
188 ]
189 }
190 }
191
192 #[test]
193 fn internal_enums_flattened() {
194 test!(InternalContainer)
195 .assert_snapshot()
196 .assert_allows_ser_roundtrip(InternalContainer::values())
197 .assert_matches_de_roundtrip(arbitrary_values());
198 }
199
200 #[derive(JsonSchema, Deserialize, Serialize)]
201 #[serde(tag = "tag1", content = "content1")]
202 enum Adjacent1 {
203 Unit1,
204 Unit2,
205 }
206
207 #[derive(JsonSchema, Deserialize, Serialize)]
208 #[serde(tag = "tag2", content = "content2")]
209 enum Adjacent2 {
210 Unit3,
211 ValueNewType(Value),
212 }
213
214 #[derive(JsonSchema, Deserialize, Serialize)]
215 #[serde(tag = "tag3", content = "content3")]
216 enum Adjacent3 {
217 Int(u32),
218 Tuple(u8, bool),
219 }
220
221 #[derive(JsonSchema, Deserialize, Serialize)]
222 #[serde(tag = "tag4", content = "content4")]
223 enum Adjacent4 {
224 StructNewType(EmptyStruct),
225 Struct { foo: i32, bar: bool },
226 }
227
228 #[derive(JsonSchema, Deserialize, Serialize)]
229 struct AdjacentContainer {
230 f: f32,
231 #[serde(flatten)]
232 e1: Adjacent1,
233 #[serde(flatten)]
234 e2: Adjacent2,
235 #[serde(flatten)]
236 e3: Adjacent3,
237 #[serde(flatten)]
238 e4: Adjacent4,
239 }
240
241 impl AdjacentContainer {
242 fn values() -> impl IntoIterator<Item = Self> {
243 [
244 Self {
245 f: 1.23,
246 e1: Adjacent1::Unit1,
247 e2: Adjacent2::Unit3,
248 e3: Adjacent3::Int(123),
249 e4: Adjacent4::StructNewType(EmptyStruct {}),
250 },
251 Self {
252 f: 9.87,
253 e1: Adjacent1::Unit2,
254 e2: Adjacent2::ValueNewType(json!({"key": "value"})),
255 e3: Adjacent3::Tuple(0, true),
256 e4: Adjacent4::Struct { foo: 1, bar: true },
257 },
258 ]
259 }
260 }
261
262 #[test]
263 fn adjacent_enums_flattened() {
264 test!(AdjacentContainer)
265 .assert_snapshot()
266 .assert_allows_ser_roundtrip(AdjacentContainer::values())
267 .assert_matches_de_roundtrip(arbitrary_values());
268 }
269
270 #[derive(JsonSchema, Deserialize, Serialize)]
271 #[serde(untagged)]
272 enum Untagged {
273 Struct1 { foo: i32 },
274 Struct2 { bar: bool },
275 ValueNewType(Value),
276 }
277
278 #[derive(JsonSchema, Deserialize, Serialize)]
279 struct UntaggedContainer {
280 f: f32,
281 #[serde(flatten)]
282 e1: Untagged,
283 }
284
285 impl UntaggedContainer {
286 fn values() -> impl IntoIterator<Item = Self> {
287 [
288 Self {
289 f: 1.23,
290 e1: Untagged::Struct1 { foo: 1 },
291 },
292 Self {
293 f: 9.87,
294 e1: Untagged::Struct2 { bar: true },
295 },
296 Self {
297 f: 42.0,
298 e1: Untagged::ValueNewType(json!({"key": "value"})),
299 },
300 ]
301 }
302 }
303
304 #[test]
305 fn untagged_enums_flattened() {
306 test!(UntaggedContainer)
307 .assert_snapshot()
308 .assert_allows_ser_roundtrip(UntaggedContainer::values())
309 .assert_matches_de_roundtrip(arbitrary_values());
310 }
311
312 #[derive(JsonSchema, Deserialize, Serialize)]
313 struct MixedContainer {
314 f: f32,
315 #[serde(flatten)]
316 e1: External1,
317 #[serde(flatten)]
318 i2: Internal2,
319 #[serde(flatten)]
320 a3: Adjacent3,
321 #[serde(flatten)]
322 u: Untagged,
323 }
324
325 impl MixedContainer {
326 fn values() -> impl IntoIterator<Item = Self> {
327 [
328 Self {
329 f: 1.23,
330 e1: External1::Unit1,
331 i2: Internal2::Unit3,
332 a3: Adjacent3::Int(123),
333 u: Untagged::Struct1 { foo: 1 },
334 },
335 Self {
336 f: 9.87,
337 e1: External1::Unit2,
338 i2: Internal2::ValueNewType(json!({"key": "value"})),
339 a3: Adjacent3::Tuple(0, true),
340 u: Untagged::ValueNewType(json!({"key": "value"})),
341 },
342 ]
343 }
344 }
345
346 #[test]
347 fn mixed_enums_flattened() {
348 test!(MixedContainer)
349 .assert_snapshot()
350 .assert_allows_ser_roundtrip(MixedContainer::values())
351 .assert_matches_de_roundtrip(arbitrary_values());
352 }