-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port "default" tests to new integration test style
- Loading branch information
Showing
6 changed files
with
176 additions
and
111 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
.../tests/integration/snapshots/schemars/tests/integration/default.rs~default_fields.de.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...tests/integration/snapshots/schemars/tests/integration/default.rs~default_fields.ser.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |