Implement JsonSchema for OsStr/OsString

This commit is contained in:
Graham Esau 2019-10-27 19:26:17 +00:00
parent e6c178117c
commit c6a20764fe
6 changed files with 118 additions and 9 deletions

View file

@ -98,8 +98,7 @@ impl<T: JsonSchema, E: JsonSchema> JsonSchema for Result<T, E> {
.insert("Err".to_owned(), gen.subschema_for::<E>()); .insert("Err".to_owned(), gen.subschema_for::<E>());
let mut schema = SchemaObject::default(); let mut schema = SchemaObject::default();
schema.instance_type = Some(InstanceType::Object.into()); schema.subschemas().one_of = Some(vec![ok_schema.into(), err_schema.into()]);
schema.subschemas().any_of = Some(vec![ok_schema.into(), err_schema.into()]);
schema.into() schema.into()
} }
} }
@ -169,15 +168,15 @@ mod tests {
#[test] #[test]
fn schema_for_result() { fn schema_for_result() {
let schema = schema_object_for::<Result<bool, String>>(); let schema = schema_object_for::<Result<bool, String>>();
let any_of = schema.subschemas.unwrap().any_of.unwrap(); let one_of = schema.subschemas.unwrap().one_of.unwrap();
assert_eq!(any_of.len(), 2); 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(); let obj = ok_schema.object.unwrap();
assert!(obj.required.contains("Ok")); assert!(obj.required.contains("Ok"));
assert_eq!(obj.properties["Ok"], schema_for::<bool>()); assert_eq!(obj.properties["Ok"], schema_for::<bool>());
let err_schema: SchemaObject = any_of[1].clone().into(); let err_schema: SchemaObject = one_of[1].clone().into();
let obj = err_schema.object.unwrap(); let obj = err_schema.object.unwrap();
assert!(obj.required.contains("Err")); assert!(obj.required.contains("Err"));
assert_eq!(obj.properties["Err"], schema_for::<String>()); assert_eq!(obj.properties["Err"], schema_for::<String>());

View file

@ -10,10 +10,11 @@ mod array;
#[cfg(feature = "chrono")] #[cfg(feature = "chrono")]
mod chrono; mod chrono;
mod core; mod core;
mod wrapper;
mod maps; mod maps;
mod os;
mod primitives; mod primitives;
mod sequences; mod sequences;
mod serdejson; mod serdejson;
mod time; mod time;
mod tuple; mod tuple;
mod wrapper;

View file

@ -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(), <Vec<u8>>::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(), <Vec<u16>>::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 {
<OsString>::schema_name()
}
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
<OsString>::json_schema(gen)
}
}

View file

@ -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"
}
}
}
}
]
}
}
}

View file

@ -1,8 +1,7 @@
{ {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"title": "Result_Of_MyStruct_Or_Array_Of_String", "title": "Result_Of_MyStruct_Or_Array_Of_String",
"type": "object", "oneOf": [
"anyOf": [
{ {
"type": "object", "type": "object",
"required": [ "required": [

15
schemars/tests/os.rs Normal file
View file

@ -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::<OsStrings>("os_strings")
}