diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a137691..74e112a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Cargo.lock b/Cargo.lock index 9cdb800d..db2b73d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -239,6 +239,9 @@ name = "either" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +dependencies = [ + "serde", +] [[package]] name = "enumset" diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index 6cf5266e..520fd25c 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -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" } @@ -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"] @@ -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"] diff --git a/schemars/src/json_schema_impls/either1.rs b/schemars/src/json_schema_impls/either1.rs index 0e9a1299..2be1684e 100644 --- a/schemars/src/json_schema_impls/either1.rs +++ b/schemars/src/json_schema_impls/either1.rs @@ -17,7 +17,28 @@ impl JsonSchema for Either { fn json_schema(generator: &mut SchemaGenerator) -> Schema { json_schema!({ - "anyOf": [generator.subschema_for::(), generator.subschema_for::()], + "oneOf": [ + { + "type": "object", + "properties": { + "Left": generator.subschema_for::() + }, + "additionalProperties": false, + "required": [ + "Left" + ] + }, + { + "type": "object", + "properties": { + "Right": generator.subschema_for::() + }, + "additionalProperties": false, + "required": [ + "Right" + ] + } + ] }) } } diff --git a/schemars/tests/either.rs b/schemars/tests/either.rs deleted file mode 100644 index 5dcd0796..00000000 --- a/schemars/tests/either.rs +++ /dev/null @@ -1,8 +0,0 @@ -mod util; -use either1::Either; -use util::*; - -#[test] -fn either() -> TestResult { - test_default_generated_schema::>>("either") -} diff --git a/schemars/tests/expected/either.json b/schemars/tests/expected/either.json deleted file mode 100644 index 18d8f517..00000000 --- a/schemars/tests/expected/either.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "Either_int32_or_Either_boolean_or_null", - "anyOf": [ - { - "type": "integer", - "format": "int32" - }, - { - "anyOf": [ - { - "type": "boolean" - }, - { - "type": "null" - } - ] - } - ] -} \ No newline at end of file diff --git a/schemars/tests/integration/either.rs b/schemars/tests/integration/either.rs new file mode 100644 index 00000000..2e5a5744 --- /dev/null +++ b/schemars/tests/integration/either.rs @@ -0,0 +1,14 @@ +use crate::prelude::*; +use either1::Either; + +#[test] +fn either() { + test!(Either>) + .assert_snapshot() + .assert_allows_ser_roundtrip([ + Either::Left(123), + Either::Right(Either::Left(true)), + Either::Right(Either::Right(())), + ]) + .assert_matches_de_roundtrip(arbitrary_values()); +} diff --git a/schemars/tests/integration/main.rs b/schemars/tests/integration/main.rs index 19529294..4d723107 100644 --- a/schemars/tests/integration/main.rs +++ b/schemars/tests/integration/main.rs @@ -14,6 +14,8 @@ mod decimal; mod default; mod deprecated; mod docs; +#[cfg(feature = "either1")] +mod either; mod prelude { pub use crate::test; diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/either.rs~either.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/either.rs~either.de.json new file mode 100644 index 00000000..b97cf1fe --- /dev/null +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/either.rs~either.de.json @@ -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" + ] + } + ] +} \ No newline at end of file diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/either.rs~either.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/either.rs~either.ser.json new file mode 100644 index 00000000..b97cf1fe --- /dev/null +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/either.rs~either.ser.json @@ -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" + ] + } + ] +} \ No newline at end of file