Skip to content

Commit

Permalink
Port "default" tests to new integration test style
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 8, 2024
1 parent 1fd1029 commit 0d9ad68
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 111 deletions.
59 changes: 0 additions & 59 deletions schemars/tests/default.rs

This file was deleted.

52 changes: 0 additions & 52 deletions schemars/tests/expected/default.json

This file was deleted.

95 changes: 95 additions & 0 deletions schemars/tests/integration/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use crate::prelude::*;

#[derive(JsonSchema, Deserialize, Serialize, Default)]
#[serde(default)]
struct MyStruct {
integer: u32,
boolean: bool,
option_string: Option<String>,
#[serde(skip_serializing_if = "str::is_empty")]
string_skip_empty: String,
#[serde(with = "struct_2_as_str")]
#[schemars(with = "str", pattern(r"^\d+ (true|false)$"))]
struct2: MyStruct2,
#[serde(skip_serializing)]
not_serialize: NotSerialize,
}

#[derive(JsonSchema, Deserialize, Serialize, Default)]
#[serde(default = "ten_and_true")]
struct MyStruct2 {
#[serde(default = "six")]
integer: u32,
boolean: bool,
}

#[allow(dead_code)]
#[derive(JsonSchema, Deserialize, Default)]
struct NotSerialize(i8);

mod struct_2_as_str {
use super::MyStruct2;

pub fn serialize<S>(value: &MyStruct2, ser: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
ser.collect_str(&format_args!("{} {}", value.integer, value.boolean))
}

pub fn deserialize<'de, D>(deser: D) -> Result<MyStruct2, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::de::{Deserialize, Error};
let error = || Error::custom("invalid string");

let (i, b) = <&str>::deserialize(deser)?
.split_once(' ')
.ok_or_else(error)?;

Ok(MyStruct2 {
integer: i.parse().map_err(|_| error())?,
boolean: b.parse().map_err(|_| error())?,
})
}
}

fn ten_and_true() -> MyStruct2 {
MyStruct2 {
integer: 10,
boolean: true,
}
}

fn six() -> u32 {
6
}

#[test]
fn default_fields() {
test!(MyStruct)
.assert_snapshot()
.assert_allows_ser_roundtrip([
MyStruct::default(),
MyStruct {
integer: 123,
boolean: true,
option_string: Some("test".into()),
string_skip_empty: "test".into(),
struct2: ten_and_true(),
not_serialize: NotSerialize(42),
},
])
.assert_allows_de_roundtrip([
json!({}),
json!({ "not_serialize": 127 })
])
.assert_rejects_de([
json!({ "not_serialize": "a string" })
])
.assert_matches_de_roundtrip(arbitrary_values_except(
Value::is_array,
"structs with `#derive(Deserialize)` can technically be deserialized from sequences, but I don't think many people know (or want!) this, and handling that would make schemas messier",
));
}
1 change: 1 addition & 0 deletions schemars/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod contract;
mod crate_alias;
#[cfg(any(feature = "rust_decimal1", feature = "bigdecimal04"))]
mod decimal;
mod default;
mod deprecated;

mod prelude {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "MyStruct",
"type": "object",
"properties": {
"integer": {
"type": "integer",
"format": "uint32",
"minimum": 0,
"default": 0
},
"boolean": {
"type": "boolean",
"default": false
},
"option_string": {
"type": [
"string",
"null"
],
"default": null
},
"string_skip_empty": {
"type": "string"
},
"struct2": {
"type": "string",
"pattern": "^\\d+ (true|false)$",
"default": "0 false"
},
"not_serialize": {
"$ref": "#/$defs/NotSerialize",
"writeOnly": true
}
},
"$defs": {
"NotSerialize": {
"type": "integer",
"format": "int8"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "MyStruct",
"type": "object",
"properties": {
"integer": {
"type": "integer",
"format": "uint32",
"minimum": 0,
"default": 0
},
"boolean": {
"type": "boolean",
"default": false
},
"option_string": {
"type": [
"string",
"null"
],
"default": null
},
"string_skip_empty": {
"type": "string"
},
"struct2": {
"type": "string",
"pattern": "^\\d+ (true|false)$",
"default": "0 false"
}
},
"required": [
"integer",
"boolean",
"option_string",
"struct2"
]
}

0 comments on commit 0d9ad68

Please sign in to comment.