Implement JsonSchema for OsStr/OsString
This commit is contained in:
parent
e6c178117c
commit
c6a20764fe
6 changed files with 118 additions and 9 deletions
|
@ -98,8 +98,7 @@ impl<T: JsonSchema, E: JsonSchema> JsonSchema for Result<T, E> {
|
|||
.insert("Err".to_owned(), gen.subschema_for::<E>());
|
||||
|
||||
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::<Result<bool, String>>();
|
||||
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::<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();
|
||||
assert!(obj.required.contains("Err"));
|
||||
assert_eq!(obj.properties["Err"], schema_for::<String>());
|
||||
|
|
|
@ -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;
|
||||
|
|
42
schemars/src/json_schema_impls/os.rs
Normal file
42
schemars/src/json_schema_impls/os.rs
Normal 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)
|
||||
}
|
||||
}
|
53
schemars/tests/expected/os_strings.json
Normal file
53
schemars/tests/expected/os_strings.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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": [
|
||||
|
|
15
schemars/tests/os.rs
Normal file
15
schemars/tests/os.rs
Normal 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")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue