Add more tests for different schema settings

This is mainly to keep this coverage around in v1, where the `schema_for_schema` tests are no longer viable
This commit is contained in:
Graham Esau 2024-05-13 17:53:56 +01:00
parent 7f6a7b7e32
commit 449bb1a0ca
4 changed files with 235 additions and 0 deletions

View file

@ -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
}
]
}
}
}

View file

@ -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
}
]
}
}
}

View file

@ -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
}
]
}
}
}

View file

@ -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())
}