Skip to content

Commit

Permalink
Port transform tests to new integration test style
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 12, 2024
1 parent 828ec95 commit c773b39
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 56 deletions.
1 change: 1 addition & 0 deletions schemars/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod smallvec;
#[cfg(feature = "smol_str02")]
mod smol_str;
mod std_types;
mod transform;
#[cfg(feature = "url2")]
mod url;
#[cfg(feature = "uuid1")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"type": "string",
"const": "Unit",
"propertyCount": 0,
"upperType": "STRING"
"x-propertyCount": 0,
"x-upperType": "STRING"
},
{
"type": "object",
Expand All @@ -17,9 +17,9 @@
"NewType"
],
"additionalProperties": false,
"propertyCount": 1,
"upperType": "OBJECT"
"x-propertyCount": 1,
"x-upperType": "OBJECT"
}
],
"propertyCount": 0
"x-propertyCount": 0
}
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"int": {
"type": "integer",
"format": "int32",
"propertyCount": 0,
"upperType": "INTEGER"
"x-propertyCount": 0,
"x-upperType": "INTEGER"
}
},
"required": [
"value",
"int"
],
"upperType": "OBJECT",
"propertyCount": 2
"x-upperType": "OBJECT",
"x-propertyCount": 2
}
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
}
68 changes: 68 additions & 0 deletions schemars/tests/integration/transform.rs
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"));
}
47 changes: 0 additions & 47 deletions schemars/tests/transform.rs

This file was deleted.

0 comments on commit c773b39

Please sign in to comment.