-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port
transform
tests to new integration test style
- Loading branch information
Showing
7 changed files
with
123 additions
and
56 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
25 changes: 25 additions & 0 deletions
25
...sts/integration/snapshots/schemars/tests/integration/transform.rs~transform_enum.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,25 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "External", | ||
"oneOf": [ | ||
{ | ||
"type": "string", | ||
"const": "Unit", | ||
"x-propertyCount": 0, | ||
"x-upperType": "STRING" | ||
}, | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"NewType": true | ||
}, | ||
"required": [ | ||
"NewType" | ||
], | ||
"additionalProperties": false, | ||
"x-propertyCount": 1, | ||
"x-upperType": "OBJECT" | ||
} | ||
], | ||
"x-propertyCount": 0 | ||
} |
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
20 changes: 20 additions & 0 deletions
20
...s/integration/snapshots/schemars/tests/integration/transform.rs~transform_struct.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,20 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "Struct", | ||
"type": "object", | ||
"properties": { | ||
"value": true, | ||
"int": { | ||
"type": "integer", | ||
"format": "int32", | ||
"x-propertyCount": 0, | ||
"x-upperType": "INTEGER" | ||
} | ||
}, | ||
"required": [ | ||
"value", | ||
"int" | ||
], | ||
"x-upperType": "OBJECT", | ||
"x-propertyCount": 2 | ||
} |
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,68 @@ | ||
use crate::prelude::*; | ||
use schemars::{ | ||
transform::{RecursiveTransform, Transform}, | ||
Schema, | ||
}; | ||
use serde_json::Map; | ||
|
||
fn insert_upper_type(schema: &mut Schema) { | ||
if let Some(Value::String(ty)) = schema.get("type") { | ||
schema.insert("x-upperType".to_owned(), ty.to_uppercase().into()); | ||
} | ||
} | ||
|
||
fn insert_property_count(schema: &mut Schema) { | ||
let count = schema | ||
.get("properties") | ||
.and_then(Value::as_object) | ||
.map_or(0, Map::len); | ||
schema.insert("x-propertyCount".to_owned(), count.into()); | ||
} | ||
|
||
#[derive(JsonSchema, Deserialize, Serialize, Default)] | ||
#[schemars(transform = RecursiveTransform(insert_upper_type), transform = insert_property_count)] | ||
struct Struct { | ||
value: Value, | ||
#[schemars(transform = insert_property_count)] | ||
int: i32, | ||
} | ||
|
||
#[test] | ||
fn transform_struct() { | ||
test!(Struct) | ||
.assert_snapshot() | ||
.assert_allows_ser_roundtrip_default() | ||
.custom(assert_upper_type_valid); | ||
} | ||
|
||
#[derive(JsonSchema, Deserialize, Serialize, Default)] | ||
#[schemars(transform = RecursiveTransform(insert_upper_type), transform = insert_property_count)] | ||
enum External { | ||
#[default] | ||
#[schemars(transform = insert_property_count)] | ||
Unit, | ||
#[schemars(transform = insert_property_count)] | ||
NewType(Value), | ||
} | ||
|
||
#[test] | ||
fn transform_enum() { | ||
test!(External) | ||
.assert_snapshot() | ||
.assert_allows_ser_roundtrip_default() | ||
.custom(assert_upper_type_valid); | ||
} | ||
|
||
fn assert_upper_type_valid(schema: &Schema, _: schemars::generate::Contract) { | ||
let mut schema = schema.clone(); | ||
|
||
RecursiveTransform(|s: &mut Schema| { | ||
assert_eq!( | ||
s.remove("x-upperType").map(|v| v.to_string()), | ||
s.get("type").map(|v| v.to_string().to_uppercase()), | ||
); | ||
}) | ||
.transform(&mut schema); | ||
|
||
assert!(!schema.to_value().to_string().contains("x-upperType")); | ||
} |
This file was deleted.
Oops, something went wrong.