Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional JsonSchema support #78

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/tests.yaml
Expand Up @@ -33,6 +33,12 @@ jobs:
command: test
args: --no-default-features

- name: Test (schema features subset)
uses: actions-rs/cargo@v1
with:
command: test
args: --features "std,schemars"

- name: Test (all features)
uses: actions-rs/cargo@v1
with:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -13,6 +13,7 @@ edition = "2018"
[dependencies]
num-traits = { version = "0.2.1", default-features = false }
serde = { version = "1.0", optional = true, default-features = false }
schemars = { version = "0.6.5", optional = true }

[dev-dependencies]
serde_test = "1.0"
Expand Down
82 changes: 82 additions & 0 deletions src/lib.rs
Expand Up @@ -995,3 +995,85 @@ mod impl_serde {
"invalid value: floating point `NaN`, expected float (but not NaN)");
}
}
#[cfg(all(feature = "std", feature = "schemars"))]
mod impl_schemars {
extern crate schemars;
use super::{OrderedFloat, NotNan};
use self::schemars::schema::{Schema, InstanceType, SchemaObject};
use self::schemars::gen::SchemaGenerator;

macro_rules! primitive_float_impl {
($type:ty, $schema_name:literal) => {
impl schemars::JsonSchema for $type {
fn is_referenceable() -> bool {
false
}

fn schema_name() -> std::string::String {
std::string::String::from($schema_name)
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Number.into()),
format: Some(std::string::String::from($schema_name)),
..Default::default()
}
.into()
}
}
}
}

primitive_float_impl!(OrderedFloat<f32>, "float");
primitive_float_impl!(OrderedFloat<f64>, "double");
primitive_float_impl!(NotNan<f32>, "float");
primitive_float_impl!(NotNan<f64>, "double");

#[test]
fn schema_generation_does_not_panic_for_common_floats() {
{
let schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<OrderedFloat<f32>>();
assert_eq!(schema.schema.instance_type, Some(schemars::schema::SingleOrVec::Single(std::boxed::Box::new(schemars::schema::InstanceType::Number))));
assert_eq!(schema.schema.metadata.unwrap().title.unwrap(), std::string::String::from("float"));
}
{
let schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<OrderedFloat<f64>>();
assert_eq!(schema.schema.instance_type, Some(schemars::schema::SingleOrVec::Single(std::boxed::Box::new(schemars::schema::InstanceType::Number))));
assert_eq!(schema.schema.metadata.unwrap().title.unwrap(), std::string::String::from("double"));
}
{
let schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<NotNan<f32>>();
assert_eq!(schema.schema.instance_type, Some(schemars::schema::SingleOrVec::Single(std::boxed::Box::new(schemars::schema::InstanceType::Number))));
assert_eq!(schema.schema.metadata.unwrap().title.unwrap(), std::string::String::from("float"));
}
{
let schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<NotNan<f64>>();
assert_eq!(schema.schema.instance_type, Some(schemars::schema::SingleOrVec::Single(std::boxed::Box::new(schemars::schema::InstanceType::Number))));
assert_eq!(schema.schema.metadata.unwrap().title.unwrap(), std::string::String::from("double"));
}
}
#[test]
fn ordered_float_schema_match_primitive_schema() {
{
let of_schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<OrderedFloat<f32>>();
let prim_schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<f32>();
assert_eq!(of_schema, prim_schema);
}
{
let of_schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<OrderedFloat<f64>>();
let prim_schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<f64>();
assert_eq!(of_schema, prim_schema);
}
{
let of_schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<NotNan<f32>>();
let prim_schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<f32>();
assert_eq!(of_schema, prim_schema);
}
{
let of_schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<NotNan<f64>>();
let prim_schema = schemars::gen::SchemaGenerator::default().into_root_schema_for::<f64>();
assert_eq!(of_schema, prim_schema);
}
}
}