Debian Codegraph @

tests/integration/enums_ref_variants.rs

raw symbols SCIP
1 use crate::prelude::*;
2 use std::collections::BTreeMap;
3
4 #[derive(JsonSchema, Deserialize, Serialize)]
5 struct UnitStruct;
6
7 #[derive(JsonSchema, Deserialize, Serialize, Default)]
8 struct Struct {
9 foo: i32,
10 bar: bool,
11 }
12
13 #[derive(JsonSchema, Deserialize, Serialize)]
14 #[serde(rename_all = "camelCase")]
15 #[schemars(_unstable_ref_variants)]
16 enum External {
17 UnitOne,
18 StringMap(BTreeMap<String, String>),
19 UnitStructNewType(UnitStruct),
20 StructNewType(Struct),
21 Struct {
22 foo: i32,
23 bar: bool,
24 },
25 Tuple(i32, bool),
26 UnitTwo,
27 #[serde(with = "unit_variant_as_u64")]
28 #[schemars(with = "u64")]
29 UnitAsInt,
30 #[serde(with = "tuple_variant_as_str")]
31 #[schemars(schema_with = "tuple_variant_as_str::json_schema")]
32 TupleAsStr(i32, bool),
33 }
34
35 impl External {
36 fn values() -> impl IntoIterator<Item = Self> {
37 [
38 Self::UnitOne,
39 Self::StringMap(
40 [("hello".to_owned(), "world".to_owned())]
41 .into_iter()
42 .collect(),
43 ),
44 Self::UnitStructNewType(UnitStruct),
45 Self::StructNewType(Struct {
46 foo: 123,
47 bar: true,
48 }),
49 Self::Struct {
50 foo: 123,
51 bar: true,
52 },
53 Self::Tuple(456, false),
54 Self::UnitTwo,
55 Self::UnitAsInt,
56 Self::TupleAsStr(789, true),
57 ]
58 }
59 }
60
61 #[derive(JsonSchema, Deserialize, Serialize)]
62 #[serde(tag = "tag")]
63 #[schemars(_unstable_ref_variants)]
64 enum Internal {
65 UnitOne,
66 StringMap(BTreeMap<String, String>),
67 UnitStructNewType(UnitStruct),
68 StructNewType(Struct),
69 Struct { foo: i32, bar: bool },
70 // Internally-tagged enums don't support tuple variants
71 // Tuple(i32, bool),
72 UnitTwo,
73 // Internally-tagged enum variants don't support non-object "payloads"
74 // #[serde(with = "unit_variant_as_u64")]
75 // #[schemars(with = "u64")]
76 // UnitAsInt,
77 // Internally-tagged enums don't support tuple variants
78 // #[serde(with = "tuple_variant_as_str")]
79 // #[schemars(schema_with = "tuple_variant_as_str::json_schema")]
80 // TupleAsStr(i32, bool),
81 }
82
83 impl Internal {
84 fn values() -> impl IntoIterator<Item = Self> {
85 [
86 Self::UnitOne,
87 Self::StringMap(
88 [("hello".to_owned(), "world".to_owned())]
89 .into_iter()
90 .collect(),
91 ),
92 Self::UnitStructNewType(UnitStruct),
93 Self::StructNewType(Struct {
94 foo: 123,
95 bar: true,
96 }),
97 Self::Struct {
98 foo: 123,
99 bar: true,
100 },
101 // Self::Tuple(456, false),
102 Self::UnitTwo,
103 // Self::UnitAsInt,
104 // Self::TupleAsStr(789, true),
105 ]
106 }
107 }
108
109 #[derive(JsonSchema, Deserialize, Serialize)]
110 #[serde(tag = "tag", content = "content")]
111 #[schemars(_unstable_ref_variants)]
112 enum Adjacent {
113 UnitOne,
114 StringMap(BTreeMap<String, String>),
115 UnitStructNewType(UnitStruct),
116 StructNewType(Struct),
117 Struct {
118 foo: i32,
119 bar: bool,
120 },
121 Tuple(i32, bool),
122 UnitTwo,
123 #[serde(with = "unit_variant_as_u64")]
124 #[schemars(with = "u64")]
125 UnitAsInt,
126 #[serde(with = "tuple_variant_as_str")]
127 #[schemars(schema_with = "tuple_variant_as_str::json_schema")]
128 TupleAsStr(i32, bool),
129 }
130
131 impl Adjacent {
132 fn values() -> impl IntoIterator<Item = Self> {
133 [
134 Self::UnitOne,
135 Self::StringMap(
136 [("hello".to_owned(), "world".to_owned())]
137 .into_iter()
138 .collect(),
139 ),
140 Self::UnitStructNewType(UnitStruct),
141 Self::StructNewType(Struct {
142 foo: 123,
143 bar: true,
144 }),
145 Self::Struct {
146 foo: 123,
147 bar: true,
148 },
149 Self::Tuple(456, false),
150 Self::UnitTwo,
151 Self::UnitAsInt,
152 Self::TupleAsStr(789, true),
153 ]
154 }
155 }
156
157 #[derive(JsonSchema, Deserialize, Serialize)]
158 #[serde(untagged)]
159 #[schemars(_unstable_ref_variants)]
160 enum Untagged {
161 UnitOne,
162 StringMap(BTreeMap<String, String>),
163 UnitStructNewType(UnitStruct),
164 StructNewType(Struct),
165 Struct {
166 foo: i32,
167 bar: bool,
168 },
169 Tuple(i32, bool),
170 UnitTwo,
171 #[serde(with = "unit_variant_as_u64")]
172 #[schemars(with = "u64")]
173 UnitAsInt,
174 #[serde(with = "tuple_variant_as_str")]
175 #[schemars(schema_with = "tuple_variant_as_str::json_schema")]
176 TupleAsStr(i32, bool),
177 }
178
179 impl Untagged {
180 fn values() -> impl IntoIterator<Item = Self> {
181 [
182 Self::UnitOne,
183 Self::StringMap(
184 [("hello".to_owned(), "world".to_owned())]
185 .into_iter()
186 .collect(),
187 ),
188 Self::UnitStructNewType(UnitStruct),
189 Self::StructNewType(Struct {
190 foo: 123,
191 bar: true,
192 }),
193 Self::Struct {
194 foo: 123,
195 bar: true,
196 },
197 Self::Tuple(456, false),
198 Self::UnitTwo,
199 Self::UnitAsInt,
200 Self::TupleAsStr(789, true),
201 ]
202 }
203 }
204
205 mod unit_variant_as_u64 {
206 pub(super) fn serialize<S>(ser: S) -> Result<S::Ok, S::Error>
207 where
208 S: serde::Serializer,
209 {
210 ser.serialize_u64(42)
211 }
212
213 pub(super) fn deserialize<'de, D>(deser: D) -> Result<(), D::Error>
214 where
215 D: serde::Deserializer<'de>,
216 {
217 use serde::de::Deserialize;
218
219 u64::deserialize(deser).map(|_| ())
220 }
221 }
222
223 mod tuple_variant_as_str {
224 pub(super) fn serialize<S>(i: &i32, b: &bool, ser: S) -> Result<S::Ok, S::Error>
225 where
226 S: serde::Serializer,
227 {
228 ser.collect_str(&format_args!("{i} {b}"))
229 }
230
231 pub(super) fn deserialize<'de, D>(deser: D) -> Result<(i32, bool), D::Error>
232 where
233 D: serde::Deserializer<'de>,
234 {
235 use serde::de::{Deserialize, Error};
236 let error = || Error::custom("invalid string");
237
238 let (i, b) = <&str>::deserialize(deser)?
239 .split_once(' ')
240 .ok_or_else(error)?;
241
242 Ok((
243 i.parse().map_err(|_| error())?,
244 b.parse().map_err(|_| error())?,
245 ))
246 }
247
248 pub(super) fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
249 schemars::json_schema!({
250 "type": "string",
251 "pattern": r"^\d+ (true|false)$"
252 })
253 }
254 }
255
256 #[test]
257 fn externally_tagged_enum() {
258 test!(External)
259 .assert_snapshot()
260 .assert_allows_ser_roundtrip(External::values())
261 .assert_matches_de_roundtrip(arbitrary_values());
262 }
263
264 #[test]
265 fn internally_tagged_enum() {
266 test!(Internal)
267 .assert_snapshot()
268 .assert_allows_ser_roundtrip(Internal::values())
269 .assert_matches_de_roundtrip(arbitrary_values());
270 }
271
272 #[test]
273 fn adjacently_tagged_enum() {
274 test!(Adjacent)
275 .assert_snapshot()
276 .assert_allows_ser_roundtrip(Adjacent::values())
277 .assert_matches_de_roundtrip(arbitrary_values());
278 }
279
280 #[test]
281 fn untagged_enum() {
282 test!(Untagged)
283 .assert_snapshot()
284 .assert_allows_ser_roundtrip(Untagged::values())
285 .assert_matches_de_roundtrip(arbitrary_values());
286 }
287
288 #[derive(JsonSchema, Serialize, Deserialize)]
289 #[schemars(_unstable_ref_variants)]
290 enum NoVariants {}
291
292 #[test]
293 fn no_variants() {
294 test!(NoVariants)
295 .assert_snapshot()
296 .assert_rejects_de(arbitrary_values());
297 }
298
299 #[derive(JsonSchema, Serialize, Deserialize)]
300 #[serde(rename_all_fields = "UPPERCASE", rename_all = "snake_case")]
301 #[schemars(_unstable_ref_variants)]
302 enum Renamed {
303 StructVariant {
304 field: String,
305 },
306 #[serde(rename = "custom name variant")]
307 RenamedStructVariant {
308 #[serde(rename = "custom name field")]
309 field: String,
310 },
311 }
312
313 #[test]
314 fn renamed() {
315 test!(Renamed)
316 .assert_snapshot()
317 .assert_allows_ser_roundtrip([
318 Renamed::StructVariant {
319 field: "foo".to_owned(),
320 },
321 Renamed::RenamedStructVariant {
322 field: "bar".to_owned(),
323 },
324 ])
325 .assert_rejects_de(arbitrary_values());
326 }