Skip to content

Commit

Permalink
Add contract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 7, 2024
1 parent ad189d4 commit 887b369
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 30 deletions.
11 changes: 5 additions & 6 deletions schemars/tests/integration/arrayvec.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
use crate::prelude::*;
use arrayvec07::{ArrayString, ArrayVec};
use serde_json::Value;

#[test]
fn arrayvec07() {
test!(ArrayVec<i32, 8>)
.assert_snapshot()
.assert_allows_serde_roundtrip([
.assert_allows_ser_roundtrip([
ArrayVec::from_iter([]),
ArrayVec::from_iter([1, 2, 3, 4, 5, 6, 7, 8]),
])
.assert_matches_deserialize(
.assert_matches_de_roundtrip(
(0..16).map(|len| Value::Array((0..len).map(Value::from).collect())),
)
// FIXME schema allows out-of-range positive integers
.assert_matches_deserialize(arbitrary_values().filter(|v| !is_array_of_u64(v)));
.assert_matches_de_roundtrip(arbitrary_values().filter(|v| !is_array_of_u64(v)));
}

#[test]
fn arrayvec07_arraystring() {
test!(ArrayString<8>)
.assert_identical::<String>()
.assert_allows_serde_roundtrip(["".try_into().unwrap(), "12345678".try_into().unwrap()])
.assert_allows_ser_roundtrip(["".try_into().unwrap(), "12345678".try_into().unwrap()])
// There's not a good way to express UTF-8 byte length in JSON schema,
// so the generated schema just ignores the ArrayString's capacity.
.assert_matches_deserialize(arbitrary_nonstring_values());
.assert_matches_de_roundtrip(arbitrary_nonstring_values());
}

fn is_array_of_u64(value: &Value) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions schemars/tests/integration/bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ where
fn manual_bound_set() {
test!(MyContainer<MyIterator>)
.assert_snapshot()
.assert_allows_serde_roundtrip([MyContainer {
.assert_allows_ser_roundtrip([MyContainer {
associated: "test".to_owned(),
generic: PhantomData,
}])
.assert_matches_deserialize(arbitrary_values());
.assert_matches_de_roundtrip(arbitrary_values());
}
9 changes: 4 additions & 5 deletions schemars/tests/integration/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use crate::prelude::*;
use bytes1::{Bytes, BytesMut};
use serde_json::Value;

#[test]
fn bytes() {
test!(Bytes)
.assert_snapshot()
.assert_allows_serde_roundtrip([Bytes::new(), Bytes::from_iter([12; 34])])
.assert_allows_ser_roundtrip([Bytes::new(), Bytes::from_iter([12; 34])])
// FIXME schema allows out-of-range positive integers
.assert_matches_deserialize(arbitrary_values().filter(|v| !is_array_of_u64(v)));
.assert_matches_de_roundtrip(arbitrary_values().filter(|v| !is_array_of_u64(v)));
}

#[test]
fn bytes_mut() {
test!(BytesMut)
.assert_identical::<Bytes>()
.assert_allows_serde_roundtrip([BytesMut::new(), BytesMut::from_iter([12; 34])])
.assert_allows_ser_roundtrip([BytesMut::new(), BytesMut::from_iter([12; 34])])
// FIXME schema allows out-of-range positive integers
.assert_matches_deserialize(arbitrary_values().filter(|v| !is_array_of_u64(v)));
.assert_matches_de_roundtrip(arbitrary_values().filter(|v| !is_array_of_u64(v)));
}

fn is_array_of_u64(value: &Value) -> bool {
Expand Down
20 changes: 10 additions & 10 deletions schemars/tests/integration/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ fn chrono() {
test!(ChronoTypes).assert_snapshot();

test!(Weekday)
.assert_allows_serde_roundtrip([Weekday::Mon])
.assert_matches_deserialize(arbitrary_values());
.assert_allows_ser_roundtrip([Weekday::Mon])
.assert_matches_de_roundtrip(arbitrary_values());

test!(DateTime<Utc>)
.assert_allows_serde_roundtrip_default()
.assert_matches_deserialize(arbitrary_values());
.assert_allows_ser_roundtrip_default()
.assert_matches_de_roundtrip(arbitrary_values());

test!(NaiveDate)
.assert_allows_serde_roundtrip_default()
.assert_matches_deserialize(arbitrary_values());
.assert_allows_ser_roundtrip_default()
.assert_matches_de_roundtrip(arbitrary_values());

test!(NaiveDateTime)
.assert_allows_serde_roundtrip_default()
.assert_allows_ser_roundtrip_default()
// Custom format "partial-date-time", so arbitrary strings technically allowed by schema
.assert_matches_deserialize(arbitrary_nonstring_values());
.assert_matches_de_roundtrip(arbitrary_nonstring_values());

test!(NaiveTime)
.assert_allows_serde_roundtrip_default()
.assert_allows_ser_roundtrip_default()
// Custom format "partial-time", so arbitrary strings technically allowed by schema
.assert_matches_deserialize(arbitrary_nonstring_values());
.assert_matches_de_roundtrip(arbitrary_nonstring_values());
}
60 changes: 60 additions & 0 deletions schemars/tests/integration/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::prelude::*;

#[derive(JsonSchema, Deserialize, Serialize)]
#[serde(rename_all(serialize = "SCREAMING-KEBAB-CASE"), deny_unknown_fields)]
struct StructDenyUnknownFields {
#[serde(skip_deserializing)]
read_only: bool,
#[allow(dead_code)]
#[serde(skip_serializing)]
write_only: bool,
#[serde(default)]
default: bool,
#[serde(skip_serializing_if = "core::ops::Not::not")]
skip_serializing_if: bool,
#[serde(rename(serialize = "ser_renamed", deserialize = "de_renamed"))]
renamed: bool,
option: Option<bool>,
}

#[derive(JsonSchema, Deserialize, Serialize)]
struct StructAllowUnknownFields {
#[serde(flatten)]
inner: StructDenyUnknownFields,
}

#[test]
fn struct_deny_unknown_fields() {
test!(StructDenyUnknownFields)
.assert_snapshot()
.assert_allows_de_roundtrip([
json!({ "write_only": false, "skip_serializing_if": false, "de_renamed": false }),
json!({ "write_only": true, "skip_serializing_if": true, "de_renamed": true, "default": true }),
json!({ "write_only": true, "skip_serializing_if": true, "de_renamed": true, "option": true }),
])
.assert_rejects_de([
json!({ "skip_serializing_if": false, "de_renamed": false }),
json!({ "write_only": false, "de_renamed": false }),
json!({ "write_only": false, "skip_serializing_if": false }),
json!({ "write_only": true, "skip_serializing_if": true, "de_renamed": true, "unknown": true }),
])
.assert_matches_de_roundtrip(arbitrary_values());
}

#[test]
fn struct_allow_unknown_fields() {
test!(StructAllowUnknownFields)
.assert_snapshot()
.assert_allows_de_roundtrip([
json!({ "write_only": false, "skip_serializing_if": false, "de_renamed": false }),
json!({ "write_only": true, "skip_serializing_if": true, "de_renamed": true, "default": true }),
json!({ "write_only": true, "skip_serializing_if": true, "de_renamed": true, "option": true }),
json!({ "write_only": true, "skip_serializing_if": true, "de_renamed": true, "unknown": true }),
])
.assert_rejects_de([
json!({ "skip_serializing_if": false, "de_renamed": false }),
json!({ "write_only": false, "de_renamed": false }),
json!({ "write_only": false, "skip_serializing_if": false }),
])
.assert_matches_de_roundtrip(arbitrary_values());
}
2 changes: 2 additions & 0 deletions schemars/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod bound;
mod bytes;
#[cfg(feature = "chrono04")]
mod chrono;
mod contract;

mod util;

Expand All @@ -13,6 +14,7 @@ mod prelude {
pub use crate::util::{arbitrary_nonstring_values, arbitrary_values};
pub use schemars::JsonSchema;
pub use serde::{Deserialize, Serialize};
pub use serde_json::{json, Value};
}

#[macro_export]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "StructAllowUnknownFields",
"type": "object",
"properties": {
"write_only": {
"type": "boolean",
"writeOnly": true
},
"default": {
"type": "boolean",
"default": false
},
"skip_serializing_if": {
"type": "boolean"
},
"de_renamed": {
"type": "boolean"
},
"option": {
"type": [
"boolean",
"null"
]
}
},
"required": [
"write_only",
"skip_serializing_if",
"de_renamed"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "StructAllowUnknownFields",
"type": "object",
"properties": {
"READ-ONLY": {
"type": "boolean",
"readOnly": true,
"default": false
},
"DEFAULT": {
"type": "boolean",
"default": false
},
"SKIP-SERIALIZING-IF": {
"type": "boolean"
},
"ser_renamed": {
"type": "boolean"
},
"OPTION": {
"type": [
"boolean",
"null"
]
}
},
"required": [
"READ-ONLY",
"DEFAULT",
"ser_renamed",
"OPTION"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "StructDenyUnknownFields",
"type": "object",
"properties": {
"write_only": {
"type": "boolean",
"writeOnly": true
},
"default": {
"type": "boolean",
"default": false
},
"skip_serializing_if": {
"type": "boolean"
},
"de_renamed": {
"type": "boolean"
},
"option": {
"type": [
"boolean",
"null"
]
}
},
"additionalProperties": false,
"required": [
"write_only",
"skip_serializing_if",
"de_renamed"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "StructDenyUnknownFields",
"type": "object",
"properties": {
"READ-ONLY": {
"type": "boolean",
"readOnly": true,
"default": false
},
"DEFAULT": {
"type": "boolean",
"default": false
},
"SKIP-SERIALIZING-IF": {
"type": "boolean"
},
"ser_renamed": {
"type": "boolean"
},
"OPTION": {
"type": [
"boolean",
"null"
]
}
},
"additionalProperties": false,
"required": [
"READ-ONLY",
"DEFAULT",
"ser_renamed",
"OPTION"
]
}
Loading

0 comments on commit 887b369

Please sign in to comment.