From d3b6ff5aebb25be88af9f7eecbd5eb1740de3a23 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Sat, 18 May 2024 21:55:05 +0100 Subject: [PATCH] Re-add `preserve_order` feature, to preserve order of struct fields in a schema's `properties` --- Cargo.lock | 1 + schemars/Cargo.toml | 1 + schemars/src/gen.rs | 47 +++----- schemars/src/schema.rs | 3 +- schemars/tests/expected/bytes.json | 4 +- schemars/tests/expected/chrono-types.json | 24 ++-- schemars/tests/expected/crate_alias.json | 6 +- schemars/tests/expected/default.json | 20 ++-- schemars/tests/expected/deprecated-enum.json | 18 +-- .../tests/expected/deprecated-struct.json | 12 +- .../tests/expected/doc_comments_enum.json | 4 +- .../expected/duration_and_systemtime.json | 20 ++-- .../expected/enum-adjacent-tagged-duf.json | 110 +++++++++--------- .../tests/expected/enum-adjacent-tagged.json | 78 ++++++------- .../tests/expected/enum-external-duf.json | 46 ++++---- schemars/tests/expected/enum-external.json | 46 ++++---- .../tests/expected/enum-internal-duf.json | 24 ++-- schemars/tests/expected/enum-internal.json | 12 +- .../expected/enum-simple-internal-duf.json | 12 +- .../tests/expected/enum-untagged-duf.json | 22 ++-- schemars/tests/expected/enum-untagged.json | 22 ++-- schemars/tests/expected/enumset.json | 2 +- schemars/tests/expected/examples.json | 32 ++--- schemars/tests/expected/flatten.json | 12 +- schemars/tests/expected/from_json_value.json | 32 ++--- .../tests/expected/from_value_2019_09.json | 56 ++++----- .../tests/expected/from_value_draft07.json | 56 ++++----- .../tests/expected/from_value_openapi3.json | 58 ++++----- schemars/tests/expected/indexmap.json | 4 +- schemars/tests/expected/macro_built_enum.json | 4 +- .../tests/expected/macro_built_struct.json | 8 +- schemars/tests/expected/nonzero_ints.json | 16 +-- schemars/tests/expected/os_strings.json | 4 +- schemars/tests/expected/range.json | 80 ++++++------- schemars/tests/expected/remote_derive.json | 46 ++++---- .../tests/expected/remote_derive_generic.json | 44 +++---- schemars/tests/expected/result.json | 24 ++-- .../tests/expected/schema-name-custom.json | 6 +- .../tests/expected/schema-name-default.json | 6 +- .../expected/schema-name-mixed-generics.json | 36 +++--- .../expected/schema_settings-2019_09.json | 30 ++--- .../expected/schema_settings-openapi3.json | 22 ++-- schemars/tests/expected/schema_settings.json | 30 ++--- .../schema_with-enum-adjacent-tagged.json | 40 +++---- .../expected/schema_with-enum-external.json | 20 ++-- .../expected/schema_with-enum-untagged.json | 4 +- .../tests/expected/schema_with-struct.json | 6 +- .../tests/expected/schema_with-tuple.json | 4 +- .../tests/expected/skip_enum_variants.json | 4 +- .../tests/expected/skip_struct_fields.json | 10 +- .../tests/expected/skip_tuple_fields.json | 4 +- .../struct-normal-additional-properties.json | 8 +- schemars/tests/expected/struct-normal.json | 8 +- schemars/tests/expected/struct-tuple.json | 4 +- .../tests/expected/transparent-struct.json | 4 +- schemars/tests/expected/validate.json | 100 ++++++++-------- schemars/tests/expected/validate_inner.json | 62 +++++----- schemars/tests/expected/validate_newtype.json | 4 +- .../expected/validate_schemars_attrs.json | 100 ++++++++-------- schemars/tests/expected/validate_tuple.json | 8 +- 60 files changed, 757 insertions(+), 773 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 757013a..c415a5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -381,6 +381,7 @@ version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" dependencies = [ + "indexmap", "itoa", "ryu", "serde", diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index 47dc643..ec9ff00 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -44,6 +44,7 @@ serde = { version = "1.0", features = ["derive"] } default = ["derive"] derive = ["schemars_derive"] +preserve_order = ["serde_json/preserve_order"] raw_value = ["serde_json/raw_value"] diff --git a/schemars/src/gen.rs b/schemars/src/gen.rs index c06f85a..678ee41 100644 --- a/schemars/src/gen.rs +++ b/schemars/src/gen.rs @@ -11,8 +11,9 @@ use crate::Schema; use crate::{visit::*, JsonSchema}; use dyn_clone::DynClone; use serde::Serialize; +use serde_json::{Map, Value}; use std::borrow::Cow; -use std::collections::{BTreeMap, HashMap}; +use std::collections::HashMap; use std::{any::Any, collections::HashSet, fmt::Debug}; /// Settings to customize how Schemas are generated. @@ -149,7 +150,7 @@ impl SchemaSettings { #[derive(Debug, Default)] pub struct SchemaGenerator { settings: SchemaSettings, - definitions: BTreeMap, + definitions: Map, pending_schema_ids: HashSet>, schema_id_to_name: HashMap, String>, used_schema_names: HashSet, @@ -254,31 +255,31 @@ impl SchemaGenerator { let schema = self.json_schema_internal::(id); - self.definitions.insert(name, schema); + self.definitions.insert(name, schema.to_value()); } /// Borrows the collection of all [referenceable](JsonSchema::is_referenceable) schemas that have been generated. /// - /// The keys of the returned `BTreeMap` are the [schema names](JsonSchema::schema_name), and the values are the schemas + /// The keys of the returned `Map` are the [schema names](JsonSchema::schema_name), and the values are the schemas /// themselves. - pub fn definitions(&self) -> &BTreeMap { + pub fn definitions(&self) -> &Map { &self.definitions } /// Mutably borrows the collection of all [referenceable](JsonSchema::is_referenceable) schemas that have been generated. /// - /// The keys of the returned `BTreeMap` are the [schema names](JsonSchema::schema_name), and the values are the schemas + /// The keys of the returned `Map` are the [schema names](JsonSchema::schema_name), and the values are the schemas /// themselves. - pub fn definitions_mut(&mut self) -> &mut BTreeMap { + pub fn definitions_mut(&mut self) -> &mut Map { &mut self.definitions } /// Returns the collection of all [referenceable](JsonSchema::is_referenceable) schemas that have been generated, /// leaving an empty map in its place. /// - /// The keys of the returned `BTreeMap` are the [schema names](JsonSchema::schema_name), and the values are the schemas + /// The keys of the returned `Map` are the [schema names](JsonSchema::schema_name), and the values are the schemas /// themselves. - pub fn take_definitions(&mut self) -> BTreeMap { + pub fn take_definitions(&mut self) -> Map { std::mem::take(&mut self.definitions) } @@ -308,12 +309,7 @@ impl SchemaGenerator { if !self.definitions.is_empty() { object.insert( "definitions".into(), - serde_json::Value::Object( - self.definitions - .iter() - .map(|(k, v)| (k.clone(), v.clone().into())) - .collect(), - ), + serde_json::Value::Object(self.definitions.clone()), ); } @@ -344,12 +340,7 @@ impl SchemaGenerator { if !self.definitions.is_empty() { object.insert( "definitions".into(), - serde_json::Value::Object( - self.definitions - .into_iter() - .map(|(k, v)| (k, v.into())) - .collect(), - ), + serde_json::Value::Object(self.definitions), ); } @@ -386,12 +377,7 @@ impl SchemaGenerator { if !self.definitions.is_empty() { object.insert( "definitions".into(), - serde_json::Value::Object( - self.definitions - .iter() - .map(|(k, v)| (k.clone(), v.clone().into())) - .collect(), - ), + serde_json::Value::Object(self.definitions.clone()), ); } @@ -428,12 +414,7 @@ impl SchemaGenerator { if !self.definitions.is_empty() { object.insert( "definitions".into(), - serde_json::Value::Object( - self.definitions - .into_iter() - .map(|(k, v)| (k, v.into())) - .collect(), - ), + serde_json::Value::Object(self.definitions), ); } diff --git a/schemars/src/schema.rs b/schemars/src/schema.rs index 82be821..27cb048 100644 --- a/schemars/src/schema.rs +++ b/schemars/src/schema.rs @@ -180,7 +180,8 @@ mod ser { use serde_json::Value; // The order of properties in a JSON Schema object is insignificant, but we explicitly order - // some of them here to make them easier for a human to read. + // some of them here to make them easier for a human to read. All other properties are ordered + // either lexicographically (by default) or by insertion order (if `preserve_order` is enabled) const ORDERED_KEYWORDS_START: [&str; 7] = [ "$id", "$schema", diff --git a/schemars/tests/expected/bytes.json b/schemars/tests/expected/bytes.json index 06458c6..8618a7f 100644 --- a/schemars/tests/expected/bytes.json +++ b/schemars/tests/expected/bytes.json @@ -20,6 +20,6 @@ } } ], - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 } \ No newline at end of file diff --git a/schemars/tests/expected/chrono-types.json b/schemars/tests/expected/chrono-types.json index b9e333d..ce03621 100644 --- a/schemars/tests/expected/chrono-types.json +++ b/schemars/tests/expected/chrono-types.json @@ -3,6 +3,18 @@ "title": "ChronoTypes", "type": "object", "properties": { + "weekday": { + "type": "string", + "enum": [ + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + "Sun" + ] + }, "date_time": { "type": "string", "format": "date-time" @@ -18,18 +30,6 @@ "naive_time": { "type": "string", "format": "partial-date-time" - }, - "weekday": { - "type": "string", - "enum": [ - "Mon", - "Tue", - "Wed", - "Thu", - "Fri", - "Sat", - "Sun" - ] } }, "required": [ diff --git a/schemars/tests/expected/crate_alias.json b/schemars/tests/expected/crate_alias.json index b01fc0d..e88a449 100644 --- a/schemars/tests/expected/crate_alias.json +++ b/schemars/tests/expected/crate_alias.json @@ -3,13 +3,13 @@ "title": "Struct", "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "description": "This is a document", "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "required": [ diff --git a/schemars/tests/expected/default.json b/schemars/tests/expected/default.json index 36f16e1..88ad12a 100644 --- a/schemars/tests/expected/default.json +++ b/schemars/tests/expected/default.json @@ -3,22 +3,22 @@ "title": "MyStruct", "type": "object", "properties": { - "my_bool": { - "type": "boolean", - "default": false - }, "my_int": { "type": "integer", "format": "int32", "default": 0 }, + "my_bool": { + "type": "boolean", + "default": false + }, "my_struct2": { + "default": "i:0 b:false", "allOf": [ { "$ref": "#/definitions/MyStruct2" } - ], - "default": "i:0 b:false" + ] }, "my_struct2_default_skipped": { "$ref": "#/definitions/MyStruct2" @@ -31,14 +31,14 @@ "MyStruct2": { "type": "object", "properties": { - "my_bool": { - "type": "boolean", - "default": true - }, "my_int": { "type": "integer", "format": "int32", "default": 6 + }, + "my_bool": { + "type": "boolean", + "default": true } } }, diff --git a/schemars/tests/expected/deprecated-enum.json b/schemars/tests/expected/deprecated-enum.json index ee3de61..e871160 100644 --- a/schemars/tests/expected/deprecated-enum.json +++ b/schemars/tests/expected/deprecated-enum.json @@ -1,7 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "title": "DeprecatedEnum", - "deprecated": true, "oneOf": [ { "type": "string", @@ -20,13 +19,13 @@ "DeprecatedStructVariant": { "type": "object", "properties": { - "deprecated_field": { - "type": "boolean", - "deprecated": true - }, "foo": { "type": "integer", "format": "int32" + }, + "deprecated_field": { + "type": "boolean", + "deprecated": true } }, "required": [ @@ -35,11 +34,12 @@ ] } }, - "additionalProperties": false, - "deprecated": true, "required": [ "DeprecatedStructVariant" - ] + ], + "additionalProperties": false, + "deprecated": true } - ] + ], + "deprecated": true } \ No newline at end of file diff --git a/schemars/tests/expected/deprecated-struct.json b/schemars/tests/expected/deprecated-struct.json index d2af941..2b25c34 100644 --- a/schemars/tests/expected/deprecated-struct.json +++ b/schemars/tests/expected/deprecated-struct.json @@ -3,18 +3,18 @@ "title": "DeprecatedStruct", "type": "object", "properties": { - "deprecated_field": { - "type": "boolean", - "deprecated": true - }, "foo": { "type": "integer", "format": "int32" + }, + "deprecated_field": { + "type": "boolean", + "deprecated": true } }, - "deprecated": true, "required": [ "foo", "deprecated_field" - ] + ], + "deprecated": true } \ No newline at end of file diff --git a/schemars/tests/expected/doc_comments_enum.json b/schemars/tests/expected/doc_comments_enum.json index fc38db1..7d9321f 100644 --- a/schemars/tests/expected/doc_comments_enum.json +++ b/schemars/tests/expected/doc_comments_enum.json @@ -34,10 +34,10 @@ } } }, - "additionalProperties": false, "required": [ "Complex" - ] + ], + "additionalProperties": false } ] } \ No newline at end of file diff --git a/schemars/tests/expected/duration_and_systemtime.json b/schemars/tests/expected/duration_and_systemtime.json index 71c17b7..bb25dcd 100644 --- a/schemars/tests/expected/duration_and_systemtime.json +++ b/schemars/tests/expected/duration_and_systemtime.json @@ -18,15 +18,15 @@ "Duration": { "type": "object", "properties": { - "nanos": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, "secs": { "type": "integer", "format": "uint64", "minimum": 0 + }, + "nanos": { + "type": "integer", + "format": "uint32", + "minimum": 0 } }, "required": [ @@ -37,15 +37,15 @@ "SystemTime": { "type": "object", "properties": { - "nanos_since_epoch": { - "type": "integer", - "format": "uint32", - "minimum": 0 - }, "secs_since_epoch": { "type": "integer", "format": "uint64", "minimum": 0 + }, + "nanos_since_epoch": { + "type": "integer", + "format": "uint32", + "minimum": 0 } }, "required": [ diff --git a/schemars/tests/expected/enum-adjacent-tagged-duf.json b/schemars/tests/expected/enum-adjacent-tagged-duf.json index f746216..b9cb5fa 100644 --- a/schemars/tests/expected/enum-adjacent-tagged-duf.json +++ b/schemars/tests/expected/enum-adjacent-tagged-duf.json @@ -12,83 +12,89 @@ ] } }, - "additionalProperties": false, "required": [ "t" - ] + ], + "additionalProperties": false }, { "type": "object", "properties": { - "c": { - "type": "object", - "additionalProperties": { - "type": "string" - } - }, "t": { "type": "string", "enum": [ "StringMap" ] + }, + "c": { + "type": "object", + "additionalProperties": { + "type": "string" + } } }, - "additionalProperties": false, "required": [ "t", "c" - ] + ], + "additionalProperties": false }, { "type": "object", "properties": { - "c": { - "$ref": "#/definitions/UnitStruct" - }, "t": { "type": "string", "enum": [ "UnitStructNewType" ] + }, + "c": { + "$ref": "#/definitions/UnitStruct" } }, - "additionalProperties": false, "required": [ "t", "c" - ] + ], + "additionalProperties": false }, { "type": "object", "properties": { - "c": { - "$ref": "#/definitions/Struct" - }, "t": { "type": "string", "enum": [ "StructNewType" ] + }, + "c": { + "$ref": "#/definitions/Struct" } }, - "additionalProperties": false, "required": [ "t", "c" - ] + ], + "additionalProperties": false }, { "type": "object", "properties": { + "t": { + "type": "string", + "enum": [ + "Struct" + ] + }, "c": { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "additionalProperties": false, @@ -96,23 +102,23 @@ "foo", "bar" ] - }, - "t": { - "type": "string", - "enum": [ - "Struct" - ] } }, - "additionalProperties": false, "required": [ "t", "c" - ] + ], + "additionalProperties": false }, { "type": "object", "properties": { + "t": { + "type": "string", + "enum": [ + "Tuple" + ] + }, "c": { "type": "array", "items": [ @@ -124,21 +130,15 @@ "type": "boolean" } ], - "maxItems": 2, - "minItems": 2 - }, - "t": { - "type": "string", - "enum": [ - "Tuple" - ] + "minItems": 2, + "maxItems": 2 } }, - "additionalProperties": false, "required": [ "t", "c" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -150,51 +150,51 @@ ] } }, - "additionalProperties": false, "required": [ "t" - ] + ], + "additionalProperties": false }, { "type": "object", "properties": { - "c": { - "type": "integer", - "format": "int32" - }, "t": { "type": "string", "enum": [ "WithInt" ] + }, + "c": { + "type": "integer", + "format": "int32" } }, - "additionalProperties": false, "required": [ "t", "c" - ] + ], + "additionalProperties": false } ], "definitions": { + "UnitStruct": { + "type": "null" + }, "Struct": { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "required": [ "foo", "bar" ] - }, - "UnitStruct": { - "type": "null" } } } \ No newline at end of file diff --git a/schemars/tests/expected/enum-adjacent-tagged.json b/schemars/tests/expected/enum-adjacent-tagged.json index b977be0..de77713 100644 --- a/schemars/tests/expected/enum-adjacent-tagged.json +++ b/schemars/tests/expected/enum-adjacent-tagged.json @@ -19,17 +19,17 @@ { "type": "object", "properties": { - "c": { - "type": "object", - "additionalProperties": { - "type": "string" - } - }, "t": { "type": "string", "enum": [ "StringMap" ] + }, + "c": { + "type": "object", + "additionalProperties": { + "type": "string" + } } }, "required": [ @@ -40,14 +40,14 @@ { "type": "object", "properties": { - "c": { - "$ref": "#/definitions/UnitStruct" - }, "t": { "type": "string", "enum": [ "UnitStructNewType" ] + }, + "c": { + "$ref": "#/definitions/UnitStruct" } }, "required": [ @@ -58,14 +58,14 @@ { "type": "object", "properties": { - "c": { - "$ref": "#/definitions/Struct" - }, "t": { "type": "string", "enum": [ "StructNewType" ] + }, + "c": { + "$ref": "#/definitions/Struct" } }, "required": [ @@ -76,27 +76,27 @@ { "type": "object", "properties": { + "t": { + "type": "string", + "enum": [ + "Struct" + ] + }, "c": { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "required": [ "foo", "bar" ] - }, - "t": { - "type": "string", - "enum": [ - "Struct" - ] } }, "required": [ @@ -107,6 +107,12 @@ { "type": "object", "properties": { + "t": { + "type": "string", + "enum": [ + "Tuple" + ] + }, "c": { "type": "array", "items": [ @@ -118,14 +124,8 @@ "type": "boolean" } ], - "maxItems": 2, - "minItems": 2 - }, - "t": { - "type": "string", - "enum": [ - "Tuple" - ] + "minItems": 2, + "maxItems": 2 } }, "required": [ @@ -150,15 +150,15 @@ { "type": "object", "properties": { - "c": { - "type": "integer", - "format": "int32" - }, "t": { "type": "string", "enum": [ "WithInt" ] + }, + "c": { + "type": "integer", + "format": "int32" } }, "required": [ @@ -168,24 +168,24 @@ } ], "definitions": { + "UnitStruct": { + "type": "null" + }, "Struct": { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "required": [ "foo", "bar" ] - }, - "UnitStruct": { - "type": "null" } } } \ No newline at end of file diff --git a/schemars/tests/expected/enum-external-duf.json b/schemars/tests/expected/enum-external-duf.json index 0351fa2..a483535 100644 --- a/schemars/tests/expected/enum-external-duf.json +++ b/schemars/tests/expected/enum-external-duf.json @@ -19,10 +19,10 @@ } } }, - "additionalProperties": false, "required": [ "stringMap" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -31,10 +31,10 @@ "$ref": "#/definitions/UnitStruct" } }, - "additionalProperties": false, "required": [ "unitStructNewType" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -43,10 +43,10 @@ "$ref": "#/definitions/Struct" } }, - "additionalProperties": false, "required": [ "structNewType" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -54,12 +54,12 @@ "struct": { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "additionalProperties": false, @@ -69,10 +69,10 @@ ] } }, - "additionalProperties": false, "required": [ "struct" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -88,14 +88,14 @@ "type": "boolean" } ], - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 } }, - "additionalProperties": false, "required": [ "tuple" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -105,31 +105,31 @@ "format": "int32" } }, - "additionalProperties": false, "required": [ "withInt" - ] + ], + "additionalProperties": false } ], "definitions": { + "UnitStruct": { + "type": "null" + }, "Struct": { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "required": [ "foo", "bar" ] - }, - "UnitStruct": { - "type": "null" } } } \ No newline at end of file diff --git a/schemars/tests/expected/enum-external.json b/schemars/tests/expected/enum-external.json index 4c1d5a6..8a1de2b 100644 --- a/schemars/tests/expected/enum-external.json +++ b/schemars/tests/expected/enum-external.json @@ -19,10 +19,10 @@ } } }, - "additionalProperties": false, "required": [ "stringMap" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -31,10 +31,10 @@ "$ref": "#/definitions/UnitStruct" } }, - "additionalProperties": false, "required": [ "unitStructNewType" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -43,10 +43,10 @@ "$ref": "#/definitions/Struct" } }, - "additionalProperties": false, "required": [ "structNewType" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -54,12 +54,12 @@ "struct": { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "required": [ @@ -68,10 +68,10 @@ ] } }, - "additionalProperties": false, "required": [ "struct" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -87,14 +87,14 @@ "type": "boolean" } ], - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 } }, - "additionalProperties": false, "required": [ "tuple" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -104,31 +104,31 @@ "format": "int32" } }, - "additionalProperties": false, "required": [ "withInt" - ] + ], + "additionalProperties": false } ], "definitions": { + "UnitStruct": { + "type": "null" + }, "Struct": { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "required": [ "foo", "bar" ] - }, - "UnitStruct": { - "type": "null" } } } \ No newline at end of file diff --git a/schemars/tests/expected/enum-internal-duf.json b/schemars/tests/expected/enum-internal-duf.json index 9c2aa9c..de51ba1 100644 --- a/schemars/tests/expected/enum-internal-duf.json +++ b/schemars/tests/expected/enum-internal-duf.json @@ -10,10 +10,10 @@ "const": "UnitOne" } }, - "additionalProperties": false, "required": [ "typeProperty" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -38,21 +38,21 @@ "const": "UnitStructNewType" } }, - "additionalProperties": false, "required": [ "typeProperty" - ] + ], + "additionalProperties": false }, { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" }, + "bar": { + "type": "boolean" + }, "typeProperty": { "type": "string", "const": "StructNewType" @@ -67,13 +67,13 @@ { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" }, + "bar": { + "type": "boolean" + }, "typeProperty": { "type": "string", "const": "Struct" @@ -94,10 +94,10 @@ "const": "UnitTwo" } }, - "additionalProperties": false, "required": [ "typeProperty" - ] + ], + "additionalProperties": false }, { "type": "object", diff --git a/schemars/tests/expected/enum-internal.json b/schemars/tests/expected/enum-internal.json index 7fd931c..a5dffe3 100644 --- a/schemars/tests/expected/enum-internal.json +++ b/schemars/tests/expected/enum-internal.json @@ -44,13 +44,13 @@ { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" }, + "bar": { + "type": "boolean" + }, "typeProperty": { "type": "string", "const": "StructNewType" @@ -65,13 +65,13 @@ { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" }, + "bar": { + "type": "boolean" + }, "typeProperty": { "type": "string", "const": "Struct" diff --git a/schemars/tests/expected/enum-simple-internal-duf.json b/schemars/tests/expected/enum-simple-internal-duf.json index 4fdf04e..7fc20c1 100644 --- a/schemars/tests/expected/enum-simple-internal-duf.json +++ b/schemars/tests/expected/enum-simple-internal-duf.json @@ -10,10 +10,10 @@ "const": "A" } }, - "additionalProperties": false, "required": [ "typeProperty" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -23,10 +23,10 @@ "const": "B" } }, - "additionalProperties": false, "required": [ "typeProperty" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -36,10 +36,10 @@ "const": "C" } }, - "additionalProperties": false, "required": [ "typeProperty" - ] + ], + "additionalProperties": false } ] } \ No newline at end of file diff --git a/schemars/tests/expected/enum-untagged-duf.json b/schemars/tests/expected/enum-untagged-duf.json index e3f7c66..13a7d0a 100644 --- a/schemars/tests/expected/enum-untagged-duf.json +++ b/schemars/tests/expected/enum-untagged-duf.json @@ -20,12 +20,12 @@ { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "additionalProperties": false, @@ -45,8 +45,8 @@ "type": "boolean" } ], - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 }, { "type": "integer", @@ -54,24 +54,24 @@ } ], "definitions": { + "UnitStruct": { + "type": "null" + }, "Struct": { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "required": [ "foo", "bar" ] - }, - "UnitStruct": { - "type": "null" } } } \ No newline at end of file diff --git a/schemars/tests/expected/enum-untagged.json b/schemars/tests/expected/enum-untagged.json index 2b23ffb..ed72b43 100644 --- a/schemars/tests/expected/enum-untagged.json +++ b/schemars/tests/expected/enum-untagged.json @@ -20,12 +20,12 @@ { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "required": [ @@ -44,8 +44,8 @@ "type": "boolean" } ], - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 }, { "type": "integer", @@ -53,24 +53,24 @@ } ], "definitions": { + "UnitStruct": { + "type": "null" + }, "Struct": { "type": "object", "properties": { - "bar": { - "type": "boolean" - }, "foo": { "type": "integer", "format": "int32" + }, + "bar": { + "type": "boolean" } }, "required": [ "foo", "bar" ] - }, - "UnitStruct": { - "type": "null" } } } \ No newline at end of file diff --git a/schemars/tests/expected/enumset.json b/schemars/tests/expected/enumset.json index b4dc26a..0f9baa2 100644 --- a/schemars/tests/expected/enumset.json +++ b/schemars/tests/expected/enumset.json @@ -2,10 +2,10 @@ "$schema": "http://json-schema.org/draft-07/schema#", "title": "Set_of_Foo", "type": "array", + "uniqueItems": true, "items": { "$ref": "#/definitions/Foo" }, - "uniqueItems": true, "definitions": { "Foo": { "type": "string", diff --git a/schemars/tests/expected/examples.json b/schemars/tests/expected/examples.json index 3203c4a..9f0a505 100644 --- a/schemars/tests/expected/examples.json +++ b/schemars/tests/expected/examples.json @@ -3,6 +3,14 @@ "title": "Struct", "type": "object", "properties": { + "foo": { + "type": "integer", + "format": "int32", + "examples": [ + 8, + null + ] + }, "bar": { "type": "boolean" }, @@ -14,26 +22,18 @@ "examples": [ null ] - }, - "foo": { - "type": "integer", - "format": "int32", - "examples": [ - 8, - null - ] } }, - "examples": [ - { - "bar": false, - "baz": null, - "foo": 0 - }, - null - ], "required": [ "foo", "bar" + ], + "examples": [ + { + "foo": 0, + "bar": false, + "baz": null + }, + null ] } \ No newline at end of file diff --git a/schemars/tests/expected/flatten.json b/schemars/tests/expected/flatten.json index 8f77dd2..4ea7094 100644 --- a/schemars/tests/expected/flatten.json +++ b/schemars/tests/expected/flatten.json @@ -3,20 +3,20 @@ "title": "Flat", "type": "object", "properties": { - "b": { - "type": "boolean" - }, "f": { "type": "number", "format": "float" }, - "os": { - "type": "string", - "default": "" + "b": { + "type": "boolean" }, "s": { "type": "string" }, + "os": { + "type": "string", + "default": "" + }, "v": { "type": "array", "items": { diff --git a/schemars/tests/expected/from_json_value.json b/schemars/tests/expected/from_json_value.json index cd1c634..6cc1ecc 100644 --- a/schemars/tests/expected/from_json_value.json +++ b/schemars/tests/expected/from_json_value.json @@ -2,12 +2,21 @@ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { - "bool": { - "type": "boolean" + "zero": { + "type": "integer" + }, + "one": { + "type": "integer" }, "minusOne": { "type": "integer" }, + "zeroPointZero": { + "type": "number" + }, + "bool": { + "type": "boolean" + }, "null": true, "object": { "type": "object", @@ -19,31 +28,22 @@ } } } - }, - "one": { - "type": "integer" - }, - "zero": { - "type": "integer" - }, - "zeroPointZero": { - "type": "number" } }, "examples": [ { - "bool": true, + "zero": 0, + "one": 1, "minusOne": -1, + "zeroPointZero": 0.0, + "bool": true, "null": null, "object": { "array": [ "foo", "bar" ] - }, - "one": 1, - "zero": 0, - "zeroPointZero": 0.0 + } } ] } \ No newline at end of file diff --git a/schemars/tests/expected/from_value_2019_09.json b/schemars/tests/expected/from_value_2019_09.json index 4c6adcf..52c0524 100644 --- a/schemars/tests/expected/from_value_2019_09.json +++ b/schemars/tests/expected/from_value_2019_09.json @@ -3,12 +3,28 @@ "title": "MyStruct", "type": "object", "properties": { + "myInt": { + "type": "integer" + }, "myBool": { "type": "boolean" }, + "myNullableEnum": true, "myInnerStruct": { "type": "object", "properties": { + "my_map": { + "type": "object", + "additionalProperties": { + "type": "number" + } + }, + "my_vec": { + "type": "array", + "items": { + "type": "string" + } + }, "my_empty_map": { "type": "object", "additionalProperties": true @@ -17,19 +33,13 @@ "type": "array", "items": true }, - "my_map": { - "type": "object", - "additionalProperties": { - "type": "number" - } - }, "my_tuple": { "type": "array", "items": [ { "type": "string", - "maxLength": 1, - "minLength": 1 + "minLength": 1, + "maxLength": 1 }, { "type": "integer" @@ -37,40 +47,30 @@ ], "maxItems": 2, "minItems": 2 - }, - "my_vec": { - "type": "array", - "items": { - "type": "string" - } } } - }, - "myInt": { - "type": "integer" - }, - "myNullableEnum": true + } }, "examples": [ { + "myInt": 123, "myBool": true, + "myNullableEnum": null, "myInnerStruct": { - "my_empty_map": {}, - "my_empty_vec": [], "my_map": { "": 0.0 }, - "my_tuple": [ - "💩", - 42 - ], "my_vec": [ "hello", "world" + ], + "my_empty_map": {}, + "my_empty_vec": [], + "my_tuple": [ + "💩", + 42 ] - }, - "myInt": 123, - "myNullableEnum": null + } } ] } \ No newline at end of file diff --git a/schemars/tests/expected/from_value_draft07.json b/schemars/tests/expected/from_value_draft07.json index 5d87f02..de89fad 100644 --- a/schemars/tests/expected/from_value_draft07.json +++ b/schemars/tests/expected/from_value_draft07.json @@ -3,12 +3,28 @@ "title": "MyStruct", "type": "object", "properties": { + "myInt": { + "type": "integer" + }, "myBool": { "type": "boolean" }, + "myNullableEnum": true, "myInnerStruct": { "type": "object", "properties": { + "my_map": { + "type": "object", + "additionalProperties": { + "type": "number" + } + }, + "my_vec": { + "type": "array", + "items": { + "type": "string" + } + }, "my_empty_map": { "type": "object", "additionalProperties": true @@ -17,19 +33,13 @@ "type": "array", "items": true }, - "my_map": { - "type": "object", - "additionalProperties": { - "type": "number" - } - }, "my_tuple": { "type": "array", "items": [ { "type": "string", - "maxLength": 1, - "minLength": 1 + "minLength": 1, + "maxLength": 1 }, { "type": "integer" @@ -37,40 +47,30 @@ ], "maxItems": 2, "minItems": 2 - }, - "my_vec": { - "type": "array", - "items": { - "type": "string" - } } } - }, - "myInt": { - "type": "integer" - }, - "myNullableEnum": true + } }, "examples": [ { + "myInt": 123, "myBool": true, + "myNullableEnum": null, "myInnerStruct": { - "my_empty_map": {}, - "my_empty_vec": [], "my_map": { "": 0.0 }, - "my_tuple": [ - "💩", - 42 - ], "my_vec": [ "hello", "world" + ], + "my_empty_map": {}, + "my_empty_vec": [], + "my_tuple": [ + "💩", + 42 ] - }, - "myInt": 123, - "myNullableEnum": null + } } ] } \ No newline at end of file diff --git a/schemars/tests/expected/from_value_openapi3.json b/schemars/tests/expected/from_value_openapi3.json index 0405935..4e9dd2c 100644 --- a/schemars/tests/expected/from_value_openapi3.json +++ b/schemars/tests/expected/from_value_openapi3.json @@ -3,12 +3,30 @@ "title": "MyStruct", "type": "object", "properties": { + "myInt": { + "type": "integer" + }, "myBool": { "type": "boolean" }, + "myNullableEnum": { + "nullable": true + }, "myInnerStruct": { "type": "object", "properties": { + "my_map": { + "type": "object", + "additionalProperties": { + "type": "number" + } + }, + "my_vec": { + "type": "array", + "items": { + "type": "string" + } + }, "my_empty_map": { "type": "object", "additionalProperties": true @@ -17,19 +35,13 @@ "type": "array", "items": {} }, - "my_map": { - "type": "object", - "additionalProperties": { - "type": "number" - } - }, "my_tuple": { "type": "array", "items": [ { "type": "string", - "maxLength": 1, - "minLength": 1 + "minLength": 1, + "maxLength": 1 }, { "type": "integer" @@ -37,40 +49,28 @@ ], "maxItems": 2, "minItems": 2 - }, - "my_vec": { - "type": "array", - "items": { - "type": "string" - } } } - }, - "myInt": { - "type": "integer" - }, - "myNullableEnum": { - "nullable": true } }, "example": { + "myInt": 123, "myBool": true, + "myNullableEnum": null, "myInnerStruct": { - "my_empty_map": {}, - "my_empty_vec": [], "my_map": { "": 0.0 }, - "my_tuple": [ - "💩", - 42 - ], "my_vec": [ "hello", "world" + ], + "my_empty_map": {}, + "my_empty_vec": [], + "my_tuple": [ + "💩", + 42 ] - }, - "myInt": 123, - "myNullableEnum": null + } } } \ No newline at end of file diff --git a/schemars/tests/expected/indexmap.json b/schemars/tests/expected/indexmap.json index 9c209e6..318bf9b 100644 --- a/schemars/tests/expected/indexmap.json +++ b/schemars/tests/expected/indexmap.json @@ -11,11 +11,11 @@ }, "set": { "type": "array", + "uniqueItems": true, "items": { "type": "integer", "format": "int" - }, - "uniqueItems": true + } } }, "required": [ diff --git a/schemars/tests/expected/macro_built_enum.json b/schemars/tests/expected/macro_built_enum.json index 51a9bb7..4fedbb2 100644 --- a/schemars/tests/expected/macro_built_enum.json +++ b/schemars/tests/expected/macro_built_enum.json @@ -9,10 +9,10 @@ "$ref": "#/definitions/InnerStruct" } }, - "additionalProperties": false, "required": [ "InnerStruct" - ] + ], + "additionalProperties": false } ], "definitions": { diff --git a/schemars/tests/expected/macro_built_struct.json b/schemars/tests/expected/macro_built_struct.json index e958c8e..74a279c 100644 --- a/schemars/tests/expected/macro_built_struct.json +++ b/schemars/tests/expected/macro_built_struct.json @@ -3,14 +3,14 @@ "title": "A", "type": "object", "properties": { - "v": { - "type": "integer", - "format": "int32" - }, "x": { "type": "integer", "format": "uint8", "minimum": 0 + }, + "v": { + "type": "integer", + "format": "int32" } }, "required": [ diff --git a/schemars/tests/expected/nonzero_ints.json b/schemars/tests/expected/nonzero_ints.json index 6ee5105..97dee99 100644 --- a/schemars/tests/expected/nonzero_ints.json +++ b/schemars/tests/expected/nonzero_ints.json @@ -3,12 +3,10 @@ "title": "MyStruct", "type": "object", "properties": { - "nonzero_signed": { + "unsigned": { "type": "integer", - "format": "int32", - "not": { - "const": 0 - } + "format": "uint32", + "minimum": 0 }, "nonzero_unsigned": { "type": "integer", @@ -19,10 +17,12 @@ "type": "integer", "format": "int32" }, - "unsigned": { + "nonzero_signed": { "type": "integer", - "format": "uint32", - "minimum": 0 + "format": "int32", + "not": { + "const": 0 + } } }, "required": [ diff --git a/schemars/tests/expected/os_strings.json b/schemars/tests/expected/os_strings.json index 927f3f9..b422026 100644 --- a/schemars/tests/expected/os_strings.json +++ b/schemars/tests/expected/os_strings.json @@ -3,10 +3,10 @@ "title": "OsStrings", "type": "object", "properties": { - "borrowed": { + "owned": { "$ref": "#/definitions/OsString" }, - "owned": { + "borrowed": { "$ref": "#/definitions/OsString" } }, diff --git a/schemars/tests/expected/range.json b/schemars/tests/expected/range.json index 19e4780..25f7389 100644 --- a/schemars/tests/expected/range.json +++ b/schemars/tests/expected/range.json @@ -3,14 +3,14 @@ "title": "MyStruct", "type": "object", "properties": { - "bound": { - "$ref": "#/definitions/Bound_of_string" + "range": { + "$ref": "#/definitions/Range_of_uint" }, "inclusive": { "$ref": "#/definitions/Range_of_double" }, - "range": { - "$ref": "#/definitions/Range_of_uint" + "bound": { + "$ref": "#/definitions/Bound_of_string" } }, "required": [ @@ -19,6 +19,42 @@ "bound" ], "definitions": { + "Range_of_uint": { + "type": "object", + "properties": { + "start": { + "type": "integer", + "format": "uint", + "minimum": 0 + }, + "end": { + "type": "integer", + "format": "uint", + "minimum": 0 + } + }, + "required": [ + "start", + "end" + ] + }, + "Range_of_double": { + "type": "object", + "properties": { + "start": { + "type": "number", + "format": "double" + }, + "end": { + "type": "number", + "format": "double" + } + }, + "required": [ + "start", + "end" + ] + }, "Bound_of_string": { "oneOf": [ { @@ -48,42 +84,6 @@ "const": "Unbounded" } ] - }, - "Range_of_double": { - "type": "object", - "properties": { - "end": { - "type": "number", - "format": "double" - }, - "start": { - "type": "number", - "format": "double" - } - }, - "required": [ - "start", - "end" - ] - }, - "Range_of_uint": { - "type": "object", - "properties": { - "end": { - "type": "integer", - "format": "uint", - "minimum": 0 - }, - "start": { - "type": "integer", - "format": "uint", - "minimum": 0 - } - }, - "required": [ - "start", - "end" - ] } } } \ No newline at end of file diff --git a/schemars/tests/expected/remote_derive.json b/schemars/tests/expected/remote_derive.json index 9477e0f..0f27ad3 100644 --- a/schemars/tests/expected/remote_derive.json +++ b/schemars/tests/expected/remote_derive.json @@ -6,27 +6,27 @@ "command_line": { "type": "string" }, - "system_cpu_time": { - "allOf": [ - { - "$ref": "#/definitions/Duration" - } - ], - "default": "0.000000000s" - }, - "user_cpu_time": { - "allOf": [ - { - "$ref": "#/definitions/Duration" - } - ], - "default": { - "nanos": 0, - "secs": 0 - } - }, "wall_time": { "$ref": "#/definitions/Duration" + }, + "user_cpu_time": { + "default": { + "secs": 0, + "nanos": 0 + }, + "allOf": [ + { + "$ref": "#/definitions/Duration" + } + ] + }, + "system_cpu_time": { + "default": "0.000000000s", + "allOf": [ + { + "$ref": "#/definitions/Duration" + } + ] } }, "required": [ @@ -37,13 +37,13 @@ "Duration": { "type": "object", "properties": { - "nanos": { - "type": "integer", - "format": "int32" - }, "secs": { "type": "integer", "format": "int64" + }, + "nanos": { + "type": "integer", + "format": "int32" } }, "required": [ diff --git a/schemars/tests/expected/remote_derive_generic.json b/schemars/tests/expected/remote_derive_generic.json index b533320..f6ba3ef 100644 --- a/schemars/tests/expected/remote_derive_generic.json +++ b/schemars/tests/expected/remote_derive_generic.json @@ -6,21 +6,21 @@ "byte_or_bool2": { "$ref": "#/definitions/Or_for_uint8_and_boolean" }, - "fake_map": { - "type": "object", - "additionalProperties": { - "type": "array", - "items": { - "type": "string" - }, - "uniqueItems": true - } + "unit_or_t2": { + "$ref": "#/definitions/Or_for_null_and_int32" }, "s": { "$ref": "#/definitions/Str" }, - "unit_or_t2": { - "$ref": "#/definitions/Or_for_null_and_int32" + "fake_map": { + "type": "object", + "additionalProperties": { + "type": "array", + "uniqueItems": true, + "items": { + "type": "string" + } + } } }, "required": [ @@ -30,17 +30,6 @@ "fake_map" ], "definitions": { - "Or_for_null_and_int32": { - "anyOf": [ - { - "type": "null" - }, - { - "type": "integer", - "format": "int32" - } - ] - }, "Or_for_uint8_and_boolean": { "anyOf": [ { @@ -53,6 +42,17 @@ } ] }, + "Or_for_null_and_int32": { + "anyOf": [ + { + "type": "null" + }, + { + "type": "integer", + "format": "int32" + } + ] + }, "Str": { "type": "string" } diff --git a/schemars/tests/expected/result.json b/schemars/tests/expected/result.json index 44c079e..8a25a0e 100644 --- a/schemars/tests/expected/result.json +++ b/schemars/tests/expected/result.json @@ -15,18 +15,6 @@ "result2" ], "definitions": { - "MyStruct": { - "type": "object", - "properties": { - "foo": { - "type": "integer", - "format": "int32" - } - }, - "required": [ - "foo" - ] - }, "Result_of_MyStruct_or_Array_of_string": { "oneOf": [ { @@ -56,6 +44,18 @@ } ] }, + "MyStruct": { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + } + }, + "required": [ + "foo" + ] + }, "Result_of_boolean_or_null": { "oneOf": [ { diff --git a/schemars/tests/expected/schema-name-custom.json b/schemars/tests/expected/schema-name-custom.json index dc5336b..447edbd 100644 --- a/schemars/tests/expected/schema-name-custom.json +++ b/schemars/tests/expected/schema-name-custom.json @@ -3,9 +3,6 @@ "title": "a-new-name-Array_of_string-int32-int32", "type": "object", "properties": { - "inner": { - "$ref": "#/definitions/another-new-name" - }, "t": { "type": "integer", "format": "int32" @@ -21,6 +18,9 @@ "items": { "type": "string" } + }, + "inner": { + "$ref": "#/definitions/another-new-name" } }, "required": [ diff --git a/schemars/tests/expected/schema-name-default.json b/schemars/tests/expected/schema-name-default.json index 7524c63..bd2ab9a 100644 --- a/schemars/tests/expected/schema-name-default.json +++ b/schemars/tests/expected/schema-name-default.json @@ -3,9 +3,6 @@ "title": "MyStruct_for_int32_and_null_and_boolean_and_Array_of_string", "type": "object", "properties": { - "inner": { - "$ref": "#/definitions/MySimpleStruct" - }, "t": { "type": "integer", "format": "int32" @@ -21,6 +18,9 @@ "items": { "type": "string" } + }, + "inner": { + "$ref": "#/definitions/MySimpleStruct" } }, "required": [ diff --git a/schemars/tests/expected/schema-name-mixed-generics.json b/schemars/tests/expected/schema-name-mixed-generics.json index fdadbab..4342410 100644 --- a/schemars/tests/expected/schema-name-mixed-generics.json +++ b/schemars/tests/expected/schema-name-mixed-generics.json @@ -3,12 +3,12 @@ "title": "MixedGenericStruct_for_MyStruct_for_int32_and_null_and_boolean_and_Array_of_string_and_42_and_z", "type": "object", "properties": { + "generic": { + "$ref": "#/definitions/MyStruct_for_int32_and_null_and_boolean_and_Array_of_string" + }, "foo": { "type": "integer", "format": "int32" - }, - "generic": { - "$ref": "#/definitions/MyStruct_for_int32_and_null_and_boolean_and_Array_of_string" } }, "required": [ @@ -16,24 +16,9 @@ "foo" ], "definitions": { - "MySimpleStruct": { - "type": "object", - "properties": { - "foo": { - "type": "integer", - "format": "int32" - } - }, - "required": [ - "foo" - ] - }, "MyStruct_for_int32_and_null_and_boolean_and_Array_of_string": { "type": "object", "properties": { - "inner": { - "$ref": "#/definitions/MySimpleStruct" - }, "t": { "type": "integer", "format": "int32" @@ -49,6 +34,9 @@ "items": { "type": "string" } + }, + "inner": { + "$ref": "#/definitions/MySimpleStruct" } }, "required": [ @@ -58,6 +46,18 @@ "w", "inner" ] + }, + "MySimpleStruct": { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + } + }, + "required": [ + "foo" + ] } } } \ No newline at end of file diff --git a/schemars/tests/expected/schema_settings-2019_09.json b/schemars/tests/expected/schema_settings-2019_09.json index 947aa2e..22f962f 100644 --- a/schemars/tests/expected/schema_settings-2019_09.json +++ b/schemars/tests/expected/schema_settings-2019_09.json @@ -3,6 +3,19 @@ "title": "Outer", "type": "object", "properties": { + "int": { + "type": "integer", + "format": "int32", + "examples": [ + 8, + null + ] + }, + "values": { + "type": "object", + "additionalProperties": true + }, + "value": true, "inner": { "anyOf": [ { @@ -12,19 +25,6 @@ "type": "null" } ] - }, - "int": { - "type": "integer", - "format": "int32", - "examples": [ - 8, - null - ] - }, - "value": true, - "values": { - "type": "object", - "additionalProperties": true } }, "required": [ @@ -52,10 +52,10 @@ "properties": { "ValueNewType": true }, - "additionalProperties": false, "required": [ "ValueNewType" - ] + ], + "additionalProperties": false } ] } diff --git a/schemars/tests/expected/schema_settings-openapi3.json b/schemars/tests/expected/schema_settings-openapi3.json index b6de302..c4e5199 100644 --- a/schemars/tests/expected/schema_settings-openapi3.json +++ b/schemars/tests/expected/schema_settings-openapi3.json @@ -3,23 +3,23 @@ "title": "Outer", "type": "object", "properties": { - "inner": { - "allOf": [ - { - "$ref": "#/components/schemas/Inner" - } - ], - "nullable": true - }, "int": { "type": "integer", "format": "int32", "example": 8 }, - "value": {}, "values": { "type": "object", "additionalProperties": true + }, + "value": {}, + "inner": { + "nullable": true, + "allOf": [ + { + "$ref": "#/components/schemas/Inner" + } + ] } }, "required": [ @@ -49,10 +49,10 @@ "properties": { "ValueNewType": {} }, - "additionalProperties": false, "required": [ "ValueNewType" - ] + ], + "additionalProperties": false } ] } diff --git a/schemars/tests/expected/schema_settings.json b/schemars/tests/expected/schema_settings.json index 6a66ca0..4a50262 100644 --- a/schemars/tests/expected/schema_settings.json +++ b/schemars/tests/expected/schema_settings.json @@ -3,6 +3,19 @@ "title": "Outer", "type": "object", "properties": { + "int": { + "type": "integer", + "format": "int32", + "examples": [ + 8, + null + ] + }, + "values": { + "type": "object", + "additionalProperties": true + }, + "value": true, "inner": { "anyOf": [ { @@ -12,19 +25,6 @@ "type": "null" } ] - }, - "int": { - "type": "integer", - "format": "int32", - "examples": [ - 8, - null - ] - }, - "value": true, - "values": { - "type": "object", - "additionalProperties": true } }, "required": [ @@ -52,10 +52,10 @@ "properties": { "ValueNewType": true }, - "additionalProperties": false, "required": [ "ValueNewType" - ] + ], + "additionalProperties": false } ] } diff --git a/schemars/tests/expected/schema_with-enum-adjacent-tagged.json b/schemars/tests/expected/schema_with-enum-adjacent-tagged.json index 5b95671..ab848e3 100644 --- a/schemars/tests/expected/schema_with-enum-adjacent-tagged.json +++ b/schemars/tests/expected/schema_with-enum-adjacent-tagged.json @@ -5,6 +5,12 @@ { "type": "object", "properties": { + "t": { + "type": "string", + "enum": [ + "Struct" + ] + }, "c": { "type": "object", "properties": { @@ -15,12 +21,6 @@ "required": [ "foo" ] - }, - "t": { - "type": "string", - "enum": [ - "Struct" - ] } }, "required": [ @@ -31,14 +31,14 @@ { "type": "object", "properties": { - "c": { - "type": "boolean" - }, "t": { "type": "string", "enum": [ "NewType" ] + }, + "c": { + "type": "boolean" } }, "required": [ @@ -49,6 +49,12 @@ { "type": "object", "properties": { + "t": { + "type": "string", + "enum": [ + "Tuple" + ] + }, "c": { "type": "array", "items": [ @@ -60,14 +66,8 @@ "format": "int32" } ], - "maxItems": 2, - "minItems": 2 - }, - "t": { - "type": "string", - "enum": [ - "Tuple" - ] + "minItems": 2, + "maxItems": 2 } }, "required": [ @@ -78,14 +78,14 @@ { "type": "object", "properties": { - "c": { - "type": "boolean" - }, "t": { "type": "string", "enum": [ "Unit" ] + }, + "c": { + "type": "boolean" } }, "required": [ diff --git a/schemars/tests/expected/schema_with-enum-external.json b/schemars/tests/expected/schema_with-enum-external.json index 78b6475..5f28d2f 100644 --- a/schemars/tests/expected/schema_with-enum-external.json +++ b/schemars/tests/expected/schema_with-enum-external.json @@ -17,10 +17,10 @@ ] } }, - "additionalProperties": false, "required": [ "struct" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -29,10 +29,10 @@ "type": "boolean" } }, - "additionalProperties": false, "required": [ "newType" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -48,14 +48,14 @@ "format": "int32" } ], - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 } }, - "additionalProperties": false, "required": [ "tuple" - ] + ], + "additionalProperties": false }, { "type": "object", @@ -64,10 +64,10 @@ "type": "boolean" } }, - "additionalProperties": false, "required": [ "unit" - ] + ], + "additionalProperties": false } ] } \ No newline at end of file diff --git a/schemars/tests/expected/schema_with-enum-untagged.json b/schemars/tests/expected/schema_with-enum-untagged.json index b912feb..834eb41 100644 --- a/schemars/tests/expected/schema_with-enum-untagged.json +++ b/schemars/tests/expected/schema_with-enum-untagged.json @@ -27,8 +27,8 @@ "format": "int32" } ], - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 }, { "type": "boolean" diff --git a/schemars/tests/expected/schema_with-struct.json b/schemars/tests/expected/schema_with-struct.json index 7cc55bf..31caebe 100644 --- a/schemars/tests/expected/schema_with-struct.json +++ b/schemars/tests/expected/schema_with-struct.json @@ -3,15 +3,15 @@ "title": "Struct", "type": "object", "properties": { + "foo": { + "type": "boolean" + }, "bar": { "type": "integer", "format": "int32" }, "baz": { "type": "boolean" - }, - "foo": { - "type": "boolean" } }, "required": [ diff --git a/schemars/tests/expected/schema_with-tuple.json b/schemars/tests/expected/schema_with-tuple.json index 1a8f8ca..4e2c095 100644 --- a/schemars/tests/expected/schema_with-tuple.json +++ b/schemars/tests/expected/schema_with-tuple.json @@ -14,6 +14,6 @@ "type": "boolean" } ], - "maxItems": 3, - "minItems": 3 + "minItems": 3, + "maxItems": 3 } \ No newline at end of file diff --git a/schemars/tests/expected/skip_enum_variants.json b/schemars/tests/expected/skip_enum_variants.json index b955e77..d245734 100644 --- a/schemars/tests/expected/skip_enum_variants.json +++ b/schemars/tests/expected/skip_enum_variants.json @@ -16,10 +16,10 @@ "format": "float" } }, - "additionalProperties": false, "required": [ "Included1" - ] + ], + "additionalProperties": false } ] } \ No newline at end of file diff --git a/schemars/tests/expected/skip_struct_fields.json b/schemars/tests/expected/skip_struct_fields.json index 7890298..48549ce 100644 --- a/schemars/tests/expected/skip_struct_fields.json +++ b/schemars/tests/expected/skip_struct_fields.json @@ -3,18 +3,18 @@ "title": "MyStruct", "type": "object", "properties": { - "included": { - "type": "null" - }, "readable": { "type": "string", - "default": "", - "readOnly": true + "readOnly": true, + "default": "" }, "writable": { "type": "number", "format": "float", "writeOnly": true + }, + "included": { + "type": "null" } }, "required": [ diff --git a/schemars/tests/expected/skip_tuple_fields.json b/schemars/tests/expected/skip_tuple_fields.json index 9e5745b..3cdce84 100644 --- a/schemars/tests/expected/skip_tuple_fields.json +++ b/schemars/tests/expected/skip_tuple_fields.json @@ -11,6 +11,6 @@ "type": "null" } ], - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 } \ No newline at end of file diff --git a/schemars/tests/expected/struct-normal-additional-properties.json b/schemars/tests/expected/struct-normal-additional-properties.json index 5ff105c..529e2c4 100644 --- a/schemars/tests/expected/struct-normal-additional-properties.json +++ b/schemars/tests/expected/struct-normal-additional-properties.json @@ -3,6 +3,10 @@ "title": "Struct", "type": "object", "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, "bar": { "type": "boolean" }, @@ -11,10 +15,6 @@ "string", "null" ] - }, - "foo": { - "type": "integer", - "format": "int32" } }, "additionalProperties": false, diff --git a/schemars/tests/expected/struct-normal.json b/schemars/tests/expected/struct-normal.json index 0a5a0af..3e73393 100644 --- a/schemars/tests/expected/struct-normal.json +++ b/schemars/tests/expected/struct-normal.json @@ -3,6 +3,10 @@ "title": "Struct", "type": "object", "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, "bar": { "type": "boolean" }, @@ -11,10 +15,6 @@ "string", "null" ] - }, - "foo": { - "type": "integer", - "format": "int32" } }, "required": [ diff --git a/schemars/tests/expected/struct-tuple.json b/schemars/tests/expected/struct-tuple.json index ced169e..c89e416 100644 --- a/schemars/tests/expected/struct-tuple.json +++ b/schemars/tests/expected/struct-tuple.json @@ -17,6 +17,6 @@ ] } ], - "maxItems": 3, - "minItems": 3 + "minItems": 3, + "maxItems": 3 } \ No newline at end of file diff --git a/schemars/tests/expected/transparent-struct.json b/schemars/tests/expected/transparent-struct.json index e83f905..7fc1c32 100644 --- a/schemars/tests/expected/transparent-struct.json +++ b/schemars/tests/expected/transparent-struct.json @@ -26,8 +26,8 @@ "format": "int32" } ], - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 } } } \ No newline at end of file diff --git a/schemars/tests/expected/validate.json b/schemars/tests/expected/validate.json index 2d6e43c..04131ce 100644 --- a/schemars/tests/expected/validate.json +++ b/schemars/tests/expected/validate.json @@ -3,61 +3,17 @@ "title": "Struct", "type": "object", "properties": { - "contains_str1": { - "type": "string", - "pattern": "substring\\.\\.\\." - }, - "contains_str2": { - "type": "string", - "pattern": "substring\\.\\.\\." - }, - "email_address": { - "type": "string", - "format": "email" - }, - "homepage": { - "type": "string", - "format": "uri" - }, - "map_contains": { - "type": "object", - "additionalProperties": { - "type": "null" - }, - "required": [ - "map_key" - ] - }, "min_max": { "type": "number", "format": "float", - "maximum": 100, - "minimum": 0.01 + "minimum": 0.01, + "maximum": 100 }, "min_max2": { "type": "number", "format": "float", - "maximum": 1000, - "minimum": 1 - }, - "non_empty_str": { - "type": "string", - "maxLength": 100, - "minLength": 1 - }, - "non_empty_str2": { - "type": "string", - "maxLength": 1000, - "minLength": 1 - }, - "pair": { - "type": "array", - "items": { - "type": "integer", - "format": "int32" - }, - "maxItems": 2, - "minItems": 2 + "minimum": 1, + "maximum": 1000 }, "regex_str1": { "type": "string", @@ -71,13 +27,57 @@ "type": "string", "pattern": "^\\d+$" }, - "required_option": { - "type": "boolean" + "contains_str1": { + "type": "string", + "pattern": "substring\\.\\.\\." + }, + "contains_str2": { + "type": "string", + "pattern": "substring\\.\\.\\." + }, + "email_address": { + "type": "string", + "format": "email" }, "tel": { "type": "string", "format": "phone" }, + "homepage": { + "type": "string", + "format": "uri" + }, + "non_empty_str": { + "type": "string", + "minLength": 1, + "maxLength": 100 + }, + "non_empty_str2": { + "type": "string", + "minLength": 1, + "maxLength": 1000 + }, + "pair": { + "type": "array", + "items": { + "type": "integer", + "format": "int32" + }, + "minItems": 2, + "maxItems": 2 + }, + "map_contains": { + "type": "object", + "additionalProperties": { + "type": "null" + }, + "required": [ + "map_key" + ] + }, + "required_option": { + "type": "boolean" + }, "x": { "type": "integer", "format": "int32" diff --git a/schemars/tests/expected/validate_inner.json b/schemars/tests/expected/validate_inner.json index f8a7eca..ba4ee9e 100644 --- a/schemars/tests/expected/validate_inner.json +++ b/schemars/tests/expected/validate_inner.json @@ -7,11 +7,11 @@ "type": "array", "items": { "type": "string", - "maxLength": 100, - "minLength": 5 + "minLength": 5, + "maxLength": 100 }, - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 }, "slice_str_contains": { "type": "array", @@ -20,33 +20,6 @@ "pattern": "substring\\.\\.\\." } }, - "vec_i32_range": { - "type": "array", - "items": { - "type": "integer", - "format": "int32", - "maximum": 10, - "minimum": -10 - } - }, - "vec_str_length": { - "type": "array", - "items": { - "type": "string", - "maxLength": 100, - "minLength": 1 - } - }, - "vec_str_length2": { - "type": "array", - "items": { - "type": "string", - "maxLength": 100, - "minLength": 1 - }, - "maxItems": 3, - "minItems": 1 - }, "vec_str_regex": { "type": "array", "items": { @@ -54,12 +27,39 @@ "pattern": "^[Hh]ello\\b" } }, + "vec_str_length": { + "type": "array", + "items": { + "type": "string", + "minLength": 1, + "maxLength": 100 + } + }, + "vec_str_length2": { + "type": "array", + "items": { + "type": "string", + "minLength": 1, + "maxLength": 100 + }, + "minItems": 1, + "maxItems": 3 + }, "vec_str_url": { "type": "array", "items": { "type": "string", "format": "uri" } + }, + "vec_i32_range": { + "type": "array", + "items": { + "type": "integer", + "format": "int32", + "minimum": -10, + "maximum": 10 + } } }, "required": [ diff --git a/schemars/tests/expected/validate_newtype.json b/schemars/tests/expected/validate_newtype.json index 4c1146e..89bec96 100644 --- a/schemars/tests/expected/validate_newtype.json +++ b/schemars/tests/expected/validate_newtype.json @@ -3,6 +3,6 @@ "title": "NewType", "type": "integer", "format": "uint8", - "maximum": 10, - "minimum": 0 + "minimum": 0, + "maximum": 10 } \ No newline at end of file diff --git a/schemars/tests/expected/validate_schemars_attrs.json b/schemars/tests/expected/validate_schemars_attrs.json index fa2dbcd..4fdba53 100644 --- a/schemars/tests/expected/validate_schemars_attrs.json +++ b/schemars/tests/expected/validate_schemars_attrs.json @@ -3,61 +3,17 @@ "title": "Struct2", "type": "object", "properties": { - "contains_str1": { - "type": "string", - "pattern": "substring\\.\\.\\." - }, - "contains_str2": { - "type": "string", - "pattern": "substring\\.\\.\\." - }, - "email_address": { - "type": "string", - "format": "email" - }, - "homepage": { - "type": "string", - "format": "uri" - }, - "map_contains": { - "type": "object", - "additionalProperties": { - "type": "null" - }, - "required": [ - "map_key" - ] - }, "min_max": { "type": "number", "format": "float", - "maximum": 100, - "minimum": 0.01 + "minimum": 0.01, + "maximum": 100 }, "min_max2": { "type": "number", "format": "float", - "maximum": 1000, - "minimum": 1 - }, - "non_empty_str": { - "type": "string", - "maxLength": 100, - "minLength": 1 - }, - "non_empty_str2": { - "type": "string", - "maxLength": 1000, - "minLength": 1 - }, - "pair": { - "type": "array", - "items": { - "type": "integer", - "format": "int32" - }, - "maxItems": 2, - "minItems": 2 + "minimum": 1, + "maximum": 1000 }, "regex_str1": { "type": "string", @@ -71,13 +27,57 @@ "type": "string", "pattern": "^\\d+$" }, - "required_option": { - "type": "boolean" + "contains_str1": { + "type": "string", + "pattern": "substring\\.\\.\\." + }, + "contains_str2": { + "type": "string", + "pattern": "substring\\.\\.\\." + }, + "email_address": { + "type": "string", + "format": "email" }, "tel": { "type": "string", "format": "phone" }, + "homepage": { + "type": "string", + "format": "uri" + }, + "non_empty_str": { + "type": "string", + "minLength": 1, + "maxLength": 100 + }, + "non_empty_str2": { + "type": "string", + "minLength": 1, + "maxLength": 1000 + }, + "pair": { + "type": "array", + "items": { + "type": "integer", + "format": "int32" + }, + "minItems": 2, + "maxItems": 2 + }, + "map_contains": { + "type": "object", + "additionalProperties": { + "type": "null" + }, + "required": [ + "map_key" + ] + }, + "required_option": { + "type": "boolean" + }, "x": { "type": "integer", "format": "int32" diff --git a/schemars/tests/expected/validate_tuple.json b/schemars/tests/expected/validate_tuple.json index 619b8c4..2a0e266 100644 --- a/schemars/tests/expected/validate_tuple.json +++ b/schemars/tests/expected/validate_tuple.json @@ -6,13 +6,13 @@ { "type": "integer", "format": "uint8", - "maximum": 10, - "minimum": 0 + "minimum": 0, + "maximum": 10 }, { "type": "boolean" } ], - "maxItems": 2, - "minItems": 2 + "minItems": 2, + "maxItems": 2 } \ No newline at end of file