Skip to content

Commit

Permalink
Port enum flatten tests to new integration test style
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 9, 2024
1 parent cc2e400 commit a25af76
Show file tree
Hide file tree
Showing 12 changed files with 675 additions and 197 deletions.
2 changes: 1 addition & 1 deletion schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ uuid1 = { version = "1.0", default-features = false, optional = true, package =
pretty_assertions = "1.2.1"
trybuild = "1.0"
serde = { version = "1.0", features = ["derive"] }
jsonschema = { version = "0.18.1", default-features = false }
jsonschema = { version = "0.18.1", default-features = false, features = ["draft201909", "draft202012"] }
snapbox = { version = "0.6.17", features = ["json"] }

arrayvec07 = { version = "0.7", default-features = false, features = ["serde"], package = "arrayvec"}
Expand Down
89 changes: 0 additions & 89 deletions schemars/tests/enum_flatten.rs

This file was deleted.

100 changes: 27 additions & 73 deletions schemars/tests/integration/enums_deny_unknown_fields.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
use crate::prelude::*;
use std::collections::BTreeMap;

use crate::prelude::*;
macro_rules! fn_values {
() => {
fn values() -> impl IntoIterator<Item = Self> {
[
Self::Unit,
Self::StringMap(
[("hello".to_owned(), "world".to_owned())]
.into_iter()
.collect(),
),
Self::StructNewType(Struct {
foo: 123,
bar: true,
}),
Self::Struct {
foo: 123,
bar: true,
},
]
}
};
}

#[derive(JsonSchema, Deserialize, Serialize, Default)]
struct Struct {
Expand All @@ -18,24 +40,7 @@ enum External {
}

impl External {
fn values() -> impl IntoIterator<Item = Self> {
[
Self::Unit,
Self::StringMap(
[("hello".to_owned(), "world".to_owned())]
.into_iter()
.collect(),
),
Self::StructNewType(Struct {
foo: 123,
bar: true,
}),
Self::Struct {
foo: 123,
bar: true,
},
]
}
fn_values!();
}

#[derive(JsonSchema, Deserialize, Serialize)]
Expand All @@ -48,24 +53,7 @@ enum Internal {
}

impl Internal {
fn values() -> impl IntoIterator<Item = Self> {
[
Self::Unit,
Self::StringMap(
[("hello".to_owned(), "world".to_owned())]
.into_iter()
.collect(),
),
Self::StructNewType(Struct {
foo: 123,
bar: true,
}),
Self::Struct {
foo: 123,
bar: true,
},
]
}
fn_values!();
}

#[derive(JsonSchema, Deserialize, Serialize)]
Expand All @@ -78,24 +66,7 @@ enum Adjacent {
}

impl Adjacent {
fn values() -> impl IntoIterator<Item = Self> {
[
Self::Unit,
Self::StringMap(
[("hello".to_owned(), "world".to_owned())]
.into_iter()
.collect(),
),
Self::StructNewType(Struct {
foo: 123,
bar: true,
}),
Self::Struct {
foo: 123,
bar: true,
},
]
}
fn_values!();
}

#[derive(JsonSchema, Deserialize, Serialize)]
Expand All @@ -108,24 +79,7 @@ enum Untagged {
}

impl Untagged {
fn values() -> impl IntoIterator<Item = Self> {
[
Self::Unit,
Self::StringMap(
[("hello".to_owned(), "world".to_owned())]
.into_iter()
.collect(),
),
Self::StructNewType(Struct {
foo: 123,
bar: true,
}),
Self::Struct {
foo: 123,
bar: true,
},
]
}
fn_values!();
}

#[test]
Expand Down
136 changes: 136 additions & 0 deletions schemars/tests/integration/enums_flattened.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use schemars::generate::SchemaSettings;

use crate::prelude::*;

macro_rules! fn_values {
() => {
fn values() -> impl IntoIterator<Item = Self> {
[
Self {
f: 1.23,
e1: Enum1::B(true),
e2: Enum2::F(4.56),
e3: Enum3::S2("abc".into()),
e4: Enum4::U2(789),
e5: Enum5::B3(false),
},
Self {
f: 9.87,
e1: Enum1::S("def".into()),
e2: Enum2::U(654),
e3: Enum3::B2(true),
e4: Enum4::F2(3.21),
e5: Enum5::S3("ghi".into()),
},
]
}
};
}

#[derive(JsonSchema, Deserialize, Serialize)]
enum Enum1 {
B(bool),
S(String),
}

#[derive(JsonSchema, Deserialize, Serialize)]
enum Enum2 {
U(u32),
F(f64),
}

#[derive(JsonSchema, Deserialize, Serialize)]
enum Enum3 {
B2(bool),
S2(String),
}

#[derive(JsonSchema, Deserialize, Serialize)]
enum Enum4 {
U2(u32),
F2(f64),
}

#[derive(JsonSchema, Deserialize, Serialize)]
enum Enum5 {
B3(bool),
S3(String),
}

#[derive(JsonSchema, Deserialize, Serialize)]
struct Container {
f: f32,
#[serde(flatten)]
e1: Enum1,
#[serde(flatten)]
e2: Enum2,
#[serde(flatten)]
e3: Enum3,
#[serde(flatten)]
e4: Enum4,
#[serde(flatten)]
e5: Enum5,
}

impl Container {
fn_values!();
}

#[derive(JsonSchema, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct ContainerDenyUnknownFields {
f: f32,
#[serde(flatten)]
e1: Enum1,
#[serde(flatten)]
e2: Enum2,
#[serde(flatten)]
e3: Enum3,
#[serde(flatten)]
e4: Enum4,
#[serde(flatten)]
e5: Enum5,
}

impl ContainerDenyUnknownFields {
fn_values!();
}

fn json_with_extra_field() -> Value {
json!({
"f": 1.23,
"B": true,
"F": 4.56,
"S2": "abc",
"U2": 789,
"B3": false,
"extra": null
})
}

#[test]
fn enums_flattened() {
test!(Container)
.assert_snapshot()
.assert_allows_ser_roundtrip(Container::values())
.assert_matches_de_roundtrip(arbitrary_values())
.assert_allows_de_roundtrip([json_with_extra_field()]);
}

#[test]
fn enums_flattened_deny_unknown_fields() {
test!(ContainerDenyUnknownFields)
.assert_snapshot()
.assert_allows_ser_roundtrip(ContainerDenyUnknownFields::values())
.assert_matches_de_roundtrip(arbitrary_values())
.assert_rejects_de([json_with_extra_field()]);
}

#[test]
fn enums_flattened_deny_unknown_fields_draft07() {
test!(ContainerDenyUnknownFields, SchemaSettings::draft07())
.assert_snapshot()
.assert_allows_ser_roundtrip(ContainerDenyUnknownFields::values())
.assert_matches_de_roundtrip(arbitrary_values())
.assert_rejects_de([json_with_extra_field()]);
}
8 changes: 6 additions & 2 deletions schemars/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod either;
mod enum_repr;
mod enums;
mod enums_deny_unknown_fields;
mod enums_flattened;

mod prelude {
pub use crate::test;
Expand All @@ -32,7 +33,7 @@ mod test_helper;

#[macro_export]
macro_rules! test {
($type:ty) => {
($type:ty, $settings:expr) => {
$crate::test_helper::TestHelper::<$type>::new(
{
fn f() {}
Expand All @@ -47,7 +48,10 @@ macro_rules! test {

format!("{}~{}", core::file!(), test_name)
},
schemars::generate::SchemaSettings::default(),
$settings,
)
};
($type:ty) => {
test!($type, schemars::generate::SchemaSettings::default())
};
}
Loading

0 comments on commit a25af76

Please sign in to comment.