Skip to content

Commit

Permalink
Add more tests for different schema settings
Browse files Browse the repository at this point in the history
This is mainly to keep this coverage around in v1, where the `schema_for_schema` tests are no longer viable
  • Loading branch information
GREsau committed May 13, 2024
1 parent 7f6a7b7 commit 449bb1a
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 0 deletions.
65 changes: 65 additions & 0 deletions schemars/tests/expected/schema_settings-2019_09.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Outer",
"type": "object",
"required": [
"int",
"value",
"values"
],
"properties": {
"int": {
"examples": [
8,
null
],
"type": "integer",
"format": "int32"
},
"values": {
"type": "object",
"additionalProperties": true
},
"value": true,
"inner": {
"anyOf": [
{
"$ref": "#/definitions/Inner"
},
{
"type": "null"
}
]
}
},
"definitions": {
"Inner": {
"oneOf": [
{
"type": "string",
"enum": [
"UndocumentedUnit1",
"UndocumentedUnit2"
]
},
{
"description": "This is a documented unit variant",
"type": "string",
"enum": [
"DocumentedUnit"
]
},
{
"type": "object",
"required": [
"ValueNewType"
],
"properties": {
"ValueNewType": true
},
"additionalProperties": false
}
]
}
}
}
60 changes: 60 additions & 0 deletions schemars/tests/expected/schema_settings-openapi3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Outer",
"type": "object",
"required": [
"int",
"value",
"values"
],
"properties": {
"int": {
"type": "integer",
"format": "int32",
"example": 8
},
"values": {
"type": "object",
"additionalProperties": true
},
"value": {},
"inner": {
"allOf": [
{
"$ref": "#/components/schemas/Inner"
}
],
"nullable": true
}
},
"definitions": {
"Inner": {
"oneOf": [
{
"type": "string",
"enum": [
"UndocumentedUnit1",
"UndocumentedUnit2"
]
},
{
"description": "This is a documented unit variant",
"type": "string",
"enum": [
"DocumentedUnit"
]
},
{
"type": "object",
"required": [
"ValueNewType"
],
"properties": {
"ValueNewType": {}
},
"additionalProperties": false
}
]
}
}
}
65 changes: 65 additions & 0 deletions schemars/tests/expected/schema_settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Outer",
"type": "object",
"required": [
"int",
"value",
"values"
],
"properties": {
"int": {
"examples": [
8,
null
],
"type": "integer",
"format": "int32"
},
"values": {
"type": "object",
"additionalProperties": true
},
"value": true,
"inner": {
"anyOf": [
{
"$ref": "#/definitions/Inner"
},
{
"type": "null"
}
]
}
},
"definitions": {
"Inner": {
"oneOf": [
{
"type": "string",
"enum": [
"UndocumentedUnit1",
"UndocumentedUnit2"
]
},
{
"description": "This is a documented unit variant",
"type": "string",
"enum": [
"DocumentedUnit"
]
},
{
"type": "object",
"required": [
"ValueNewType"
],
"properties": {
"ValueNewType": true
},
"additionalProperties": false
}
]
}
}
}
45 changes: 45 additions & 0 deletions schemars/tests/schema_settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
mod util;
use schemars::gen::SchemaSettings;
use schemars::JsonSchema;
use serde_json::Value;
use std::collections::BTreeMap;
use util::*;

#[derive(JsonSchema)]
pub struct Outer {
#[schemars(example = "eight", example = "null")]
pub int: i32,
pub values: BTreeMap<&'static str, Value>,
pub value: Value,
pub inner: Option<Inner>,
}

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

fn eight() -> i32 {
8
}

fn null() {}

#[test]
fn schema_matches_draft07() -> TestResult {
test_generated_schema::<Outer>("schema_settings", SchemaSettings::draft07())
}

#[test]
fn schema_matches_2019_09() -> TestResult {
test_generated_schema::<Outer>("schema_settings-2019_09", SchemaSettings::draft2019_09())
}

#[test]
fn schema_matches_openapi3() -> TestResult {
test_generated_schema::<Outer>("schema_settings-openapi3", SchemaSettings::openapi3())
}

0 comments on commit 449bb1a

Please sign in to comment.