Skip to content

Commit a4988ae

Browse files
committed
add schemars 1.2 support
1 parent 37865d1 commit a4988ae

3 files changed

Lines changed: 141 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 48 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ rand = { version = "0.8.3", optional = true, default-features = false }
2626
rkyv = { version = "0.7.41", optional = true, default-features = false, features = ["rend"] }
2727
rkyv_08 = { version = "0.8", optional = true, default-features = false, package = "rkyv" }
2828
schemars = { version = "0.8.8", optional = true }
29+
schemars1 = { package = "schemars", version = "1.2", optional = true }
2930
serde = { version = "1.0", optional = true, default-features = false }
3031
speedy = { version = "0.8.3", optional = true, default-features = false }
3132

src/lib.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,6 +2690,98 @@ mod impl_schemars {
26902690
}
26912691
}
26922692

2693+
#[cfg(all(feature = "std", feature = "schemars1"))]
2694+
mod impl_schemars1 {
2695+
extern crate schemars1 as schemars;
2696+
use self::schemars::generate::SchemaGenerator;
2697+
use self::schemars::Schema;
2698+
use super::{NotNan, OrderedFloat};
2699+
2700+
macro_rules! primitive_float_impl {
2701+
($type:ty, $schema_name:literal) => {
2702+
impl schemars::JsonSchema for $type {
2703+
fn inline_schema() -> bool {
2704+
true
2705+
}
2706+
2707+
fn schema_id() -> std::borrow::Cow<'static, str> {
2708+
concat!(module_path!(), "::", core::stringify!($type)).into()
2709+
}
2710+
2711+
fn schema_name() -> std::borrow::Cow<'static, str> {
2712+
std::borrow::Cow::from($schema_name)
2713+
}
2714+
2715+
fn json_schema(_: &mut SchemaGenerator) -> Schema {
2716+
schemars1::json_schema!({
2717+
"type": "number",
2718+
"title": $schema_name,
2719+
"format": $schema_name,
2720+
})
2721+
}
2722+
}
2723+
};
2724+
}
2725+
2726+
primitive_float_impl!(OrderedFloat<f32>, "float");
2727+
primitive_float_impl!(OrderedFloat<f64>, "double");
2728+
primitive_float_impl!(NotNan<f32>, "float");
2729+
primitive_float_impl!(NotNan<f64>, "double");
2730+
2731+
#[test]
2732+
fn schema_generation_does_not_panic_for_common_floats() {
2733+
fn test_schema_properties<T: schemars::JsonSchema>(title: &str) {
2734+
let schema = schemars::generate::SchemaGenerator::default().into_root_schema_for::<T>();
2735+
2736+
assert_eq!(
2737+
schema
2738+
.get("type")
2739+
.expect("schema defines `type` key")
2740+
.as_str()
2741+
.expect("value for the `type` key is a string"),
2742+
"number"
2743+
);
2744+
assert_eq!(
2745+
schema
2746+
.get("title")
2747+
.expect("schema defines `title` key")
2748+
.as_str()
2749+
.expect("value for the `title` key is a string"),
2750+
title
2751+
);
2752+
assert_eq!(
2753+
schema
2754+
.get("format")
2755+
.expect("schema defines `format` key")
2756+
.as_str()
2757+
.expect("value for the `format` key is a string"),
2758+
title
2759+
);
2760+
}
2761+
2762+
test_schema_properties::<OrderedFloat<f32>>("float");
2763+
test_schema_properties::<NotNan<f32>>("float");
2764+
test_schema_properties::<OrderedFloat<f64>>("double");
2765+
test_schema_properties::<NotNan<f64>>("double");
2766+
}
2767+
2768+
#[test]
2769+
fn ordered_float_schema_match_primitive_schema() {
2770+
fn test_schema_eq<Wrapped: schemars::JsonSchema, Inner: schemars::JsonSchema>() {
2771+
let wrapped_schema =
2772+
schemars::generate::SchemaGenerator::default().into_root_schema_for::<Wrapped>();
2773+
let primitive_schema =
2774+
schemars::generate::SchemaGenerator::default().into_root_schema_for::<Inner>();
2775+
assert_eq!(wrapped_schema, primitive_schema);
2776+
}
2777+
2778+
test_schema_eq::<OrderedFloat<f32>, f32>();
2779+
test_schema_eq::<NotNan<f32>, f32>();
2780+
test_schema_eq::<OrderedFloat<f64>, f64>();
2781+
test_schema_eq::<NotNan<f64>, f64>();
2782+
}
2783+
}
2784+
26932785
#[cfg(feature = "rand")]
26942786
mod impl_rand {
26952787
use super::{NotNan, OrderedFloat};

0 commit comments

Comments
 (0)