From 449bb1a0ca64ddb549c95d8ebb5d210bebd187e8 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Mon, 13 May 2024 17:53:56 +0100 Subject: [PATCH] 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 --- .../expected/schema_settings-2019_09.json | 65 +++++++++++++++++++ .../expected/schema_settings-openapi3.json | 60 +++++++++++++++++ schemars/tests/expected/schema_settings.json | 65 +++++++++++++++++++ schemars/tests/schema_settings.rs | 45 +++++++++++++ 4 files changed, 235 insertions(+) create mode 100644 schemars/tests/expected/schema_settings-2019_09.json create mode 100644 schemars/tests/expected/schema_settings-openapi3.json create mode 100644 schemars/tests/expected/schema_settings.json create mode 100644 schemars/tests/schema_settings.rs diff --git a/schemars/tests/expected/schema_settings-2019_09.json b/schemars/tests/expected/schema_settings-2019_09.json new file mode 100644 index 00000000..fb936a72 --- /dev/null +++ b/schemars/tests/expected/schema_settings-2019_09.json @@ -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 + } + ] + } + } +} \ No newline at end of file diff --git a/schemars/tests/expected/schema_settings-openapi3.json b/schemars/tests/expected/schema_settings-openapi3.json new file mode 100644 index 00000000..17fe8051 --- /dev/null +++ b/schemars/tests/expected/schema_settings-openapi3.json @@ -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 + } + ] + } + } +} \ No newline at end of file diff --git a/schemars/tests/expected/schema_settings.json b/schemars/tests/expected/schema_settings.json new file mode 100644 index 00000000..c876d6f0 --- /dev/null +++ b/schemars/tests/expected/schema_settings.json @@ -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 + } + ] + } + } +} \ No newline at end of file diff --git a/schemars/tests/schema_settings.rs b/schemars/tests/schema_settings.rs new file mode 100644 index 00000000..a82570db --- /dev/null +++ b/schemars/tests/schema_settings.rs @@ -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, +} + +#[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::("schema_settings", SchemaSettings::draft07()) +} + +#[test] +fn schema_matches_2019_09() -> TestResult { + test_generated_schema::("schema_settings-2019_09", SchemaSettings::draft2019_09()) +} + +#[test] +fn schema_matches_openapi3() -> TestResult { + test_generated_schema::("schema_settings-openapi3", SchemaSettings::openapi3()) +}