From 63b90f55622e9059ca1fb167f08316ab9663251d Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Mon, 9 Sep 2024 12:58:27 +0100 Subject: [PATCH] Port more enum tests to new integration test style --- schemars/tests/enum_deny_unknown_fields.rs | 127 ----------- .../expected/enum-simple-internal-duf.json | 45 ---- .../tests/expected/enum-simple-internal.json | 42 ---- schemars/tests/integration/enums.rs | 16 ++ .../integration/enums_deny_unknown_fields.rs | 208 ++++++++++++++++++ schemars/tests/integration/main.rs | 1 + ..._fields.rs~adjacently_tagged_enum.de.json} | 90 +++----- ...fields.rs~adjacently_tagged_enum.ser.json} | 109 ++------- ...n_fields.rs~externally_tagged_enum.de.json | 83 +++++++ ..._fields.rs~externally_tagged_enum.ser.json | 83 +++++++ ..._fields.rs~internally_tagged_enum.de.json} | 57 +---- ..._fields.rs~internally_tagged_enum.ser.json | 77 +++++++ ...y_unknown_fields.rs~untagged_enum.de.json} | 24 -- ...y_unknown_fields.rs~untagged_enum.ser.json | 53 +++++ 14 files changed, 575 insertions(+), 440 deletions(-) delete mode 100644 schemars/tests/enum_deny_unknown_fields.rs delete mode 100644 schemars/tests/expected/enum-simple-internal-duf.json delete mode 100644 schemars/tests/expected/enum-simple-internal.json create mode 100644 schemars/tests/integration/enums_deny_unknown_fields.rs rename schemars/tests/{expected/enum-external-duf.json => integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.de.json} (56%) rename schemars/tests/{expected/enum-adjacent-tagged-duf.json => integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.ser.json} (50%) create mode 100644 schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.de.json create mode 100644 schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.ser.json rename schemars/tests/{expected/enum-internal-duf.json => integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.de.json} (55%) create mode 100644 schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.ser.json rename schemars/tests/{expected/enum-untagged-duf.json => integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.de.json} (69%) create mode 100644 schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.ser.json diff --git a/schemars/tests/enum_deny_unknown_fields.rs b/schemars/tests/enum_deny_unknown_fields.rs deleted file mode 100644 index ef56d058..00000000 --- a/schemars/tests/enum_deny_unknown_fields.rs +++ /dev/null @@ -1,127 +0,0 @@ -mod util; -use std::collections::BTreeMap; - -use schemars::JsonSchema; -use util::*; - -// Ensure that schemars_derive uses the full path to std::string::String -pub struct String; - -#[derive(JsonSchema)] -struct UnitStruct; - -#[allow(dead_code)] -#[derive(JsonSchema)] -struct Struct { - foo: i32, - bar: bool, -} - -// Outer container should always have additionalProperties: false -// `Struct` variant should have additionalProperties: false -#[allow(dead_code)] -#[derive(JsonSchema)] -#[schemars(rename_all = "camelCase", deny_unknown_fields)] -enum External { - UnitOne, - StringMap(BTreeMap<&'static str, &'static str>), - UnitStructNewType(UnitStruct), - StructNewType(Struct), - Struct { - foo: i32, - bar: bool, - }, - UnitTwo, - Tuple(i32, bool), - #[schemars(with = "i32")] - WithInt, -} - -#[test] -fn enum_external_tag() -> TestResult { - test_default_generated_schema::("enum-external-duf") -} - -// Only `Struct` variant should have additionalProperties: false -#[allow(dead_code)] -#[derive(JsonSchema)] -#[schemars(tag = "typeProperty", deny_unknown_fields)] -enum Internal { - UnitOne, - StringMap(BTreeMap<&'static str, &'static str>), - UnitStructNewType(UnitStruct), - StructNewType(Struct), - Struct { - foo: i32, - bar: bool, - }, - UnitTwo, - #[schemars(with = "i32")] - WithInt, -} - -#[test] -fn enum_internal_tag() -> TestResult { - test_default_generated_schema::("enum-internal-duf") -} - -// Only `Struct` variant should have additionalProperties: false -#[allow(dead_code)] -#[derive(JsonSchema)] -#[schemars(untagged, deny_unknown_fields)] -enum Untagged { - UnitOne, - StringMap(BTreeMap<&'static str, &'static str>), - UnitStructNewType(UnitStruct), - StructNewType(Struct), - Struct { - foo: i32, - bar: bool, - }, - Tuple(i32, bool), - #[schemars(with = "i32")] - WithInt, -} - -#[test] -fn enum_untagged() -> TestResult { - test_default_generated_schema::("enum-untagged-duf") -} - -// Outer container and `Struct` variant should have additionalProperties: false -#[allow(dead_code)] -#[derive(JsonSchema)] -#[schemars(tag = "t", content = "c", deny_unknown_fields)] -enum Adjacent { - UnitOne, - StringMap(BTreeMap<&'static str, &'static str>), - UnitStructNewType(UnitStruct), - StructNewType(Struct), - Struct { - foo: i32, - bar: bool, - }, - Tuple(i32, bool), - UnitTwo, - #[schemars(with = "i32")] - WithInt, -} - -#[test] -fn enum_adjacent_tagged() -> TestResult { - test_default_generated_schema::("enum-adjacent-tagged-duf") -} - -#[allow(dead_code)] -#[derive(JsonSchema)] -#[schemars(tag = "typeProperty", deny_unknown_fields)] -enum SimpleInternal { - A, - B, - C, -} - -#[test] -fn enum_simple_internal_tag() -> TestResult { - test_default_generated_schema::("enum-simple-internal-duf") -} diff --git a/schemars/tests/expected/enum-simple-internal-duf.json b/schemars/tests/expected/enum-simple-internal-duf.json deleted file mode 100644 index aaf1eef8..00000000 --- a/schemars/tests/expected/enum-simple-internal-duf.json +++ /dev/null @@ -1,45 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "SimpleInternal", - "oneOf": [ - { - "type": "object", - "properties": { - "typeProperty": { - "type": "string", - "const": "A" - } - }, - "required": [ - "typeProperty" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "typeProperty": { - "type": "string", - "const": "B" - } - }, - "required": [ - "typeProperty" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "typeProperty": { - "type": "string", - "const": "C" - } - }, - "required": [ - "typeProperty" - ], - "additionalProperties": false - } - ] -} \ No newline at end of file diff --git a/schemars/tests/expected/enum-simple-internal.json b/schemars/tests/expected/enum-simple-internal.json deleted file mode 100644 index e955d2af..00000000 --- a/schemars/tests/expected/enum-simple-internal.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "SimpleInternal", - "oneOf": [ - { - "type": "object", - "properties": { - "typeProperty": { - "type": "string", - "const": "A" - } - }, - "required": [ - "typeProperty" - ] - }, - { - "type": "object", - "properties": { - "typeProperty": { - "type": "string", - "const": "B" - } - }, - "required": [ - "typeProperty" - ] - }, - { - "type": "object", - "properties": { - "typeProperty": { - "type": "string", - "const": "C" - } - }, - "required": [ - "typeProperty" - ] - } - ] -} \ No newline at end of file diff --git a/schemars/tests/integration/enums.rs b/schemars/tests/integration/enums.rs index bd223814..2329be0a 100644 --- a/schemars/tests/integration/enums.rs +++ b/schemars/tests/integration/enums.rs @@ -42,6 +42,10 @@ impl External { .collect(), ), Self::UnitStructNewType(UnitStruct), + Self::StructNewType(Struct { + foo: 123, + bar: true, + }), Self::Struct { foo: 123, bar: true, @@ -85,6 +89,10 @@ impl Internal { .collect(), ), Self::UnitStructNewType(UnitStruct), + Self::StructNewType(Struct { + foo: 123, + bar: true, + }), Self::Struct { foo: 123, bar: true, @@ -128,6 +136,10 @@ impl Adjacent { .collect(), ), Self::UnitStructNewType(UnitStruct), + Self::StructNewType(Struct { + foo: 123, + bar: true, + }), Self::Struct { foo: 123, bar: true, @@ -171,6 +183,10 @@ impl Untagged { .collect(), ), Self::UnitStructNewType(UnitStruct), + Self::StructNewType(Struct { + foo: 123, + bar: true, + }), Self::Struct { foo: 123, bar: true, diff --git a/schemars/tests/integration/enums_deny_unknown_fields.rs b/schemars/tests/integration/enums_deny_unknown_fields.rs new file mode 100644 index 00000000..eb1de3ca --- /dev/null +++ b/schemars/tests/integration/enums_deny_unknown_fields.rs @@ -0,0 +1,208 @@ +use std::collections::BTreeMap; + +use crate::prelude::*; + +#[derive(JsonSchema, Deserialize, Serialize, Default)] +struct Struct { + foo: i32, + bar: bool, +} + +#[derive(JsonSchema, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +enum External { + Unit, + StringMap(BTreeMap), + StructNewType(Struct), + Struct { foo: i32, bar: bool }, +} + +impl External { + fn values() -> impl IntoIterator { + [ + 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)] +#[serde(tag = "tag", deny_unknown_fields)] +enum Internal { + Unit, + StringMap(BTreeMap), + StructNewType(Struct), + Struct { foo: i32, bar: bool }, +} + +impl Internal { + fn values() -> impl IntoIterator { + [ + 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)] +#[serde(tag = "tag", content = "content", deny_unknown_fields)] +enum Adjacent { + Unit, + StringMap(BTreeMap), + StructNewType(Struct), + Struct { foo: i32, bar: bool }, +} + +impl Adjacent { + fn values() -> impl IntoIterator { + [ + 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)] +#[serde(untagged, deny_unknown_fields)] +enum Untagged { + Unit, + StringMap(BTreeMap), + StructNewType(Struct), + Struct { foo: i32, bar: bool }, +} + +impl Untagged { + fn values() -> impl IntoIterator { + [ + 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, + }, + ] + } +} + +#[test] +fn externally_tagged_enum() { + test!(External) + .assert_snapshot() + .assert_allows_ser_roundtrip(External::values()) + .assert_matches_de_roundtrip(arbitrary_values()) + .assert_rejects_de([json!({ + "Struct": { + "foo": 123, + "bar": true, + "extra": null + } + })]) + .assert_allows_de_roundtrip([json!({ + "StructNewType": { + "foo": 123, + "bar": true, + "extra": null + } + })]); +} + +#[test] +fn internally_tagged_enum() { + test!(Internal) + .assert_snapshot() + .assert_allows_ser_roundtrip(Internal::values()) + .assert_matches_de_roundtrip(arbitrary_values()) + .assert_rejects_de([json!({ + "tag": "Struct", + "foo": 123, + "bar": true, + "extra": null + })]) + .assert_allows_de_roundtrip([json!({ + "tag": "StructNewType", + "foo": 123, + "bar": true, + "extra": null + })]); +} + +#[test] +fn adjacently_tagged_enum() { + test!(Adjacent) + .assert_snapshot() + .assert_allows_ser_roundtrip(Adjacent::values()) + .assert_matches_de_roundtrip(arbitrary_values()) + .assert_rejects_de([json!({ + "tag": "Struct", + "content": { + "foo": 123, + "bar": true, + "extra": null + } + })]) + .assert_allows_de_roundtrip([json!({ + "tag": "StructNewType", + "content": { + "foo": 123, + "bar": true, + "extra": null + } + })]); +} + +#[test] +fn untagged_enum() { + test!(Untagged) + .assert_snapshot() + .assert_allows_ser_roundtrip(Untagged::values()) + .assert_matches_de_roundtrip(arbitrary_values()) + .assert_allows_de_roundtrip([json!({ + "foo": 123, + "bar": true, + "extra": null + })]); +} diff --git a/schemars/tests/integration/main.rs b/schemars/tests/integration/main.rs index ade7ce32..2cfded98 100644 --- a/schemars/tests/integration/main.rs +++ b/schemars/tests/integration/main.rs @@ -18,6 +18,7 @@ mod docs; mod either; mod enum_repr; mod enums; +mod enums_deny_unknown_fields; mod prelude { pub use crate::test; diff --git a/schemars/tests/expected/enum-external-duf.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.de.json similarity index 56% rename from schemars/tests/expected/enum-external-duf.json rename to schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.de.json index 76be5b3f..20f2aed4 100644 --- a/schemars/tests/expected/enum-external-duf.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.de.json @@ -1,57 +1,65 @@ { "$schema": "https://json-schema.org/draft/2020-12/schema", - "title": "External", + "title": "Adjacent", "oneOf": [ - { - "type": "string", - "enum": [ - "unitOne", - "unitTwo" - ] - }, { "type": "object", "properties": { - "stringMap": { - "type": "object", - "additionalProperties": { - "type": "string" - } + "tag": { + "type": "string", + "const": "Unit" } }, "required": [ - "stringMap" + "tag" ], "additionalProperties": false }, { "type": "object", "properties": { - "unitStructNewType": { - "$ref": "#/$defs/UnitStruct" + "tag": { + "type": "string", + "const": "StringMap" + }, + "content": { + "type": "object", + "additionalProperties": { + "type": "string" + } } }, "required": [ - "unitStructNewType" + "tag", + "content" ], "additionalProperties": false }, { "type": "object", "properties": { - "structNewType": { + "tag": { + "type": "string", + "const": "StructNewType" + }, + "content": { "$ref": "#/$defs/Struct" } }, "required": [ - "structNewType" + "tag", + "content" ], "additionalProperties": false }, { "type": "object", "properties": { - "struct": { + "tag": { + "type": "string", + "const": "Struct" + }, + "content": { "type": "object", "properties": { "foo": { @@ -70,51 +78,13 @@ } }, "required": [ - "struct" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "tuple": { - "type": "array", - "prefixItems": [ - { - "type": "integer", - "format": "int32" - }, - { - "type": "boolean" - } - ], - "minItems": 2, - "maxItems": 2 - } - }, - "required": [ - "tuple" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "withInt": { - "type": "integer", - "format": "int32" - } - }, - "required": [ - "withInt" + "tag", + "content" ], "additionalProperties": false } ], "$defs": { - "UnitStruct": { - "type": "null" - }, "Struct": { "type": "object", "properties": { diff --git a/schemars/tests/expected/enum-adjacent-tagged-duf.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.ser.json similarity index 50% rename from schemars/tests/expected/enum-adjacent-tagged-duf.json rename to schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.ser.json index b181f557..20f2aed4 100644 --- a/schemars/tests/expected/enum-adjacent-tagged-duf.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~adjacently_tagged_enum.ser.json @@ -5,24 +5,24 @@ { "type": "object", "properties": { - "t": { + "tag": { "type": "string", - "const": "UnitOne" + "const": "Unit" } }, "required": [ - "t" + "tag" ], "additionalProperties": false }, { "type": "object", "properties": { - "t": { + "tag": { "type": "string", "const": "StringMap" }, - "c": { + "content": { "type": "object", "additionalProperties": { "type": "string" @@ -30,53 +30,36 @@ } }, "required": [ - "t", - "c" + "tag", + "content" ], "additionalProperties": false }, { "type": "object", "properties": { - "t": { - "type": "string", - "const": "UnitStructNewType" - }, - "c": { - "$ref": "#/$defs/UnitStruct" - } - }, - "required": [ - "t", - "c" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "t": { + "tag": { "type": "string", "const": "StructNewType" }, - "c": { + "content": { "$ref": "#/$defs/Struct" } }, "required": [ - "t", - "c" + "tag", + "content" ], "additionalProperties": false }, { "type": "object", "properties": { - "t": { + "tag": { "type": "string", "const": "Struct" }, - "c": { + "content": { "type": "object", "properties": { "foo": { @@ -95,75 +78,13 @@ } }, "required": [ - "t", - "c" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "t": { - "type": "string", - "const": "Tuple" - }, - "c": { - "type": "array", - "prefixItems": [ - { - "type": "integer", - "format": "int32" - }, - { - "type": "boolean" - } - ], - "minItems": 2, - "maxItems": 2 - } - }, - "required": [ - "t", - "c" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "t": { - "type": "string", - "const": "UnitTwo" - } - }, - "required": [ - "t" - ], - "additionalProperties": false - }, - { - "type": "object", - "properties": { - "t": { - "type": "string", - "const": "WithInt" - }, - "c": { - "type": "integer", - "format": "int32" - } - }, - "required": [ - "t", - "c" + "tag", + "content" ], "additionalProperties": false } ], "$defs": { - "UnitStruct": { - "type": "null" - }, "Struct": { "type": "object", "properties": { diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.de.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.de.json new file mode 100644 index 00000000..819e0e46 --- /dev/null +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.de.json @@ -0,0 +1,83 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "External", + "oneOf": [ + { + "type": "string", + "enum": [ + "Unit" + ] + }, + { + "type": "object", + "properties": { + "StringMap": { + "type": "object", + "additionalProperties": { + "type": "string" + } + } + }, + "required": [ + "StringMap" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "StructNewType": { + "$ref": "#/$defs/Struct" + } + }, + "required": [ + "StructNewType" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "Struct": { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, + "bar": { + "type": "boolean" + } + }, + "additionalProperties": false, + "required": [ + "foo", + "bar" + ] + } + }, + "required": [ + "Struct" + ], + "additionalProperties": false + } + ], + "$defs": { + "Struct": { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, + "bar": { + "type": "boolean" + } + }, + "required": [ + "foo", + "bar" + ] + } + } +} \ No newline at end of file diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.ser.json new file mode 100644 index 00000000..819e0e46 --- /dev/null +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~externally_tagged_enum.ser.json @@ -0,0 +1,83 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "External", + "oneOf": [ + { + "type": "string", + "enum": [ + "Unit" + ] + }, + { + "type": "object", + "properties": { + "StringMap": { + "type": "object", + "additionalProperties": { + "type": "string" + } + } + }, + "required": [ + "StringMap" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "StructNewType": { + "$ref": "#/$defs/Struct" + } + }, + "required": [ + "StructNewType" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "Struct": { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, + "bar": { + "type": "boolean" + } + }, + "additionalProperties": false, + "required": [ + "foo", + "bar" + ] + } + }, + "required": [ + "Struct" + ], + "additionalProperties": false + } + ], + "$defs": { + "Struct": { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, + "bar": { + "type": "boolean" + } + }, + "required": [ + "foo", + "bar" + ] + } + } +} \ No newline at end of file diff --git a/schemars/tests/expected/enum-internal-duf.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.de.json similarity index 55% rename from schemars/tests/expected/enum-internal-duf.json rename to schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.de.json index 73e4743b..2e5b8c88 100644 --- a/schemars/tests/expected/enum-internal-duf.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.de.json @@ -5,20 +5,20 @@ { "type": "object", "properties": { - "typeProperty": { + "tag": { "type": "string", - "const": "UnitOne" + "const": "Unit" } }, "required": [ - "typeProperty" + "tag" ], "additionalProperties": false }, { "type": "object", "properties": { - "typeProperty": { + "tag": { "type": "string", "const": "StringMap" } @@ -27,22 +27,9 @@ "type": "string" }, "required": [ - "typeProperty" + "tag" ] }, - { - "type": "object", - "properties": { - "typeProperty": { - "type": "string", - "const": "UnitStructNewType" - } - }, - "required": [ - "typeProperty" - ], - "additionalProperties": false - }, { "type": "object", "properties": { @@ -53,13 +40,13 @@ "bar": { "type": "boolean" }, - "typeProperty": { + "tag": { "type": "string", "const": "StructNewType" } }, "required": [ - "typeProperty", + "tag", "foo", "bar" ] @@ -74,43 +61,17 @@ "bar": { "type": "boolean" }, - "typeProperty": { + "tag": { "type": "string", "const": "Struct" } }, "additionalProperties": false, "required": [ - "typeProperty", + "tag", "foo", "bar" ] - }, - { - "type": "object", - "properties": { - "typeProperty": { - "type": "string", - "const": "UnitTwo" - } - }, - "required": [ - "typeProperty" - ], - "additionalProperties": false - }, - { - "type": "object", - "format": "int32", - "properties": { - "typeProperty": { - "type": "string", - "const": "WithInt" - } - }, - "required": [ - "typeProperty" - ] } ] } \ No newline at end of file diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.ser.json new file mode 100644 index 00000000..2e5b8c88 --- /dev/null +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~internally_tagged_enum.ser.json @@ -0,0 +1,77 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Internal", + "oneOf": [ + { + "type": "object", + "properties": { + "tag": { + "type": "string", + "const": "Unit" + } + }, + "required": [ + "tag" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "tag": { + "type": "string", + "const": "StringMap" + } + }, + "additionalProperties": { + "type": "string" + }, + "required": [ + "tag" + ] + }, + { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, + "bar": { + "type": "boolean" + }, + "tag": { + "type": "string", + "const": "StructNewType" + } + }, + "required": [ + "tag", + "foo", + "bar" + ] + }, + { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, + "bar": { + "type": "boolean" + }, + "tag": { + "type": "string", + "const": "Struct" + } + }, + "additionalProperties": false, + "required": [ + "tag", + "foo", + "bar" + ] + } + ] +} \ No newline at end of file diff --git a/schemars/tests/expected/enum-untagged-duf.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.de.json similarity index 69% rename from schemars/tests/expected/enum-untagged-duf.json rename to schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.de.json index 58bdbe16..9b581362 100644 --- a/schemars/tests/expected/enum-untagged-duf.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.de.json @@ -11,9 +11,6 @@ "type": "string" } }, - { - "$ref": "#/$defs/UnitStruct" - }, { "$ref": "#/$defs/Struct" }, @@ -33,30 +30,9 @@ "foo", "bar" ] - }, - { - "type": "array", - "prefixItems": [ - { - "type": "integer", - "format": "int32" - }, - { - "type": "boolean" - } - ], - "minItems": 2, - "maxItems": 2 - }, - { - "type": "integer", - "format": "int32" } ], "$defs": { - "UnitStruct": { - "type": "null" - }, "Struct": { "type": "object", "properties": { diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.ser.json new file mode 100644 index 00000000..9b581362 --- /dev/null +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/enums_deny_unknown_fields.rs~untagged_enum.ser.json @@ -0,0 +1,53 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Untagged", + "anyOf": [ + { + "type": "null" + }, + { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + { + "$ref": "#/$defs/Struct" + }, + { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, + "bar": { + "type": "boolean" + } + }, + "additionalProperties": false, + "required": [ + "foo", + "bar" + ] + } + ], + "$defs": { + "Struct": { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, + "bar": { + "type": "boolean" + } + }, + "required": [ + "foo", + "bar" + ] + } + } +} \ No newline at end of file