| 1 | use crate::prelude::*; |
| 2 | use pretty_assertions::assert_eq; |
| 3 | use schemars::generate::SchemaSettings; |
| 4 | use std::collections::BTreeMap; |
| 5 | |
| 6 | #[derive(JsonSchema, Deserialize, Serialize)] |
| 7 | struct UnitStruct; |
| 8 | |
| 9 | #[derive(JsonSchema, Deserialize, Serialize, Default)] |
| 10 | struct Struct { |
| 11 | foo: i32, |
| 12 | bar: bool, |
| 13 | } |
| 14 | |
| 15 | #[derive(JsonSchema, Deserialize, Serialize)] |
| 16 | #[serde(rename_all = "camelCase")] |
| 17 | enum External { |
| 18 | UnitOne, |
| 19 | StringMap(BTreeMap<String, String>), |
| 20 | UnitStructNewType(UnitStruct), |
| 21 | StructNewType(Struct), |
| 22 | Struct { |
| 23 | foo: i32, |
| 24 | bar: bool, |
| 25 | }, |
| 26 | EmptyTuple(), |
| 27 | Tuple(i32, bool), |
| 28 | UnitTwo, |
| 29 | #[serde(with = "unit_variant_as_u64")] |
| 30 | #[schemars(with = "u64")] |
| 31 | UnitAsInt, |
| 32 | #[serde(with = "tuple_variant_as_str")] |
| 33 | #[schemars(schema_with = "tuple_variant_as_str::json_schema")] |
| 34 | TupleAsStr(i32, bool), |
| 35 | } |
| 36 | |
| 37 | impl External { |
| 38 | fn values() -> impl IntoIterator<Item = Self> { |
| 39 | [ |
| 40 | Self::UnitOne, |
| 41 | Self::StringMap( |
| 42 | [("hello".to_owned(), "world".to_owned())] |
| 43 | .into_iter() |
| 44 | .collect(), |
| 45 | ), |
| 46 | Self::UnitStructNewType(UnitStruct), |
| 47 | Self::StructNewType(Struct { |
| 48 | foo: 123, |
| 49 | bar: true, |
| 50 | }), |
| 51 | Self::Struct { |
| 52 | foo: 123, |
| 53 | bar: true, |
| 54 | }, |
| 55 | // Ignored due to https://github.com/serde-rs/json/issues/1084 |
| 56 | // Self::EmptyTuple(), |
| 57 | Self::Tuple(456, false), |
| 58 | Self::UnitTwo, |
| 59 | Self::UnitAsInt, |
| 60 | Self::TupleAsStr(789, true), |
| 61 | ] |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #[derive(JsonSchema, Deserialize, Serialize)] |
| 66 | #[serde(tag = "tag")] |
| 67 | enum Internal { |
| 68 | UnitOne, |
| 69 | StringMap(BTreeMap<String, String>), |
| 70 | UnitStructNewType(UnitStruct), |
| 71 | StructNewType(Struct), |
| 72 | Struct { foo: i32, bar: bool }, |
| 73 | // Internally-tagged enums don't support tuple variants |
| 74 | // EmptyTuple(), |
| 75 | // Tuple(i32, bool), |
| 76 | UnitTwo, |
| 77 | // Internally-tagged enum variants don't support non-object "payloads" |
| 78 | // #[serde(with = "unit_variant_as_u64")] |
| 79 | // #[schemars(with = "u64")] |
| 80 | // UnitAsInt, |
| 81 | // Internally-tagged enums don't support tuple variants |
| 82 | // #[serde(with = "tuple_variant_as_str")] |
| 83 | // #[schemars(schema_with = "tuple_variant_as_str::json_schema")] |
| 84 | // TupleAsStr(i32, bool), |
| 85 | } |
| 86 | |
| 87 | impl Internal { |
| 88 | fn values() -> impl IntoIterator<Item = Self> { |
| 89 | [ |
| 90 | Self::UnitOne, |
| 91 | Self::StringMap( |
| 92 | [("hello".to_owned(), "world".to_owned())] |
| 93 | .into_iter() |
| 94 | .collect(), |
| 95 | ), |
| 96 | Self::UnitStructNewType(UnitStruct), |
| 97 | Self::StructNewType(Struct { |
| 98 | foo: 123, |
| 99 | bar: true, |
| 100 | }), |
| 101 | Self::Struct { |
| 102 | foo: 123, |
| 103 | bar: true, |
| 104 | }, |
| 105 | // Self::EmptyTuple(), |
| 106 | // Self::Tuple(456, false), |
| 107 | Self::UnitTwo, |
| 108 | // Self::UnitAsInt, |
| 109 | // Self::TupleAsStr(789, true), |
| 110 | ] |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[derive(JsonSchema, Deserialize, Serialize)] |
| 115 | #[serde(tag = "tag", content = "content")] |
| 116 | enum Adjacent { |
| 117 | UnitOne, |
| 118 | StringMap(BTreeMap<String, String>), |
| 119 | UnitStructNewType(UnitStruct), |
| 120 | StructNewType(Struct), |
| 121 | Struct { |
| 122 | foo: i32, |
| 123 | bar: bool, |
| 124 | }, |
| 125 | EmptyTuple(), |
| 126 | Tuple(i32, bool), |
| 127 | UnitTwo, |
| 128 | #[serde(with = "unit_variant_as_u64")] |
| 129 | #[schemars(with = "u64")] |
| 130 | UnitAsInt, |
| 131 | #[serde(with = "tuple_variant_as_str")] |
| 132 | #[schemars(schema_with = "tuple_variant_as_str::json_schema")] |
| 133 | TupleAsStr(i32, bool), |
| 134 | } |
| 135 | |
| 136 | impl Adjacent { |
| 137 | fn values() -> impl IntoIterator<Item = Self> { |
| 138 | [ |
| 139 | Self::UnitOne, |
| 140 | Self::StringMap( |
| 141 | [("hello".to_owned(), "world".to_owned())] |
| 142 | .into_iter() |
| 143 | .collect(), |
| 144 | ), |
| 145 | Self::UnitStructNewType(UnitStruct), |
| 146 | Self::StructNewType(Struct { |
| 147 | foo: 123, |
| 148 | bar: true, |
| 149 | }), |
| 150 | Self::Struct { |
| 151 | foo: 123, |
| 152 | bar: true, |
| 153 | }, |
| 154 | Self::EmptyTuple(), |
| 155 | Self::Tuple(456, false), |
| 156 | Self::UnitTwo, |
| 157 | Self::UnitAsInt, |
| 158 | Self::TupleAsStr(789, true), |
| 159 | ] |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | #[derive(JsonSchema, Deserialize, Serialize)] |
| 164 | #[serde(untagged)] |
| 165 | enum Untagged { |
| 166 | UnitOne, |
| 167 | StringMap(BTreeMap<String, String>), |
| 168 | UnitStructNewType(UnitStruct), |
| 169 | StructNewType(Struct), |
| 170 | Struct { |
| 171 | foo: i32, |
| 172 | bar: bool, |
| 173 | }, |
| 174 | EmptyTuple(), |
| 175 | Tuple(i32, bool), |
| 176 | UnitTwo, |
| 177 | #[serde(with = "unit_variant_as_u64")] |
| 178 | #[schemars(with = "u64")] |
| 179 | UnitAsInt, |
| 180 | #[serde(with = "tuple_variant_as_str")] |
| 181 | #[schemars(schema_with = "tuple_variant_as_str::json_schema")] |
| 182 | TupleAsStr(i32, bool), |
| 183 | } |
| 184 | |
| 185 | impl Untagged { |
| 186 | fn values() -> impl IntoIterator<Item = Self> { |
| 187 | [ |
| 188 | Self::UnitOne, |
| 189 | Self::StringMap( |
| 190 | [("hello".to_owned(), "world".to_owned())] |
| 191 | .into_iter() |
| 192 | .collect(), |
| 193 | ), |
| 194 | Self::UnitStructNewType(UnitStruct), |
| 195 | Self::StructNewType(Struct { |
| 196 | foo: 123, |
| 197 | bar: true, |
| 198 | }), |
| 199 | Self::Struct { |
| 200 | foo: 123, |
| 201 | bar: true, |
| 202 | }, |
| 203 | Self::EmptyTuple(), |
| 204 | Self::Tuple(456, false), |
| 205 | Self::UnitTwo, |
| 206 | Self::UnitAsInt, |
| 207 | Self::TupleAsStr(789, true), |
| 208 | ] |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | mod unit_variant_as_u64 { |
| 213 | pub(super) fn serialize<S>(ser: S) -> Result<S::Ok, S::Error> |
| 214 | where |
| 215 | S: serde::Serializer, |
| 216 | { |
| 217 | ser.serialize_u64(42) |
| 218 | } |
| 219 | |
| 220 | pub(super) fn deserialize<'de, D>(deser: D) -> Result<(), D::Error> |
| 221 | where |
| 222 | D: serde::Deserializer<'de>, |
| 223 | { |
| 224 | use serde::de::Deserialize; |
| 225 | |
| 226 | u64::deserialize(deser).map(|_| ()) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | mod tuple_variant_as_str { |
| 231 | pub(super) fn serialize<S>(i: &i32, b: &bool, ser: S) -> Result<S::Ok, S::Error> |
| 232 | where |
| 233 | S: serde::Serializer, |
| 234 | { |
| 235 | ser.collect_str(&format_args!("{i} {b}")) |
| 236 | } |
| 237 | |
| 238 | pub(super) fn deserialize<'de, D>(deser: D) -> Result<(i32, bool), D::Error> |
| 239 | where |
| 240 | D: serde::Deserializer<'de>, |
| 241 | { |
| 242 | use serde::de::{Deserialize, Error}; |
| 243 | let error = || Error::custom("invalid string"); |
| 244 | |
| 245 | let (i, b) = <&str>::deserialize(deser)? |
| 246 | .split_once(' ') |
| 247 | .ok_or_else(error)?; |
| 248 | |
| 249 | Ok(( |
| 250 | i.parse().map_err(|_| error())?, |
| 251 | b.parse().map_err(|_| error())?, |
| 252 | )) |
| 253 | } |
| 254 | |
| 255 | pub(super) fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema { |
| 256 | schemars::json_schema!({ |
| 257 | "type": "string", |
| 258 | "pattern": r"^\d+ (true|false)$" |
| 259 | }) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn externally_tagged_enum() { |
| 265 | test!(External) |
| 266 | .assert_snapshot() |
| 267 | .assert_allows_ser_roundtrip(External::values()) |
| 268 | .assert_matches_de_roundtrip(arbitrary_values()); |
| 269 | } |
| 270 | |
| 271 | #[test] |
| 272 | fn internally_tagged_enum() { |
| 273 | test!(Internal) |
| 274 | .assert_snapshot() |
| 275 | .assert_allows_ser_roundtrip(Internal::values()) |
| 276 | .assert_matches_de_roundtrip(arbitrary_values()); |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn adjacently_tagged_enum() { |
| 281 | test!(Adjacent) |
| 282 | .assert_snapshot() |
| 283 | .assert_allows_ser_roundtrip(Adjacent::values()) |
| 284 | .assert_matches_de_roundtrip(arbitrary_values()); |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn untagged_enum() { |
| 289 | test!(Untagged) |
| 290 | .assert_snapshot() |
| 291 | .assert_allows_ser_roundtrip(Untagged::values()) |
| 292 | .assert_matches_de_roundtrip(arbitrary_values()); |
| 293 | } |
| 294 | |
| 295 | #[test] |
| 296 | fn untagged_enum_with_titles() { |
| 297 | test!( |
| 298 | Untagged, |
| 299 | SchemaSettings::default().with(|s| s.untagged_enum_variant_titles = true) |
| 300 | ) |
| 301 | .custom(|s, _| { |
| 302 | assert_eq!( |
| 303 | s.pointer("/anyOf/0/title").and_then(Value::as_str), |
| 304 | Some("UnitOne") |
| 305 | ); |
| 306 | for variant in s.pointer("/anyOf").and_then(Value::as_array).unwrap() { |
| 307 | assert!(variant.get("title").is_some_and(Value::is_string)); |
| 308 | } |
| 309 | }) |
| 310 | .assert_snapshot() |
| 311 | .assert_allows_ser_roundtrip(Untagged::values()) |
| 312 | .assert_matches_de_roundtrip(arbitrary_values()); |
| 313 | } |
| 314 | |
| 315 | #[derive(JsonSchema, Serialize, Deserialize)] |
| 316 | enum NoVariants {} |
| 317 | |
| 318 | #[test] |
| 319 | fn no_variants() { |
| 320 | test!(NoVariants) |
| 321 | .assert_snapshot() |
| 322 | .assert_rejects_de(arbitrary_values()); |
| 323 | } |
| 324 | |
| 325 | #[derive(JsonSchema, Serialize, Deserialize)] |
| 326 | #[serde(rename_all_fields = "UPPERCASE", rename_all = "snake_case")] |
| 327 | enum Renamed { |
| 328 | StructVariant { |
| 329 | field: String, |
| 330 | }, |
| 331 | #[serde(rename = "custom name variant")] |
| 332 | RenamedStructVariant { |
| 333 | #[serde(rename = "custom name field")] |
| 334 | field: String, |
| 335 | }, |
| 336 | } |
| 337 | |
| 338 | #[test] |
| 339 | fn renamed() { |
| 340 | test!(Renamed) |
| 341 | .assert_snapshot() |
| 342 | .assert_allows_ser_roundtrip([ |
| 343 | Renamed::StructVariant { |
| 344 | field: "foo".to_owned(), |
| 345 | }, |
| 346 | Renamed::RenamedStructVariant { |
| 347 | field: "bar".to_owned(), |
| 348 | }, |
| 349 | ]) |
| 350 | .assert_rejects_de(arbitrary_values()); |
| 351 | } |
| 352 | |
| 353 | #[allow(dead_code)] |
| 354 | #[derive(JsonSchema, Deserialize, Serialize)] |
| 355 | enum SoundOfMusic { |
| 356 | /// # A deer |
| 357 | /// |
| 358 | /// A female deer |
| 359 | Do, |
| 360 | /// A drop of golden sun |
| 361 | Re, |
| 362 | /// A name I call myself |
| 363 | Mi, |
| 364 | } |
| 365 | |
| 366 | #[test] |
| 367 | fn unit_variants_with_doc_comments() { |
| 368 | test!(SoundOfMusic) |
| 369 | .assert_snapshot() |
| 370 | .assert_allows_ser_roundtrip([SoundOfMusic::Do, SoundOfMusic::Re, SoundOfMusic::Mi]) |
| 371 | .assert_rejects_de(arbitrary_values()) |
| 372 | .custom(|schema, _| { |
| 373 | assert_eq!( |
| 374 | schema.as_value().pointer("/oneOf/0/title"), |
| 375 | Some(&("A deer".into())), |
| 376 | ); |
| 377 | assert_eq!( |
| 378 | schema.as_value().pointer("/oneOf/0/description"), |
| 379 | Some(&("A female deer".into())), |
| 380 | ); |
| 381 | assert_eq!( |
| 382 | schema.as_value().pointer("/oneOf/1/description"), |
| 383 | Some(&("A drop of golden sun".into())), |
| 384 | ); |
| 385 | assert_eq!( |
| 386 | schema.as_value().pointer("/oneOf/2/description"), |
| 387 | Some(&("A name I call myself".into())), |
| 388 | ); |
| 389 | }); |
| 390 | } |