Skip to content

Commit

Permalink
Port "either" tests to new integration test style
Browse files Browse the repository at this point in the history
Also fix a bug that the new test found!
  • Loading branch information
GREsau committed Sep 9, 2024
1 parent e7637bd commit 8ea9e25
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixed

- The "deserialize" schema for `bytes::Bytes`/`BytesMut` now allows strings, matching the actual deserialize behaviour of the types.
- The schema for `either::Either` now matches the actual serialize/deserialize behaviour of that type.

## [1.0.0-alpha.15] - 2024-09-05

Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ arrayvec07 = { version = "0.7", default-features = false, optional = true, packa
bigdecimal04 = { version = "0.4", default-features = false, optional = true, package = "bigdecimal" }
bytes1 = { version = "1.0", default-features = false, optional = true, package = "bytes" }
chrono04 = { version = "0.4", default-features = false, optional = true, package = "chrono" }
either1 = { version = "1.3", default-features = false, optional = true, package = "either" }
either1 = { version = "1.3", default-features = false, optional = true, package = "either" }
enumset1 = { version = "1.0", default-features = false, optional = true, package = "enumset" }
indexmap2 = { version = "2.0", default-features = false, optional = true, package = "indexmap" }
rust_decimal1 = { version = "1", default-features = false, optional = true, package = "rust_decimal" }
Expand All @@ -46,6 +46,7 @@ bytes1 = { version = "1.0", default-features = false, features = ["serde"], pack
chrono04 = { version = "0.4", default-features = false, features = ["serde"], package = "chrono" }
bigdecimal04 = { version = "0.4", default-features = false, features = ["serde"], package = "bigdecimal" }
rust_decimal1 = { version = "1", default-features = false, features = ["serde"], package = "rust_decimal" }
either1 = { version = "1.3", default-features = false, features = ["serde"], package = "either" }

[features]
default = ["derive", "std"]
Expand Down Expand Up @@ -84,10 +85,6 @@ required-features = ["_ui_test"]
name = "indexmap"
required-features = ["indexmap2"]

[[test]]
name = "either"
required-features = ["either1"]

[[test]]
name = "uuid"
required-features = ["uuid1"]
Expand Down
23 changes: 22 additions & 1 deletion schemars/src/json_schema_impls/either1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,28 @@ impl<L: JsonSchema, R: JsonSchema> JsonSchema for Either<L, R> {

fn json_schema(generator: &mut SchemaGenerator) -> Schema {
json_schema!({
"anyOf": [generator.subschema_for::<L>(), generator.subschema_for::<R>()],
"oneOf": [
{
"type": "object",
"properties": {
"Left": generator.subschema_for::<L>()
},
"additionalProperties": false,
"required": [
"Left"
]
},
{
"type": "object",
"properties": {
"Right": generator.subschema_for::<R>()
},
"additionalProperties": false,
"required": [
"Right"
]
}
]
})
}
}
8 changes: 0 additions & 8 deletions schemars/tests/either.rs

This file was deleted.

20 changes: 0 additions & 20 deletions schemars/tests/expected/either.json

This file was deleted.

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

#[test]
fn either() {
test!(Either<i32, Either<bool, ()>>)
.assert_snapshot()
.assert_allows_ser_roundtrip([
Either::Left(123),
Either::Right(Either::Left(true)),
Either::Right(Either::Right(())),
])
.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 @@ -14,6 +14,8 @@ mod decimal;
mod default;
mod deprecated;
mod docs;
#[cfg(feature = "either1")]
mod either;

mod prelude {
pub use crate::test;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Either_int32_or_Either_boolean_or_null",
"oneOf": [
{
"type": "object",
"properties": {
"Left": {
"type": "integer",
"format": "int32"
}
},
"additionalProperties": false,
"required": [
"Left"
]
},
{
"type": "object",
"properties": {
"Right": {
"oneOf": [
{
"type": "object",
"properties": {
"Left": {
"type": "boolean"
}
},
"additionalProperties": false,
"required": [
"Left"
]
},
{
"type": "object",
"properties": {
"Right": {
"type": "null"
}
},
"additionalProperties": false,
"required": [
"Right"
]
}
]
}
},
"additionalProperties": false,
"required": [
"Right"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Either_int32_or_Either_boolean_or_null",
"oneOf": [
{
"type": "object",
"properties": {
"Left": {
"type": "integer",
"format": "int32"
}
},
"additionalProperties": false,
"required": [
"Left"
]
},
{
"type": "object",
"properties": {
"Right": {
"oneOf": [
{
"type": "object",
"properties": {
"Left": {
"type": "boolean"
}
},
"additionalProperties": false,
"required": [
"Left"
]
},
{
"type": "object",
"properties": {
"Right": {
"type": "null"
}
},
"additionalProperties": false,
"required": [
"Right"
]
}
]
}
},
"additionalProperties": false,
"required": [
"Right"
]
}
]
}

0 comments on commit 8ea9e25

Please sign in to comment.