From 68ddfa310faa44d09056425a300d6a19f0d795bd Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Sun, 13 Oct 2019 23:01:48 +0100 Subject: [PATCH] Add default, deprecated, readOnly, writeOnly properties --- schemars/src/flatten.rs | 19 ++++++++++--- schemars/src/schema.rs | 13 +++++++++ schemars/tests/expected/schema-openapi3.json | 30 ++++++++++++++------ schemars/tests/expected/schema.json | 28 ++++++++++++------ 4 files changed, 68 insertions(+), 22 deletions(-) diff --git a/schemars/src/flatten.rs b/schemars/src/flatten.rs index 506e719..367e5d8 100644 --- a/schemars/src/flatten.rs +++ b/schemars/src/flatten.rs @@ -40,10 +40,21 @@ impl_merge!(SchemaObject { or: format const_value reference, }); -impl_merge!(Metadata { - merge: definitions, - or: schema id title description, -}); +impl Merge for Metadata { + fn merge(self, other: Self) -> Self { + Metadata { + definitions: self.definitions.merge(other.definitions), + schema: self.schema.or(other.schema), + id: self.id.or(other.id), + title: self.title.or(other.title), + description: self.description.or(other.description), + default: self.default.or(other.default), + deprecated: self.deprecated || other.deprecated, + read_only: self.read_only || other.read_only, + write_only: self.write_only || other.write_only, + } + } +} impl_merge!(SubschemaValidation { or: all_of any_of one_of not if_schema then_schema else_schema, diff --git a/schemars/src/schema.rs b/schemars/src/schema.rs index 69bc8eb..9fde629 100644 --- a/schemars/src/schema.rs +++ b/schemars/src/schema.rs @@ -126,6 +126,19 @@ pub struct Metadata { pub description: Option, #[serde(alias = "$defs", skip_serializing_if = "Map::is_empty")] pub definitions: Map, + #[serde(skip_serializing_if = "Option::is_none")] + pub default: Option, + #[serde(skip_serializing_if = "is_false")] + pub deprecated: bool, + #[serde(skip_serializing_if = "is_false")] + pub read_only: bool, + #[serde(skip_serializing_if = "is_false")] + pub write_only: bool, +} + +#[allow(clippy::trivially_copy_pass_by_ref)] +fn is_false(b: &bool) -> bool { + !b } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] diff --git a/schemars/tests/expected/schema-openapi3.json b/schemars/tests/expected/schema-openapi3.json index f110998..e091416 100644 --- a/schemars/tests/expected/schema-openapi3.json +++ b/schemars/tests/expected/schema-openapi3.json @@ -1,14 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "title": "Schema", - "anyOf": [ - { - "type": "boolean" - }, - { - "$ref": "#/components/schemas/SchemaObject" - } - ], "definitions": { "InstanceType": { "enum": [ @@ -96,12 +88,18 @@ ], "nullable": true }, + "default": { + "nullable": true + }, "definitions": { "type": "object", "additionalProperties": { "$ref": "#/components/schemas/Schema" } }, + "deprecated": { + "type": "boolean" + }, "description": { "type": "string", "nullable": true @@ -251,6 +249,9 @@ ], "nullable": true }, + "readOnly": { + "type": "boolean" + }, "required": { "type": "array", "items": { @@ -289,9 +290,20 @@ "uniqueItems": { "type": "boolean", "nullable": true + }, + "writeOnly": { + "type": "boolean" } }, "additionalProperties": true } - } + }, + "anyOf": [ + { + "type": "boolean" + }, + { + "$ref": "#/components/schemas/SchemaObject" + } + ] } \ No newline at end of file diff --git a/schemars/tests/expected/schema.json b/schemars/tests/expected/schema.json index 5aaf3bf..009b6b8 100644 --- a/schemars/tests/expected/schema.json +++ b/schemars/tests/expected/schema.json @@ -1,14 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "title": "Schema", - "anyOf": [ - { - "type": "boolean" - }, - { - "$ref": "#/definitions/SchemaObject" - } - ], "definitions": { "InstanceType": { "enum": [ @@ -101,12 +93,16 @@ } ] }, + "default": true, "definitions": { "type": "object", "additionalProperties": { "$ref": "#/definitions/Schema" } }, + "deprecated": { + "type": "boolean" + }, "description": { "type": [ "string", @@ -280,6 +276,9 @@ } ] }, + "readOnly": { + "type": "boolean" + }, "required": { "type": "array", "items": { @@ -317,6 +316,9 @@ "boolean", "null" ] + }, + "writeOnly": { + "type": "boolean" } }, "additionalProperties": true @@ -347,5 +349,13 @@ } ] } - } + }, + "anyOf": [ + { + "type": "boolean" + }, + { + "$ref": "#/definitions/SchemaObject" + } + ] } \ No newline at end of file