Small refactor
This commit is contained in:
parent
0198ede4b6
commit
1605a8a34d
2 changed files with 20 additions and 26 deletions
|
@ -8,19 +8,16 @@ pub struct MyStruct {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(JsonSchema)]
|
#[derive(JsonSchema)]
|
||||||
pub enum MyEnum {
|
pub enum MyEnum {
|
||||||
StringNewType(String),
|
StringNewType(String),
|
||||||
StructVariant {
|
StructVariant { floats: Vec<f32> },
|
||||||
floats: Vec<f32>,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let settings = SchemaSettings {
|
let settings = SchemaSettings::draft07().with(|s| {
|
||||||
option_nullable: true,
|
s.option_nullable = true;
|
||||||
option_add_null_type: false,
|
s.option_add_null_type = false;
|
||||||
..SchemaSettings::draft07()
|
});
|
||||||
};
|
|
||||||
let gen = settings.into_generator();
|
let gen = settings.into_generator();
|
||||||
let schema = gen.into_root_schema_for::<MyStruct>();
|
let schema = gen.into_root_schema_for::<MyStruct>();
|
||||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||||
|
|
|
@ -17,12 +17,13 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
|
||||||
schema = match schema {
|
schema = match schema {
|
||||||
Schema::Bool(true) => Schema::Bool(true),
|
Schema::Bool(true) => Schema::Bool(true),
|
||||||
Schema::Bool(false) => <()>::json_schema(gen),
|
Schema::Bool(false) => <()>::json_schema(gen),
|
||||||
Schema::Object(
|
Schema::Object(SchemaObject {
|
||||||
obj @ SchemaObject {
|
instance_type: Some(ref mut instance_type),
|
||||||
instance_type: Some(_),
|
..
|
||||||
..
|
}) => {
|
||||||
},
|
add_null_type(instance_type);
|
||||||
) => Schema::Object(with_null_type(obj)),
|
schema
|
||||||
|
}
|
||||||
schema => SchemaObject {
|
schema => SchemaObject {
|
||||||
subschemas: Some(Box::new(SubschemaValidation {
|
subschemas: Some(Box::new(SubschemaValidation {
|
||||||
any_of: Some(vec![schema, <()>::json_schema(gen)]),
|
any_of: Some(vec![schema, <()>::json_schema(gen)]),
|
||||||
|
@ -56,18 +57,14 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_null_type(mut obj: SchemaObject) -> SchemaObject {
|
fn add_null_type(instance_type: &mut SingleOrVec<InstanceType>) {
|
||||||
match obj
|
match instance_type {
|
||||||
.instance_type
|
SingleOrVec::Single(ty) if **ty != InstanceType::Null => {
|
||||||
.as_mut()
|
*instance_type = vec![**ty, InstanceType::Null].into()
|
||||||
.expect("checked in calling function")
|
}
|
||||||
{
|
SingleOrVec::Vec(ty) if !ty.contains(&InstanceType::Null) => ty.push(InstanceType::Null),
|
||||||
SingleOrVec::Single(ty) if **ty == InstanceType::Null => {}
|
_ => {}
|
||||||
SingleOrVec::Vec(ty) if ty.contains(&InstanceType::Null) => {}
|
|
||||||
SingleOrVec::Single(ty) => obj.instance_type = Some(vec![**ty, InstanceType::Null].into()),
|
|
||||||
SingleOrVec::Vec(ref mut ty) => ty.push(InstanceType::Null),
|
|
||||||
};
|
};
|
||||||
obj
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: JsonSchema, E: JsonSchema> JsonSchema for Result<T, E> {
|
impl<T: JsonSchema, E: JsonSchema> JsonSchema for Result<T, E> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue