Skip to content

Commit

Permalink
Port schema_settings tests to new integration test style
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 15, 2024
1 parent a927da6 commit f17d3d6
Show file tree
Hide file tree
Showing 11 changed files with 475 additions and 91 deletions.
1 change: 1 addition & 0 deletions schemars/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod schema_name;
mod schema_with;
#[cfg(feature = "semver1")]
mod semver;
mod settings;
#[cfg(feature = "smallvec1")]
mod smallvec;
#[cfg(feature = "smol_str02")]
Expand Down
68 changes: 68 additions & 0 deletions schemars/tests/integration/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::prelude::*;
use schemars::{generate::SchemaSettings, Schema};

#[derive(JsonSchema, Deserialize, Serialize, Default)]
pub struct OuterStruct {
#[schemars(extend("examples" = [8, null]))]
maybe_int: Option<i32>,
values: serde_json::Map<String, Value>,
value: Value,
inner: InnerEnum,
maybe_inner: Option<InnerEnum>,
tuples: Vec<(u8, i64)>,
}

#[derive(JsonSchema, Deserialize, Serialize, Default)]
pub enum InnerEnum {
#[default]
UndocumentedUnit1,
UndocumentedUnit2,
/// This is a documented unit variant
DocumentedUnit,
ValueNewType(Value),
}

#[test]
fn draft07() {
test!(OuterStruct, SchemaSettings::draft07())
.assert_snapshot()
.assert_allows_ser_roundtrip_default()
.assert_matches_de_roundtrip(arbitrary_values());
}

#[test]
fn draft2019_09() {
test!(OuterStruct, SchemaSettings::draft2019_09())
.assert_snapshot()
.assert_allows_ser_roundtrip_default()
.assert_matches_de_roundtrip(arbitrary_values());
}

#[test]
fn draft2020_12() {
test!(OuterStruct, SchemaSettings::draft2020_12())
.assert_snapshot()
.assert_allows_ser_roundtrip_default()
.assert_matches_de_roundtrip(arbitrary_values());
}

#[test]
fn openapi3() {
let mut settings = SchemaSettings::openapi3();
// Hack to apply recursive transforms to schemas at components.schemas:
// First, move them to $defs, then run the transforms, then move them back again.
settings.transforms.insert(
0,
Box::new(|s: &mut Schema| {
let obj = s.ensure_object();
let defs = obj["components"]["schemas"].take();
obj.insert("$defs".to_owned(), defs);
}),
);
settings.transforms.push(Box::new(|s: &mut Schema| {
let obj = s.ensure_object();
obj["components"]["schemas"] = obj.remove("$defs").unwrap();
}));

test!(OuterStruct, settings).assert_snapshot();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Outer",
"title": "OuterStruct",
"type": "object",
"properties": {
"int": {
"type": "integer",
"maybe_int": {
"type": [
"integer",
"null"
],
"format": "int32",
"examples": [
8,
Expand All @@ -17,9 +20,12 @@
},
"value": true,
"inner": {
"$ref": "#/definitions/InnerEnum"
},
"maybe_inner": {
"anyOf": [
{
"$ref": "#/definitions/Inner"
"$ref": "#/definitions/InnerEnum"
},
{
"type": "null"
Expand Down Expand Up @@ -47,13 +53,13 @@
}
},
"required": [
"int",
"values",
"value",
"inner",
"tuples"
],
"definitions": {
"Inner": {
"InnerEnum": {
"oneOf": [
{
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "OuterStruct",
"type": "object",
"properties": {
"maybe_int": {
"type": [
"integer",
"null"
],
"format": "int32",
"examples": [
8,
null
]
},
"values": {
"type": "object",
"additionalProperties": true
},
"value": true,
"inner": {
"$ref": "#/definitions/InnerEnum"
},
"maybe_inner": {
"anyOf": [
{
"$ref": "#/definitions/InnerEnum"
},
{
"type": "null"
}
]
},
"tuples": {
"type": "array",
"items": {
"type": "array",
"maxItems": 2,
"minItems": 2,
"items": [
{
"type": "integer",
"format": "uint8",
"minimum": 0
},
{
"type": "integer",
"format": "int64"
}
]
}
}
},
"required": [
"maybe_int",
"values",
"value",
"inner",
"maybe_inner",
"tuples"
],
"definitions": {
"InnerEnum": {
"oneOf": [
{
"type": "string",
"enum": [
"UndocumentedUnit1",
"UndocumentedUnit2"
]
},
{
"description": "This is a documented unit variant",
"type": "string",
"const": "DocumentedUnit"
},
{
"type": "object",
"properties": {
"ValueNewType": true
},
"required": [
"ValueNewType"
],
"additionalProperties": false
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Outer",
"title": "OuterStruct",
"type": "object",
"properties": {
"int": {
"type": "integer",
"maybe_int": {
"type": [
"integer",
"null"
],
"format": "int32",
"examples": [
8,
Expand All @@ -17,9 +20,12 @@
},
"value": true,
"inner": {
"$ref": "#/$defs/InnerEnum"
},
"maybe_inner": {
"anyOf": [
{
"$ref": "#/$defs/Inner"
"$ref": "#/$defs/InnerEnum"
},
{
"type": "null"
Expand Down Expand Up @@ -47,13 +53,13 @@
}
},
"required": [
"int",
"values",
"value",
"inner",
"tuples"
],
"$defs": {
"Inner": {
"InnerEnum": {
"oneOf": [
{
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "OuterStruct",
"type": "object",
"properties": {
"maybe_int": {
"type": [
"integer",
"null"
],
"format": "int32",
"examples": [
8,
null
]
},
"values": {
"type": "object",
"additionalProperties": true
},
"value": true,
"inner": {
"$ref": "#/$defs/InnerEnum"
},
"maybe_inner": {
"anyOf": [
{
"$ref": "#/$defs/InnerEnum"
},
{
"type": "null"
}
]
},
"tuples": {
"type": "array",
"items": {
"type": "array",
"maxItems": 2,
"minItems": 2,
"items": [
{
"type": "integer",
"format": "uint8",
"minimum": 0
},
{
"type": "integer",
"format": "int64"
}
]
}
}
},
"required": [
"maybe_int",
"values",
"value",
"inner",
"maybe_inner",
"tuples"
],
"$defs": {
"InnerEnum": {
"oneOf": [
{
"type": "string",
"enum": [
"UndocumentedUnit1",
"UndocumentedUnit2"
]
},
{
"description": "This is a documented unit variant",
"type": "string",
"const": "DocumentedUnit"
},
{
"type": "object",
"properties": {
"ValueNewType": true
},
"required": [
"ValueNewType"
],
"additionalProperties": false
}
]
}
}
}
Loading

0 comments on commit f17d3d6

Please sign in to comment.