diff --git a/Cargo.lock b/Cargo.lock index 33852dd..d19ad13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -415,6 +415,26 @@ dependencies = [ "rand_core", ] +[[package]] +name = "ref-cast" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a0ae411dbe946a674d89546582cea4ba2bb8defac896622d6496f14c23ba5cf" +dependencies = [ + "ref-cast-impl", +] + +[[package]] +name = "ref-cast-impl" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1165225c21bff1f3bbce98f5a1f889949bc902d3575308cc7b0de30b4f6d27c7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.90", +] + [[package]] name = "regex-syntax" version = "0.8.5" @@ -490,11 +510,12 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "schemars" -version = "0.8.21" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09c024468a378b7e36765cd36702b7a90cc3cba11654f6685c8f233408e89e92" +checksum = "82d20c4491bc164fa2f6c5d44565947a52ad80b9505d8e36f8d54c27c739fcd0" dependencies = [ "dyn-clone", + "ref-cast", "schemars_derive", "serde", "serde_json", @@ -502,9 +523,9 @@ dependencies = [ [[package]] name = "schemars_derive" -version = "0.8.21" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1eee588578aff73f856ab961cd2f79e36bc45d7ded33a7562adba4667aecc0e" +checksum = "33d020396d1d138dc19f1165df7545479dcd58d93810dc5d646a16e55abefa80" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 61de11f..294d885 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ num-traits = { version = "0.2.9", default-features = false } proptest = { version = "1.0.0", optional = true } rand = { version = "0.8.3", optional = true, default-features = false } rkyv = { version = "0.7.41", optional = true, default-features = false, features = ["rend"] } -schemars = { version = "0.8.8", optional = true } +schemars = { version = "1.0", optional = true } serde = { version = "1.0", optional = true, default-features = false } speedy = { version = "0.8.3", optional = true, default-features = false } diff --git a/src/lib.rs b/src/lib.rs index 00d64a7..ef30d54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2440,28 +2440,27 @@ mod impl_borsh { #[cfg(all(feature = "std", feature = "schemars"))] mod impl_schemars { extern crate schemars; - use self::schemars::gen::SchemaGenerator; - use self::schemars::schema::{InstanceType, Schema, SchemaObject}; + use self::schemars::generate::SchemaGenerator; + use self::schemars::Schema; use super::{NotNan, OrderedFloat}; macro_rules! primitive_float_impl { ($type:ty, $schema_name:literal) => { impl schemars::JsonSchema for $type { - fn is_referenceable() -> bool { - false + fn inline_schema() -> bool { + true } - fn schema_name() -> std::string::String { - std::string::String::from($schema_name) + fn schema_name() -> std::borrow::Cow<'static, str> { + std::borrow::Cow::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() + schemars::json_schema!({ + "type": "number", + "title": $schema_name, + "format": $schema_name, + }) } } }; @@ -2474,93 +2473,34 @@ mod impl_schemars { #[test] fn schema_generation_does_not_panic_for_common_floats() { - { - let schema = schemars::gen::SchemaGenerator::default() - .into_root_schema_for::>(); - 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::>(); - 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::>(); - 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::>(); - 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") - ); + fn test(title: &str) { + let schema = schemars::generate::SchemaGenerator::default().into_root_schema_for::(); + + assert_eq!(schema.get("type").unwrap().as_str().unwrap(), "number"); + assert_eq!(schema.get("title").unwrap().as_str().unwrap(), title); + assert_eq!(schema.get("format").unwrap().as_str().unwrap(), title); } + + test::>("float"); + test::>("float"); + test::>("double"); + test::>("double"); } + #[test] fn ordered_float_schema_match_primitive_schema() { - { - let of_schema = schemars::gen::SchemaGenerator::default() - .into_root_schema_for::>(); - let prim_schema = - schemars::gen::SchemaGenerator::default().into_root_schema_for::(); - assert_eq!(of_schema, prim_schema); - } - { - let of_schema = schemars::gen::SchemaGenerator::default() - .into_root_schema_for::>(); - let prim_schema = - schemars::gen::SchemaGenerator::default().into_root_schema_for::(); - assert_eq!(of_schema, prim_schema); - } - { - let of_schema = - schemars::gen::SchemaGenerator::default().into_root_schema_for::>(); - let prim_schema = - schemars::gen::SchemaGenerator::default().into_root_schema_for::(); - assert_eq!(of_schema, prim_schema); - } - { + fn test() { let of_schema = - schemars::gen::SchemaGenerator::default().into_root_schema_for::>(); + schemars::generate::SchemaGenerator::default().into_root_schema_for::(); let prim_schema = - schemars::gen::SchemaGenerator::default().into_root_schema_for::(); + schemars::generate::SchemaGenerator::default().into_root_schema_for::(); assert_eq!(of_schema, prim_schema); } + + test::, f32>(); + test::, f32>(); + test::, f64>(); + test::, f64>(); } }