From c6a20764fe9b93aa1f7eedbf6244473bfe8d5906 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Sun, 27 Oct 2019 19:26:17 +0000 Subject: [PATCH] Implement JsonSchema for OsStr/OsString --- schemars/src/json_schema_impls/core.rs | 11 +++-- schemars/src/json_schema_impls/mod.rs | 3 +- schemars/src/json_schema_impls/os.rs | 42 ++++++++++++++++++++ schemars/tests/expected/os_strings.json | 53 +++++++++++++++++++++++++ schemars/tests/expected/result.json | 3 +- schemars/tests/os.rs | 15 +++++++ 6 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 schemars/src/json_schema_impls/os.rs create mode 100644 schemars/tests/expected/os_strings.json create mode 100644 schemars/tests/os.rs diff --git a/schemars/src/json_schema_impls/core.rs b/schemars/src/json_schema_impls/core.rs index 8c54892..ccdcd57 100644 --- a/schemars/src/json_schema_impls/core.rs +++ b/schemars/src/json_schema_impls/core.rs @@ -98,8 +98,7 @@ impl JsonSchema for Result { .insert("Err".to_owned(), gen.subschema_for::()); let mut schema = SchemaObject::default(); - schema.instance_type = Some(InstanceType::Object.into()); - schema.subschemas().any_of = Some(vec![ok_schema.into(), err_schema.into()]); + schema.subschemas().one_of = Some(vec![ok_schema.into(), err_schema.into()]); schema.into() } } @@ -169,15 +168,15 @@ mod tests { #[test] fn schema_for_result() { let schema = schema_object_for::>(); - let any_of = schema.subschemas.unwrap().any_of.unwrap(); - assert_eq!(any_of.len(), 2); + let one_of = schema.subschemas.unwrap().one_of.unwrap(); + assert_eq!(one_of.len(), 2); - let ok_schema: SchemaObject = any_of[0].clone().into(); + let ok_schema: SchemaObject = one_of[0].clone().into(); let obj = ok_schema.object.unwrap(); assert!(obj.required.contains("Ok")); assert_eq!(obj.properties["Ok"], schema_for::()); - let err_schema: SchemaObject = any_of[1].clone().into(); + let err_schema: SchemaObject = one_of[1].clone().into(); let obj = err_schema.object.unwrap(); assert!(obj.required.contains("Err")); assert_eq!(obj.properties["Err"], schema_for::()); diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index f753d5d..42f6426 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -10,10 +10,11 @@ mod array; #[cfg(feature = "chrono")] mod chrono; mod core; -mod wrapper; mod maps; +mod os; mod primitives; mod sequences; mod serdejson; mod time; mod tuple; +mod wrapper; diff --git a/schemars/src/json_schema_impls/os.rs b/schemars/src/json_schema_impls/os.rs new file mode 100644 index 0000000..ec4dcbf --- /dev/null +++ b/schemars/src/json_schema_impls/os.rs @@ -0,0 +1,42 @@ +use crate::gen::SchemaGenerator; +use crate::schema::*; +use crate::JsonSchema; +use std::ffi::{OsStr, OsString}; + +impl JsonSchema for OsString { + fn schema_name() -> String { + "OS_String".to_owned() + } + + fn json_schema(gen: &mut SchemaGenerator) -> Schema { + let mut unix_schema = SchemaObject::default(); + unix_schema.instance_type = Some(InstanceType::Object.into()); + unix_schema.object().required.insert("Unix".to_owned()); + unix_schema + .object() + .properties + .insert("Unix".to_owned(), >::json_schema(gen)); + + let mut win_schema = SchemaObject::default(); + win_schema.instance_type = Some(InstanceType::Object.into()); + win_schema.object().required.insert("Windows".to_owned()); + win_schema + .object() + .properties + .insert("Windows".to_owned(), >::json_schema(gen)); + + let mut schema = SchemaObject::default(); + schema.subschemas().one_of = Some(vec![unix_schema.into(), win_schema.into()]); + schema.into() + } +} + +impl JsonSchema for OsStr { + fn schema_name() -> String { + ::schema_name() + } + + fn json_schema(gen: &mut SchemaGenerator) -> Schema { + ::json_schema(gen) + } +} diff --git a/schemars/tests/expected/os_strings.json b/schemars/tests/expected/os_strings.json new file mode 100644 index 0000000..0611998 --- /dev/null +++ b/schemars/tests/expected/os_strings.json @@ -0,0 +1,53 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "OsStrings", + "type": "object", + "required": [ + "borrowed", + "owned" + ], + "properties": { + "borrowed": { + "$ref": "#/definitions/OS_String" + }, + "owned": { + "$ref": "#/definitions/OS_String" + } + }, + "definitions": { + "OS_String": { + "oneOf": [ + { + "type": "object", + "required": [ + "Unix" + ], + "properties": { + "Unix": { + "type": "array", + "items": { + "type": "integer", + "format": "uint8" + } + } + } + }, + { + "type": "object", + "required": [ + "Windows" + ], + "properties": { + "Windows": { + "type": "array", + "items": { + "type": "integer", + "format": "uint16" + } + } + } + } + ] + } + } +} \ No newline at end of file diff --git a/schemars/tests/expected/result.json b/schemars/tests/expected/result.json index 156f500..42d4642 100644 --- a/schemars/tests/expected/result.json +++ b/schemars/tests/expected/result.json @@ -1,8 +1,7 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "title": "Result_Of_MyStruct_Or_Array_Of_String", - "type": "object", - "anyOf": [ + "oneOf": [ { "type": "object", "required": [ diff --git a/schemars/tests/os.rs b/schemars/tests/os.rs new file mode 100644 index 0000000..25a2196 --- /dev/null +++ b/schemars/tests/os.rs @@ -0,0 +1,15 @@ +mod util; +use schemars::JsonSchema; +use util::*; +use std::ffi::{OsStr, OsString}; + +#[derive(Debug, JsonSchema)] +struct OsStrings { + owned: OsString, + borrowed: &'static OsStr, +} + +#[test] +fn os_strings() -> TestResult { + test_default_generated_schema::("os_strings") +}