-
-
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.
Port
schema_settings
tests to new integration test style
- Loading branch information
Showing
11 changed files
with
475 additions
and
91 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
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(); | ||
} |
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
91 changes: 91 additions & 0 deletions
91
schemars/tests/integration/snapshots/schemars/tests/integration/settings.rs~draft07.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,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 | ||
} | ||
] | ||
} | ||
} | ||
} |
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
91 changes: 91 additions & 0 deletions
91
.../tests/integration/snapshots/schemars/tests/integration/settings.rs~draft2019_09.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,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 | ||
} | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.