-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
284 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...tion/snapshots/schemars/tests/integration/contract.rs~struct_allow_unknown_fields.de.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
34 changes: 34 additions & 0 deletions
34
...ion/snapshots/schemars/tests/integration/contract.rs~struct_allow_unknown_fields.ser.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
33 changes: 33 additions & 0 deletions
33
...ation/snapshots/schemars/tests/integration/contract.rs~struct_deny_unknown_fields.de.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
35 changes: 35 additions & 0 deletions
35
...tion/snapshots/schemars/tests/integration/contract.rs~struct_deny_unknown_fields.ser.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
Oops, something went wrong.