Add unit tests for Option schemas
This commit is contained in:
parent
00148bdf57
commit
45e37a9c81
3 changed files with 67 additions and 4 deletions
|
@ -73,4 +73,17 @@ mod tests {
|
||||||
assert_eq!(schema.extensions.get("maxItems"), Some(&json!(8)));
|
assert_eq!(schema.extensions.get("maxItems"), Some(&json!(8)));
|
||||||
assert_eq!(schema.items, Some(SingleOrVec::from(schema_for::<i32>())));
|
assert_eq!(schema.items, Some(SingleOrVec::from(schema_for::<i32>())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SomeStruct does not implement JsonSchema
|
||||||
|
struct SomeStruct;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn schema_for_empty_array() {
|
||||||
|
let schema = schema_object_for::<[SomeStruct; 0]>();
|
||||||
|
assert_eq!(
|
||||||
|
schema.instance_type,
|
||||||
|
Some(SingleOrVec::from(InstanceType::Array))
|
||||||
|
);
|
||||||
|
assert_eq!(schema.extensions.get("maxItems"), Some(&json!(0)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,3 +47,39 @@ impl<T: ?Sized> JsonSchema for std::marker::PhantomData<T> {
|
||||||
<()>::json_schema(gen)
|
<()>::json_schema(gen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::gen::*;
|
||||||
|
use crate::tests::{custom_schema_object_for, schema_for, schema_object_for};
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn schema_for_option() {
|
||||||
|
let schema = schema_object_for::<Option<i32>>();
|
||||||
|
assert_eq!(schema.instance_type, None);
|
||||||
|
assert_eq!(schema.extensions.get("nullable"), None);
|
||||||
|
assert_eq!(schema.any_of.is_some(), true);
|
||||||
|
let any_of = schema.any_of.unwrap();
|
||||||
|
assert_eq!(any_of.len(), 2);
|
||||||
|
assert_eq!(any_of[0], schema_for::<i32>());
|
||||||
|
assert_eq!(any_of[1], schema_for::<()>());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn schema_for_option_with_nullable() {
|
||||||
|
let settings = SchemaSettings {
|
||||||
|
option_nullable: true,
|
||||||
|
option_add_null_type: false,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let schema = custom_schema_object_for::<Option<i32>>(settings);
|
||||||
|
assert_eq!(
|
||||||
|
schema.instance_type,
|
||||||
|
Some(SingleOrVec::from(InstanceType::Integer))
|
||||||
|
);
|
||||||
|
assert_eq!(schema.extensions.get("nullable"), Some(&json!(true)));
|
||||||
|
assert_eq!(schema.any_of.is_none(), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -28,8 +28,22 @@ pub trait JsonSchema {
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
pub fn schema_object_for<T: JsonSchema>() -> schema::SchemaObject {
|
||||||
|
schema_object(schema_for::<T>())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn custom_schema_object_for<T: JsonSchema>(
|
||||||
|
settings: gen::SchemaSettings,
|
||||||
|
) -> schema::SchemaObject {
|
||||||
|
schema_object(custom_schema_for::<T>(settings))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn schema_for<T: JsonSchema>() -> schema::Schema {
|
pub fn schema_for<T: JsonSchema>() -> schema::Schema {
|
||||||
match T::json_schema(&mut gen::SchemaGenerator::default()) {
|
custom_schema_for::<T>(Default::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn custom_schema_for<T: JsonSchema>(settings: gen::SchemaSettings) -> schema::Schema {
|
||||||
|
match T::json_schema(&mut gen::SchemaGenerator::new(settings)) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(e) => panic!(
|
Err(e) => panic!(
|
||||||
"Couldn't generate schema object for {}: {}",
|
"Couldn't generate schema object for {}: {}",
|
||||||
|
@ -39,10 +53,10 @@ pub mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn schema_object_for<T: JsonSchema>() -> schema::SchemaObject {
|
pub fn schema_object(schema: schema::Schema) -> schema::SchemaObject {
|
||||||
match schema_for::<T>() {
|
match schema {
|
||||||
schema::Schema::Object(o) => o,
|
schema::Schema::Object(o) => o,
|
||||||
s => panic!("Schema for {} was not an object: {:?}", T::schema_name(), s),
|
s => panic!("Schema was not an object: {:?}", s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue