Debian Codegraph @

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