| 1 |
//! Test Cases |
| 2 |
|
| 3 |
use crate::utils::{check_matches_schema, check_valid_json_schema}; |
| 4 |
use expect_test::expect_file; |
| 5 |
use schemars::JsonSchema; |
| 6 |
use serde::Serialize; |
| 7 |
use serde_json::json; |
| 8 |
use serde_with::*; |
| 9 |
use std::collections::BTreeSet; |
| 10 |
|
| 11 |
// This avoids us having to add `#[schemars(crate = "::schemars_0_8")]` all |
| 12 |
// over the place. We're not testing that and it is inconvenient. |
| 13 |
extern crate schemars_0_8 as schemars; |
| 14 |
|
| 15 |
mod utils; |
| 16 |
|
| 17 |
/// Declare a snapshot tests for a struct. |
| 18 |
/// |
| 19 |
/// The snapshot files are stored under the `schemars_0_8` folder alongside |
| 20 |
/// this test file. |
| 21 |
macro_rules! declare_snapshot_test { |
| 22 |
{$( |
| 23 |
$( #[$tattr:meta] )* |
| 24 |
$test:ident { |
| 25 |
$( #[$stattr:meta] )* |
| 26 |
struct $name:ident { |
| 27 |
$( |
| 28 |
$( #[ $fattr:meta ] )* |
| 29 |
$field:ident : $ty:ty |
| 30 |
),* |
| 31 |
$(,)? |
| 32 |
} |
| 33 |
} |
| 34 |
)*} => {$( |
| 35 |
#[test] |
| 36 |
$(#[$tattr])* |
| 37 |
fn $test() { |
| 38 |
#[serde_as] |
| 39 |
#[derive(JsonSchema, Serialize)] |
| 40 |
$( #[$stattr] )* |
| 41 |
struct $name { |
| 42 |
$( |
| 43 |
$( #[$fattr] )* |
| 44 |
$field: $ty, |
| 45 |
)* |
| 46 |
} |
| 47 |
|
| 48 |
let schema = schemars::schema_for!($name); |
| 49 |
let _ = jsonschema::Validator::new( |
| 50 |
&serde_json::to_value(&schema).expect("generated schema is not valid json"), |
| 51 |
) |
| 52 |
.expect("generated schema is not valid"); |
| 53 |
|
| 54 |
let mut schema = serde_json::to_string_pretty(&schema) |
| 55 |
.expect("schema could not be serialized"); |
| 56 |
schema.push('\n'); |
| 57 |
|
| 58 |
let filename = concat!("./", module_path!(), "::", stringify!($test), ".json") |
| 59 |
.replace("::", "/"); |
| 60 |
|
| 61 |
let expected = expect_file![filename]; |
| 62 |
expected.assert_eq(&schema); |
| 63 |
} |
| 64 |
)*} |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn schemars_basic() { |
| 69 |
#[serde_as] |
| 70 |
#[derive(JsonSchema, Serialize)] |
| 71 |
#[schemars(crate = "::schemars_0_8")] |
| 72 |
struct Basic { |
| 73 |
/// Basic field, no attribute |
| 74 |
bare_field: u32, |
| 75 |
|
| 76 |
/// Field that directly uses DisplayFromStr |
| 77 |
#[serde_as(as = "DisplayFromStr")] |
| 78 |
display_from_str: u32, |
| 79 |
|
| 80 |
/// Same does not implement JsonSchema directly so this checks that the |
| 81 |
/// correct schemars attribute was injected. |
| 82 |
#[serde_as(as = "Same")] |
| 83 |
same: u32, |
| 84 |
|
| 85 |
/// This checks that Same still works when wrapped in a box. |
| 86 |
#[serde_as(as = "Box<Same>")] |
| 87 |
box_same: Box<u32>, |
| 88 |
|
| 89 |
/// Same thing, but with a Vec this time. |
| 90 |
#[serde_as(as = "Vec<_>")] |
| 91 |
vec_same: Vec<u32>, |
| 92 |
} |
| 93 |
|
| 94 |
let schema = schemars::schema_for!(Basic); |
| 95 |
let mut schema = serde_json::to_string_pretty(&schema).expect("schema could not be serialized"); |
| 96 |
schema.push('\n'); |
| 97 |
|
| 98 |
let expected = expect_file!["./schemars_0_8/schemars_basic.json"]; |
| 99 |
expected.assert_eq(&schema); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn schemars_other_cfg_attrs() { |
| 104 |
#[serde_as] |
| 105 |
#[derive(JsonSchema, Serialize)] |
| 106 |
struct Test { |
| 107 |
#[serde_as(as = "DisplayFromStr")] |
| 108 |
#[cfg_attr(any(), arbitrary("some" |weird| syntax::<bool, 2>()))] |
| 109 |
#[cfg_attr(any(), schemars(with = "i32"))] |
| 110 |
custom: i32, |
| 111 |
} |
| 112 |
|
| 113 |
check_matches_schema::<Test>(&json!({ |
| 114 |
"custom": "23", |
| 115 |
})); |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn schemars_custom_with() { |
| 120 |
#[serde_as] |
| 121 |
#[derive(JsonSchema, Serialize)] |
| 122 |
struct Test { |
| 123 |
#[serde_as(as = "DisplayFromStr")] |
| 124 |
#[schemars(with = "i32")] |
| 125 |
custom: i32, |
| 126 |
|
| 127 |
#[serde_as(as = "DisplayFromStr")] |
| 128 |
#[cfg_attr(any(), schemars(with = "i32"))] |
| 129 |
with_disabled: i32, |
| 130 |
|
| 131 |
#[serde_as(as = "DisplayFromStr")] |
| 132 |
#[cfg_attr(all(), schemars(with = "i32"))] |
| 133 |
always_enabled: i32, |
| 134 |
} |
| 135 |
|
| 136 |
check_matches_schema::<Test>(&json!({ |
| 137 |
"custom": 3, |
| 138 |
"with_disabled": "5", |
| 139 |
"always_enabled": 7, |
| 140 |
})); |
| 141 |
} |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn schemars_deserialize_only_bug_735() { |
| 145 |
#[serde_as] |
| 146 |
#[derive(JsonSchema, Serialize)] |
| 147 |
#[schemars(crate = "::schemars_0_8")] |
| 148 |
struct Basic { |
| 149 |
/// Basic field, no attribute |
| 150 |
bare_field: u32, |
| 151 |
|
| 152 |
/// Will emit matching schemars attribute |
| 153 |
#[serde_as(as = "PickFirst<(_, DisplayFromStr)>")] |
| 154 |
both: u32, |
| 155 |
|
| 156 |
/// Can emit schemars with serialize_as, but it will be ignored |
| 157 |
#[serde_as(serialize_as = "PickFirst<(_, DisplayFromStr)>")] |
| 158 |
serialize_only: u32, |
| 159 |
|
| 160 |
/// schemars doesn't support deserialize_as |
| 161 |
#[serde_as(deserialize_as = "PickFirst<(_, DisplayFromStr)>")] |
| 162 |
deserialize_only: u32, |
| 163 |
|
| 164 |
/// Can emit schemars with serialize_as, but it will be ignored |
| 165 |
/// schemars doesn't support deserialize_as |
| 166 |
#[serde_as( |
| 167 |
serialize_as = "PickFirst<(_, DisplayFromStr)>", |
| 168 |
deserialize_as = "PickFirst<(_, DisplayFromStr)>" |
| 169 |
)] |
| 170 |
serialize_and_deserialize: u32, |
| 171 |
} |
| 172 |
|
| 173 |
let schema = schemars::schema_for!(Basic); |
| 174 |
let mut schema = serde_json::to_string_pretty(&schema).expect("schema could not be serialized"); |
| 175 |
schema.push('\n'); |
| 176 |
|
| 177 |
let expected = expect_file!["./schemars_0_8/schemars_deserialize_only_bug_735.json"]; |
| 178 |
expected.assert_eq(&schema); |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn schemars_custom_schema_with() { |
| 183 |
fn custom_int(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { |
| 184 |
use schemars::schema::*; |
| 185 |
|
| 186 |
SchemaObject { |
| 187 |
instance_type: Some(InstanceType::Integer.into()), |
| 188 |
..Default::default() |
| 189 |
} |
| 190 |
.into() |
| 191 |
} |
| 192 |
|
| 193 |
#[serde_as] |
| 194 |
#[derive(JsonSchema, Serialize)] |
| 195 |
struct Test { |
| 196 |
#[serde_as(as = "DisplayFromStr")] |
| 197 |
#[schemars(schema_with = "custom_int")] |
| 198 |
custom: i32, |
| 199 |
|
| 200 |
#[serde_as(as = "DisplayFromStr")] |
| 201 |
#[cfg_attr(any(), schemars(schema_with = "custom_int"))] |
| 202 |
with_disabled: i32, |
| 203 |
|
| 204 |
#[serde_as(as = "DisplayFromStr")] |
| 205 |
#[cfg_attr(all(), schemars(schema_with = "custom_int"))] |
| 206 |
always_enabled: i32, |
| 207 |
} |
| 208 |
|
| 209 |
check_matches_schema::<Test>(&json!({ |
| 210 |
"custom": 3, |
| 211 |
"with_disabled": "5", |
| 212 |
"always_enabled": 7, |
| 213 |
})); |
| 214 |
} |
| 215 |
|
| 216 |
mod test_std { |
| 217 |
use super::*; |
| 218 |
use std::collections::{BTreeMap, VecDeque}; |
| 219 |
|
| 220 |
declare_snapshot_test! { |
| 221 |
option { |
| 222 |
struct Test { |
| 223 |
#[serde_with(as = "Option<_>")] |
| 224 |
optional: Option<i32>, |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
vec { |
| 229 |
struct Test { |
| 230 |
#[serde_with(as = "Vec<_>")] |
| 231 |
vec: Vec<String> |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
vec_deque { |
| 236 |
struct Test { |
| 237 |
#[serde_with(as = "VecDeque<_>")] |
| 238 |
vec_deque: VecDeque<String> |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
map { |
| 243 |
struct Test { |
| 244 |
#[serde_with(as = "BTreeMap<_, _>")] |
| 245 |
map: BTreeMap<String, i32> |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
set { |
| 250 |
struct Test { |
| 251 |
#[serde_with(as = "BTreeSet<_>")] |
| 252 |
map: BTreeSet<String>, |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
tuples { |
| 257 |
struct Test { |
| 258 |
#[serde_with(as = "()")] |
| 259 |
tuple0: (), |
| 260 |
|
| 261 |
#[serde_with(as = "(_ ,)")] |
| 262 |
tuple1: (i32,), |
| 263 |
|
| 264 |
#[serde_with(as = "(_, _)")] |
| 265 |
tuple2: (i32, i32), |
| 266 |
|
| 267 |
#[serde_with(as = "(_, _, _)")] |
| 268 |
tuple3: (i32, i32, String) |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
mod snapshots { |
| 275 |
use super::*; |
| 276 |
use serde_with::formats::*; |
| 277 |
|
| 278 |
#[allow(dead_code)] |
| 279 |
#[derive(JsonSchema, Serialize)] |
| 280 |
enum Mappable { |
| 281 |
A(i32), |
| 282 |
B(String), |
| 283 |
C { c: i32, b: Option<u64> }, |
| 284 |
} |
| 285 |
|
| 286 |
#[derive(JsonSchema, Serialize)] |
| 287 |
struct KvMapData { |
| 288 |
#[serde(rename = "$key$")] |
| 289 |
key: String, |
| 290 |
|
| 291 |
a: u32, |
| 292 |
b: String, |
| 293 |
c: f32, |
| 294 |
d: bool, |
| 295 |
} |
| 296 |
|
| 297 |
#[allow(dead_code, variant_size_differences)] |
| 298 |
#[derive(JsonSchema, Serialize)] |
| 299 |
#[serde(tag = "$key$")] |
| 300 |
enum KvMapEnum { |
| 301 |
TypeA { a: u32 }, |
| 302 |
TypeB { b: String }, |
| 303 |
TypeC { c: bool }, |
| 304 |
} |
| 305 |
|
| 306 |
#[derive(JsonSchema, Serialize)] |
| 307 |
struct KvMapFlatten { |
| 308 |
#[serde(flatten)] |
| 309 |
data: KvMapEnum, |
| 310 |
extra: bool, |
| 311 |
} |
| 312 |
|
| 313 |
declare_snapshot_test! { |
| 314 |
bytes { |
| 315 |
struct Test { |
| 316 |
#[serde_as(as = "Bytes")] |
| 317 |
bytes: Vec<u8>, |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
default_on_null { |
| 322 |
struct Test { |
| 323 |
#[serde_as(as = "DefaultOnNull<_>")] |
| 324 |
data: String, |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
string_with_separator { |
| 329 |
struct Test { |
| 330 |
#[serde_as(as = "StringWithSeparator<CommaSeparator, String>")] |
| 331 |
data: Vec<String>, |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
from_into { |
| 336 |
struct Test { |
| 337 |
#[serde_as(as = "FromInto<u64>")] |
| 338 |
data: u32, |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
map { |
| 343 |
struct Test { |
| 344 |
#[serde_as(as = "Map<_, _>")] |
| 345 |
data: Vec<(String, u32)>, |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
map_fixed { |
| 350 |
struct Test { |
| 351 |
#[serde_as(as = "Map<_, _>")] |
| 352 |
data: [(String, u32); 4], |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
set_last_value_wins { |
| 357 |
struct Test { |
| 358 |
#[serde_as(as = "SetLastValueWins<_>")] |
| 359 |
data: BTreeSet<u32>, |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
set_prevent_duplicates { |
| 364 |
struct Test { |
| 365 |
#[serde_as(as = "SetPreventDuplicates<_>")] |
| 366 |
data: BTreeSet<u32>, |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
duration { |
| 371 |
struct Test { |
| 372 |
#[serde_as(as = "DurationSeconds<u64, Flexible>")] |
| 373 |
seconds: std::time::Duration, |
| 374 |
|
| 375 |
#[serde_as(as = "DurationSecondsWithFrac<f64, Flexible>")] |
| 376 |
frac: std::time::Duration, |
| 377 |
|
| 378 |
#[serde_as(as = "DurationSeconds<String, Flexible>")] |
| 379 |
flexible_string: std::time::Duration, |
| 380 |
|
| 381 |
#[serde_as(as = "DurationSeconds<u64, Strict>")] |
| 382 |
seconds_u64_strict: std::time::Duration, |
| 383 |
|
| 384 |
#[serde_as(as = "TimestampSeconds<i64, Flexible>")] |
| 385 |
time_i64: std::time::SystemTime, |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
enum_map { |
| 390 |
struct Test { |
| 391 |
#[serde_as(as = "EnumMap")] |
| 392 |
data: Vec<Mappable>, |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
key_value_map { |
| 397 |
struct Test { |
| 398 |
#[serde_as(as = "KeyValueMap<_>")] |
| 399 |
data: Vec<KvMapData>, |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
key_value_map_enum { |
| 404 |
struct Test { |
| 405 |
#[serde_as(as = "KeyValueMap<_>")] |
| 406 |
data: Vec<KvMapEnum>, |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
key_value_map_flatten { |
| 411 |
struct Test { |
| 412 |
#[serde_as(as = "KeyValueMap<_>")] |
| 413 |
data: Vec<KvMapFlatten>, |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
one_or_many_prefer_one { |
| 418 |
#[serde(transparent)] |
| 419 |
struct Test { |
| 420 |
#[serde_as(as = "OneOrMany<_, PreferOne>")] |
| 421 |
data: Vec<i32>, |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
pickfirst { |
| 426 |
#[serde(transparent)] |
| 427 |
struct Test { |
| 428 |
#[serde_as(as = "PickFirst<(_, DisplayFromStr)>")] |
| 429 |
value: u32 |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
pickfirst_nested { |
| 434 |
#[serde(transparent)] |
| 435 |
struct Test { |
| 436 |
#[serde_as(as = "OneOrMany<PickFirst<(_, DisplayFromStr)>>")] |
| 437 |
optional_value: Vec<u32> |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
one_or_many_nested { |
| 442 |
struct Test { |
| 443 |
#[serde_as(as = "Option<OneOrMany<_>>")] |
| 444 |
optional_many: Option<Vec<String>>, |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
mod derive { |
| 451 |
use super::*; |
| 452 |
|
| 453 |
#[serde_as] |
| 454 |
#[derive(Serialize)] |
| 455 |
#[cfg_attr(all(), derive(JsonSchema))] |
| 456 |
struct Enabled { |
| 457 |
#[serde_as(as = "DisplayFromStr")] |
| 458 |
field: u32, |
| 459 |
} |
| 460 |
|
| 461 |
#[allow(dead_code)] |
| 462 |
#[serde_as] |
| 463 |
#[derive(Serialize)] |
| 464 |
#[cfg_attr(any(), derive(JsonSchema))] |
| 465 |
struct Disabled { |
| 466 |
// If we are incorrectly adding `#[schemars(with = ...)]` attributes |
| 467 |
// then we should get an error on this field. |
| 468 |
#[serde_as(as = "DisplayFromStr")] |
| 469 |
field: u32, |
| 470 |
} |
| 471 |
|
| 472 |
#[test] |
| 473 |
fn test_enabled_has_correct_schema() { |
| 474 |
check_valid_json_schema(&Enabled { field: 77 }); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
mod array { |
| 479 |
use super::*; |
| 480 |
|
| 481 |
#[serde_as] |
| 482 |
#[derive(JsonSchema, Serialize)] |
| 483 |
struct FixedArray { |
| 484 |
#[serde_as(as = "[_; 3]")] |
| 485 |
array: [u32; 3], |
| 486 |
} |
| 487 |
|
| 488 |
#[test] |
| 489 |
fn test_serialized_is_valid() { |
| 490 |
let array = FixedArray { array: [1, 2, 3] }; |
| 491 |
|
| 492 |
check_valid_json_schema(&array); |
| 493 |
} |
| 494 |
|
| 495 |
#[test] |
| 496 |
fn test_valid_json() { |
| 497 |
let value = json!({ "array": [1, 2, 3] }); |
| 498 |
check_matches_schema::<FixedArray>(&value); |
| 499 |
} |
| 500 |
|
| 501 |
#[test] |
| 502 |
#[should_panic] |
| 503 |
fn test_too_short() { |
| 504 |
check_matches_schema::<FixedArray>(&json!({ |
| 505 |
"array": [1], |
| 506 |
})); |
| 507 |
} |
| 508 |
|
| 509 |
#[test] |
| 510 |
#[should_panic] |
| 511 |
fn test_too_long() { |
| 512 |
check_matches_schema::<FixedArray>(&json!({ |
| 513 |
"array": [1, 2, 3, 4] |
| 514 |
})); |
| 515 |
} |
| 516 |
|
| 517 |
#[test] |
| 518 |
#[should_panic] |
| 519 |
fn test_wrong_item_type() { |
| 520 |
check_matches_schema::<FixedArray>(&json!({ |
| 521 |
"array": ["1", "2", "3"] |
| 522 |
})); |
| 523 |
} |
| 524 |
|
| 525 |
#[test] |
| 526 |
#[should_panic] |
| 527 |
fn test_oob_item() { |
| 528 |
check_matches_schema::<FixedArray>(&json!({ |
| 529 |
"array": [-1, 0x1_0000_0000i64, 32] |
| 530 |
})); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
mod bool_from_int { |
| 535 |
use super::*; |
| 536 |
use serde_with::formats::{Flexible, Strict}; |
| 537 |
|
| 538 |
#[serde_as] |
| 539 |
#[derive(Serialize, JsonSchema)] |
| 540 |
struct BoolStrict { |
| 541 |
#[serde_as(as = "BoolFromInt<Strict>")] |
| 542 |
value: bool, |
| 543 |
} |
| 544 |
|
| 545 |
#[serde_as] |
| 546 |
#[derive(Serialize, JsonSchema)] |
| 547 |
struct BoolFlexible { |
| 548 |
#[serde_as(as = "BoolFromInt<Flexible>")] |
| 549 |
value: bool, |
| 550 |
} |
| 551 |
|
| 552 |
#[test] |
| 553 |
fn test_serialized_strict_is_valid() { |
| 554 |
check_valid_json_schema(&vec![ |
| 555 |
BoolStrict { value: true }, |
| 556 |
BoolStrict { value: false }, |
| 557 |
]); |
| 558 |
} |
| 559 |
|
| 560 |
#[test] |
| 561 |
fn test_serialized_flexible_is_valid() { |
| 562 |
check_valid_json_schema(&vec![ |
| 563 |
BoolFlexible { value: true }, |
| 564 |
BoolFlexible { value: false }, |
| 565 |
]); |
| 566 |
} |
| 567 |
|
| 568 |
#[test] |
| 569 |
#[should_panic] |
| 570 |
fn strict_out_of_range() { |
| 571 |
check_matches_schema::<BoolStrict>(&json!({ |
| 572 |
"value": 5 |
| 573 |
})); |
| 574 |
} |
| 575 |
|
| 576 |
#[test] |
| 577 |
fn flexible_out_of_range() { |
| 578 |
check_matches_schema::<BoolFlexible>(&json!({ |
| 579 |
"value": 5 |
| 580 |
})); |
| 581 |
} |
| 582 |
|
| 583 |
#[test] |
| 584 |
#[should_panic] |
| 585 |
fn flexible_wrong_type() { |
| 586 |
check_matches_schema::<BoolFlexible>(&json!({ |
| 587 |
"value": "seven" |
| 588 |
})); |
| 589 |
} |
| 590 |
|
| 591 |
#[test] |
| 592 |
#[should_panic] |
| 593 |
fn test_fractional_value_strict() { |
| 594 |
check_matches_schema::<BoolStrict>(&json!({ |
| 595 |
"value": 0.5 |
| 596 |
})); |
| 597 |
} |
| 598 |
|
| 599 |
#[test] |
| 600 |
#[should_panic] |
| 601 |
fn test_fractional_value_flexible() { |
| 602 |
check_matches_schema::<BoolFlexible>(&json!({ |
| 603 |
"value": 0.5 |
| 604 |
})); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
mod bytes_or_string { |
| 609 |
use super::*; |
| 610 |
|
| 611 |
#[serde_as] |
| 612 |
#[derive(Serialize, JsonSchema)] |
| 613 |
struct Test { |
| 614 |
#[serde_as(as = "BytesOrString")] |
| 615 |
bytes: Vec<u8>, |
| 616 |
} |
| 617 |
|
| 618 |
#[test] |
| 619 |
fn test_serialized_is_valid() { |
| 620 |
check_valid_json_schema(&Test { |
| 621 |
bytes: b"test".to_vec(), |
| 622 |
}); |
| 623 |
} |
| 624 |
|
| 625 |
#[test] |
| 626 |
fn test_string_valid_json() { |
| 627 |
check_matches_schema::<Test>(&json!({ |
| 628 |
"bytes": "test string" |
| 629 |
})); |
| 630 |
} |
| 631 |
|
| 632 |
#[test] |
| 633 |
fn test_bytes_valid_json() { |
| 634 |
check_matches_schema::<Test>(&json!({ |
| 635 |
"bytes": [1, 2, 3, 4] |
| 636 |
})); |
| 637 |
} |
| 638 |
|
| 639 |
#[test] |
| 640 |
#[should_panic] |
| 641 |
fn test_int_not_valid_json() { |
| 642 |
check_matches_schema::<Test>(&json!({ |
| 643 |
"bytes": 5 |
| 644 |
})); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
mod enum_map { |
| 649 |
use super::*; |
| 650 |
|
| 651 |
#[derive(Serialize, JsonSchema)] |
| 652 |
struct InnerStruct { |
| 653 |
c: String, |
| 654 |
d: f64, |
| 655 |
} |
| 656 |
|
| 657 |
#[derive(Serialize, JsonSchema)] |
| 658 |
enum Inner { |
| 659 |
A(i32), |
| 660 |
B(String), |
| 661 |
C(InnerStruct), |
| 662 |
} |
| 663 |
|
| 664 |
#[serde_as] |
| 665 |
#[derive(Serialize, JsonSchema)] |
| 666 |
#[serde(transparent)] |
| 667 |
struct Outer(#[serde_as(as = "EnumMap")] Vec<Inner>); |
| 668 |
|
| 669 |
#[test] |
| 670 |
fn test_serialized_is_valid() { |
| 671 |
check_valid_json_schema(&Outer(vec![ |
| 672 |
Inner::A(5), |
| 673 |
Inner::B("test".into()), |
| 674 |
Inner::C(InnerStruct { |
| 675 |
c: "c".into(), |
| 676 |
d: -34.0, |
| 677 |
}), |
| 678 |
])); |
| 679 |
} |
| 680 |
|
| 681 |
#[test] |
| 682 |
fn test_matches_expected() { |
| 683 |
check_matches_schema::<Outer>(&json!({ |
| 684 |
"A": 75, |
| 685 |
"B": "BBBBBB", |
| 686 |
"C": { |
| 687 |
"c": "inner C", |
| 688 |
"d": 777 |
| 689 |
} |
| 690 |
})); |
| 691 |
} |
| 692 |
|
| 693 |
#[test] |
| 694 |
fn test_no_fields_required() { |
| 695 |
check_matches_schema::<Outer>(&json!({})); |
| 696 |
} |
| 697 |
|
| 698 |
#[test] |
| 699 |
#[should_panic] |
| 700 |
fn test_mixed_up_schemas() { |
| 701 |
check_matches_schema::<Outer>(&json!({ |
| 702 |
"A": "b", |
| 703 |
"B": 5 |
| 704 |
})); |
| 705 |
} |
| 706 |
|
| 707 |
#[test] |
| 708 |
#[should_panic] |
| 709 |
fn test_invalid_key() { |
| 710 |
check_matches_schema::<Outer>(&json!({ |
| 711 |
"invalid": 4 |
| 712 |
})); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
mod duration { |
| 717 |
use super::*; |
| 718 |
use serde_with::formats::{Flexible, Strict}; |
| 719 |
use std::time::{Duration, SystemTime}; |
| 720 |
|
| 721 |
#[serde_as] |
| 722 |
#[derive(Serialize, JsonSchema)] |
| 723 |
struct DurationTest { |
| 724 |
#[serde_as(as = "DurationSeconds<u64, Strict>")] |
| 725 |
strict_u64: Duration, |
| 726 |
|
| 727 |
#[serde_as(as = "DurationSeconds<String, Strict>")] |
| 728 |
strict_str: Duration, |
| 729 |
|
| 730 |
#[serde_as(as = "DurationSecondsWithFrac<f64, Strict>")] |
| 731 |
strict_f64: Duration, |
| 732 |
|
| 733 |
#[serde_as(as = "DurationSeconds<u64, Flexible>")] |
| 734 |
flexible_u64: Duration, |
| 735 |
|
| 736 |
#[serde_as(as = "DurationSeconds<f64, Flexible>")] |
| 737 |
flexible_f64: Duration, |
| 738 |
|
| 739 |
#[serde_as(as = "DurationSeconds<String, Flexible>")] |
| 740 |
flexible_str: Duration, |
| 741 |
} |
| 742 |
|
| 743 |
#[test] |
| 744 |
fn test_serialized_is_valid() { |
| 745 |
check_valid_json_schema(&DurationTest { |
| 746 |
strict_u64: Duration::from_millis(2500), |
| 747 |
strict_str: Duration::from_millis(2500), |
| 748 |
strict_f64: Duration::from_millis(2500), |
| 749 |
flexible_u64: Duration::from_millis(2500), |
| 750 |
flexible_f64: Duration::from_millis(2500), |
| 751 |
flexible_str: Duration::from_millis(2500), |
| 752 |
}); |
| 753 |
} |
| 754 |
|
| 755 |
#[serde_as] |
| 756 |
#[derive(Serialize, JsonSchema)] |
| 757 |
struct FlexibleU64Duration(#[serde_as(as = "DurationSeconds<u64, Flexible>")] Duration); |
| 758 |
|
| 759 |
#[serde_as] |
| 760 |
#[derive(Serialize, JsonSchema)] |
| 761 |
struct FlexibleStringDuration(#[serde_as(as = "DurationSeconds<String, Flexible>")] Duration); |
| 762 |
|
| 763 |
#[serde_as] |
| 764 |
#[derive(Serialize, JsonSchema)] |
| 765 |
struct FlexibleTimestamp(#[serde_as(as = "TimestampSeconds<i64, Flexible>")] SystemTime); |
| 766 |
|
| 767 |
#[test] |
| 768 |
fn test_string_as_flexible_u64() { |
| 769 |
check_matches_schema::<FlexibleU64Duration>(&json!("32")); |
| 770 |
} |
| 771 |
|
| 772 |
#[test] |
| 773 |
fn test_integer_as_flexible_u64() { |
| 774 |
check_matches_schema::<FlexibleU64Duration>(&json!(16)); |
| 775 |
} |
| 776 |
|
| 777 |
#[test] |
| 778 |
fn test_number_as_flexible_u64() { |
| 779 |
check_matches_schema::<FlexibleU64Duration>(&json!(54.1)); |
| 780 |
} |
| 781 |
|
| 782 |
#[test] |
| 783 |
#[should_panic] |
| 784 |
fn test_negative_as_flexible_u64() { |
| 785 |
check_matches_schema::<FlexibleU64Duration>(&json!(-5)); |
| 786 |
} |
| 787 |
|
| 788 |
#[test] |
| 789 |
fn test_string_as_flexible_string() { |
| 790 |
check_matches_schema::<FlexibleStringDuration>(&json!("32")); |
| 791 |
} |
| 792 |
|
| 793 |
#[test] |
| 794 |
fn test_integer_as_flexible_string() { |
| 795 |
check_matches_schema::<FlexibleStringDuration>(&json!(16)); |
| 796 |
} |
| 797 |
|
| 798 |
#[test] |
| 799 |
fn test_number_as_flexible_string() { |
| 800 |
check_matches_schema::<FlexibleStringDuration>(&json!(54.1)); |
| 801 |
} |
| 802 |
|
| 803 |
#[test] |
| 804 |
#[should_panic] |
| 805 |
fn test_negative_as_flexible_string() { |
| 806 |
check_matches_schema::<FlexibleStringDuration>(&json!(-5)); |
| 807 |
} |
| 808 |
|
| 809 |
#[test] |
| 810 |
fn test_negative_as_flexible_timestamp() { |
| 811 |
check_matches_schema::<FlexibleTimestamp>(&json!(-50000)); |
| 812 |
} |
| 813 |
|
| 814 |
#[test] |
| 815 |
fn test_negative_string_as_flexible_timestamp() { |
| 816 |
check_matches_schema::<FlexibleTimestamp>(&json!("-50000")); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
#[test] |
| 821 |
fn test_borrow_cow() { |
| 822 |
use std::borrow::Cow; |
| 823 |
|
| 824 |
#[serde_as] |
| 825 |
#[derive(Serialize, JsonSchema)] |
| 826 |
struct Borrowed<'a> { |
| 827 |
#[serde_as(as = "BorrowCow")] |
| 828 |
data: Cow<'a, str>, |
| 829 |
} |
| 830 |
|
| 831 |
check_valid_json_schema(&Borrowed { |
| 832 |
data: Cow::Borrowed("test"), |
| 833 |
}); |
| 834 |
} |
| 835 |
|
| 836 |
#[test] |
| 837 |
fn test_map() { |
| 838 |
#[serde_as] |
| 839 |
#[derive(Serialize, JsonSchema)] |
| 840 |
struct Test { |
| 841 |
map: [(&'static str, u32); 2], |
| 842 |
} |
| 843 |
|
| 844 |
check_valid_json_schema(&Test { |
| 845 |
map: [("a", 1), ("b", 2)], |
| 846 |
}); |
| 847 |
} |
| 848 |
|
| 849 |
#[test] |
| 850 |
fn test_if_is_human_readable() { |
| 851 |
#[serde_as] |
| 852 |
#[derive(Serialize, JsonSchema)] |
| 853 |
struct Test { |
| 854 |
#[serde_as(as = "IfIsHumanReadable<DisplayFromStr>")] |
| 855 |
data: i32, |
| 856 |
} |
| 857 |
|
| 858 |
check_valid_json_schema(&Test { data: 5 }); |
| 859 |
check_matches_schema::<Test>(&json!({ "data": "5" })); |
| 860 |
} |
| 861 |
|
| 862 |
#[test] |
| 863 |
fn test_set_last_value_wins_with_duplicates() { |
| 864 |
#[serde_as] |
| 865 |
#[derive(Serialize, JsonSchema)] |
| 866 |
struct Test { |
| 867 |
#[serde_as(as = "SetLastValueWins<_>")] |
| 868 |
set: BTreeSet<u32>, |
| 869 |
} |
| 870 |
|
| 871 |
check_matches_schema::<Test>(&json!({ |
| 872 |
"set": [ 1, 2, 3, 1, 4, 2 ] |
| 873 |
})); |
| 874 |
} |
| 875 |
|
| 876 |
#[test] |
| 877 |
#[should_panic] |
| 878 |
fn test_set_prevent_duplicates_with_duplicates() { |
| 879 |
#[serde_as] |
| 880 |
#[derive(Serialize, JsonSchema)] |
| 881 |
struct Test { |
| 882 |
#[serde_as(as = "SetPreventDuplicates<_>")] |
| 883 |
set: BTreeSet<u32>, |
| 884 |
} |
| 885 |
|
| 886 |
check_matches_schema::<Test>(&json!({ |
| 887 |
"set": [ 1, 1 ] |
| 888 |
})); |
| 889 |
} |
| 890 |
|
| 891 |
mod key_value_map { |
| 892 |
use super::*; |
| 893 |
use std::collections::BTreeMap; |
| 894 |
|
| 895 |
#[serde_as] |
| 896 |
#[derive(Clone, Debug, JsonSchema, Serialize)] |
| 897 |
#[serde(transparent)] |
| 898 |
struct KVMap<E>( |
| 899 |
#[serde_as(as = "KeyValueMap<_>")] |
| 900 |
#[serde(bound(serialize = "E: Serialize", deserialize = "E: Deserialize<'de>"))] |
| 901 |
Vec<E>, |
| 902 |
); |
| 903 |
|
| 904 |
#[derive(Clone, Debug, JsonSchema, Serialize)] |
| 905 |
#[serde(untagged)] |
| 906 |
enum UntaggedEnum { |
| 907 |
A { |
| 908 |
#[serde(rename = "$key$")] |
| 909 |
key: String, |
| 910 |
field1: String, |
| 911 |
}, |
| 912 |
B(String, i32), |
| 913 |
} |
| 914 |
|
| 915 |
#[test] |
| 916 |
fn test_untagged_enum() { |
| 917 |
let value = KVMap(vec![ |
| 918 |
UntaggedEnum::A { |
| 919 |
key: "v1".into(), |
| 920 |
field1: "field".into(), |
| 921 |
}, |
| 922 |
UntaggedEnum::B("v2".into(), 7), |
| 923 |
]); |
| 924 |
|
| 925 |
check_valid_json_schema(&value); |
| 926 |
} |
| 927 |
|
| 928 |
#[derive(Clone, Debug, JsonSchema, Serialize)] |
| 929 |
#[serde(untagged)] |
| 930 |
enum UntaggedNestedEnum { |
| 931 |
Nested(UntaggedEnum), |
| 932 |
C { |
| 933 |
#[serde(rename = "$key$")] |
| 934 |
key: String, |
| 935 |
field2: i32, |
| 936 |
}, |
| 937 |
} |
| 938 |
|
| 939 |
#[test] |
| 940 |
fn test_untagged_nested_enum() { |
| 941 |
let value = KVMap(vec![ |
| 942 |
UntaggedNestedEnum::Nested(UntaggedEnum::A { |
| 943 |
key: "v1".into(), |
| 944 |
field1: "field".into(), |
| 945 |
}), |
| 946 |
UntaggedNestedEnum::Nested(UntaggedEnum::B("v2".into(), 7)), |
| 947 |
UntaggedNestedEnum::C { |
| 948 |
key: "v2".into(), |
| 949 |
field2: 222, |
| 950 |
}, |
| 951 |
]); |
| 952 |
|
| 953 |
check_valid_json_schema(&value); |
| 954 |
} |
| 955 |
|
| 956 |
#[test] |
| 957 |
fn test_btreemap() { |
| 958 |
let value = KVMap(vec![ |
| 959 |
BTreeMap::from_iter([("$key$", "a"), ("value", "b")]), |
| 960 |
BTreeMap::from_iter([("$key$", "b"), ("value", "d")]), |
| 961 |
]); |
| 962 |
|
| 963 |
check_valid_json_schema(&value); |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
mod one_or_many { |
| 968 |
use super::*; |
| 969 |
use serde_with::formats::{PreferMany, PreferOne}; |
| 970 |
|
| 971 |
#[serde_as] |
| 972 |
#[derive(Clone, Debug, JsonSchema, Serialize)] |
| 973 |
#[serde(transparent)] |
| 974 |
struct WithPreferOne(#[serde_as(as = "OneOrMany<_, PreferOne>")] Vec<i32>); |
| 975 |
|
| 976 |
#[serde_as] |
| 977 |
#[derive(Clone, Debug, JsonSchema, Serialize)] |
| 978 |
#[serde(transparent)] |
| 979 |
struct WithPreferMany(#[serde_as(as = "OneOrMany<_, PreferMany>")] Vec<i32>); |
| 980 |
|
| 981 |
#[test] |
| 982 |
fn test_prefer_one() { |
| 983 |
let single = WithPreferOne(vec![7]); |
| 984 |
let multiple = WithPreferOne(vec![1, 2, 3]); |
| 985 |
|
| 986 |
check_valid_json_schema(&single); |
| 987 |
check_valid_json_schema(&multiple); |
| 988 |
} |
| 989 |
|
| 990 |
#[test] |
| 991 |
fn test_prefer_one_matches() { |
| 992 |
check_matches_schema::<WithPreferOne>(&json!(7)); |
| 993 |
check_matches_schema::<WithPreferOne>(&json!([1, 2, 3])); |
| 994 |
} |
| 995 |
|
| 996 |
#[test] |
| 997 |
#[should_panic] |
| 998 |
fn test_prefer_one_no_invalid_type_one() { |
| 999 |
check_matches_schema::<WithPreferOne>(&json!("test")); |
| 1000 |
} |
| 1001 |
|
| 1002 |
#[test] |
| 1003 |
#[should_panic] |
| 1004 |
fn test_prefer_one_no_invalid_type_many() { |
| 1005 |
check_matches_schema::<WithPreferOne>(&json!(["test", 1])); |
| 1006 |
} |
| 1007 |
|
| 1008 |
#[test] |
| 1009 |
fn test_prefer_many() { |
| 1010 |
let single = WithPreferMany(vec![7]); |
| 1011 |
let multiple = WithPreferMany(vec![1, 2, 3]); |
| 1012 |
|
| 1013 |
check_valid_json_schema(&single); |
| 1014 |
check_valid_json_schema(&multiple); |
| 1015 |
} |
| 1016 |
|
| 1017 |
#[test] |
| 1018 |
#[should_panic] |
| 1019 |
fn test_prefer_many_no_invalid_type_one() { |
| 1020 |
check_matches_schema::<WithPreferMany>(&json!("test")); |
| 1021 |
} |
| 1022 |
|
| 1023 |
#[test] |
| 1024 |
#[should_panic] |
| 1025 |
fn test_prefer_many_no_invalid_type_many() { |
| 1026 |
check_matches_schema::<WithPreferMany>(&json!(["test", 1])); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
#[test] |
| 1031 |
fn test_pickfirst() { |
| 1032 |
#[serde_as] |
| 1033 |
#[derive(JsonSchema, Serialize)] |
| 1034 |
#[serde(transparent)] |
| 1035 |
struct IntOrDisplay(#[serde_as(as = "PickFirst<(_, DisplayFromStr)>")] u32); |
| 1036 |
|
| 1037 |
check_matches_schema::<IntOrDisplay>(&json!(7)); |
| 1038 |
check_matches_schema::<IntOrDisplay>(&json!("17")); |
| 1039 |
} |