diff --git a/schemars/.gitignore b/schemars/.gitignore index 6936990..67d434b 100644 --- a/schemars/.gitignore +++ b/schemars/.gitignore @@ -1,3 +1,4 @@ /target **/*.rs.bk Cargo.lock +/tests/*.actual.* \ No newline at end of file diff --git a/schemars/src/lib.rs b/schemars/src/lib.rs index a288e1b..587a0ee 100644 --- a/schemars/src/lib.rs +++ b/schemars/src/lib.rs @@ -2,7 +2,6 @@ pub mod gen; pub mod make_schema; pub mod schema; -pub use schema::{Schema, SchemaObject, SchemaRef}; pub use make_schema::MakeSchema; pub use schemars_derive::*; diff --git a/schemars/src/main.rs b/schemars/src/main.rs index fd620bb..d7e4774 100644 --- a/schemars/src/main.rs +++ b/schemars/src/main.rs @@ -3,7 +3,7 @@ use serde_json::Result; fn main() -> Result<()> { let gen = gen::SchemaGenerator::new(); - let schema = gen.into_root_schema_for::(); + let schema = gen.into_root_schema_for::(); let json = serde_json::to_string_pretty(&schema)?; println!("{}", json); diff --git a/schemars/src/schema.rs b/schemars/src/schema.rs index 595bd18..589b05f 100644 --- a/schemars/src/schema.rs +++ b/schemars/src/schema.rs @@ -1,5 +1,5 @@ use crate as schemars; -use schemars::MakeSchema; +use crate::MakeSchema; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::BTreeMap as Map; @@ -39,7 +39,7 @@ pub struct SchemaRef { } #[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, MakeSchema)] -#[serde(rename_all = "camelCase")] +#[serde(rename_all = "camelCase", default)] pub struct SchemaObject { #[serde(rename = "$schema", skip_serializing_if = "Option::is_none")] pub schema: Option, diff --git a/schemars/tests/schema.json b/schemars/tests/schema.json new file mode 100644 index 0000000..b39d9a5 --- /dev/null +++ b/schemars/tests/schema.json @@ -0,0 +1,267 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "schemars__schema__Schema", + "anyOf": [ + { + "properties": { + "Bool": { + "type": "boolean" + } + } + }, + { + "properties": { + "Ref": { + "$ref": "#/definitions/schemars__schema__SchemaRef" + } + } + }, + { + "properties": { + "Object": { + "$ref": "#/definitions/schemars__schema__SchemaObject" + } + } + } + ], + "definitions": { + "schemars__schema__InstanceType": { + "enum": [ + "null", + "boolean", + "object", + "array", + "number", + "string", + "integer" + ] + }, + "schemars__schema__Schema": { + "anyOf": [ + { + "properties": { + "Bool": { + "type": "boolean" + } + } + }, + { + "properties": { + "Ref": { + "$ref": "#/definitions/schemars__schema__SchemaRef" + } + } + }, + { + "properties": { + "Object": { + "$ref": "#/definitions/schemars__schema__SchemaObject" + } + } + } + ] + }, + "schemars__schema__SchemaObject": { + "properties": { + "$id": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "$schema": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "allOf": { + "anyOf": [ + { + "type": "array", + "items": { + "$ref": "#/definitions/schemars__schema__Schema" + } + }, + { + "type": "null" + } + ] + }, + "anyOf": { + "anyOf": [ + { + "type": "array", + "items": { + "$ref": "#/definitions/schemars__schema__Schema" + } + }, + { + "type": "null" + } + ] + }, + "definitions": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/schemars__schema__Schema" + } + }, + "description": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "enum": { + "anyOf": [ + { + "type": "array", + "items": true + }, + { + "type": "null" + } + ] + }, + "extensions": { + "type": "object", + "additionalProperties": true + }, + "items": { + "anyOf": [ + { + "$ref": "#/definitions/schemars__schema__SingleOrVec_schemars__schema__Schema_" + }, + { + "type": "null" + } + ] + }, + "not": { + "anyOf": [ + { + "$ref": "#/definitions/schemars__schema__Schema" + }, + { + "type": "null" + } + ] + }, + "oneOf": { + "anyOf": [ + { + "type": "array", + "items": { + "$ref": "#/definitions/schemars__schema__Schema" + } + }, + { + "type": "null" + } + ] + }, + "properties": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/schemars__schema__Schema" + } + }, + "required": { + "anyOf": [ + { + "type": "array", + "items": { + "type": "string" + } + }, + { + "type": "null" + } + ] + }, + "title": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "type": { + "anyOf": [ + { + "$ref": "#/definitions/schemars__schema__SingleOrVec_schemars__schema__InstanceType_" + }, + { + "type": "null" + } + ] + } + } + }, + "schemars__schema__SchemaRef": { + "properties": { + "$ref": { + "type": "string" + } + } + }, + "schemars__schema__SingleOrVec_schemars__schema__InstanceType_": { + "anyOf": [ + { + "properties": { + "Single": { + "$ref": "#/definitions/schemars__schema__InstanceType" + } + } + }, + { + "properties": { + "Vec": { + "type": "array", + "items": { + "$ref": "#/definitions/schemars__schema__InstanceType" + } + } + } + } + ] + }, + "schemars__schema__SingleOrVec_schemars__schema__Schema_": { + "anyOf": [ + { + "properties": { + "Single": { + "$ref": "#/definitions/schemars__schema__Schema" + } + } + }, + { + "properties": { + "Vec": { + "type": "array", + "items": { + "$ref": "#/definitions/schemars__schema__Schema" + } + } + } + } + ] + } + } +} \ No newline at end of file diff --git a/schemars/tests/test.rs b/schemars/tests/test.rs new file mode 100644 index 0000000..3f3087c --- /dev/null +++ b/schemars/tests/test.rs @@ -0,0 +1,22 @@ +use schemars::gen::SchemaGenerator; +use schemars::schema::*; +use serde_json::{from_str, to_string_pretty}; +use std::fs; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn schema_matches() -> Result<(), Box> { + let expected_json = fs::read_to_string("tests/schema.json")?; + let expected: Schema = from_str(&expected_json)?; + + let gen = SchemaGenerator::new(); + let actual = gen.into_root_schema_for::(); + fs::write("tests/schema.actual.json", to_string_pretty(&actual)?)?; + + assert_eq!(actual, expected, "\n\nGenerated schema did not match saved schema - generated schema has been written to \"tests/schema.actual.json\"."); + Ok(()) + } +} diff --git a/schemars_derive/src/lib.rs b/schemars_derive/src/lib.rs index 09a3335..4c07590 100644 --- a/schemars_derive/src/lib.rs +++ b/schemars_derive/src/lib.rs @@ -34,7 +34,7 @@ pub fn derive_make_schema(input: proc_macro::TokenStream) -> proc_macro::TokenSt let impl_block = quote! { #[automatically_derived] impl #impl_generics schemars::MakeSchema for #name #ty_generics #where_clause { - fn make_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::Schema { + fn make_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { #schema } }; @@ -52,7 +52,7 @@ fn add_trait_bounds(generics: &mut Generics) { fn wrap_schema_fields(schema_contents: TokenStream) -> TokenStream { quote! { - schemars::SchemaObject { + schemars::schema::SchemaObject { #schema_contents ..Default::default() }