diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 382c9226..de5049d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: strategy: matrix: rust: - - 1.45.0 + - 1.51.0 - stable - beta - nightly diff --git a/clippy.toml b/clippy.toml index 90bfd5f6..434e968a 100644 --- a/clippy.toml +++ b/clippy.toml @@ -1 +1 @@ -msrv = "1.45.0" +msrv = "1.51.0" diff --git a/schemars/src/json_schema_impls/array.rs b/schemars/src/json_schema_impls/array.rs index 558ddbd9..6accf16e 100644 --- a/schemars/src/json_schema_impls/array.rs +++ b/schemars/src/json_schema_impls/array.rs @@ -2,8 +2,10 @@ use crate::gen::SchemaGenerator; use crate::schema::*; use crate::JsonSchema; +pub struct EmptyArray(pub [T; 0]); + // Does not require T: JsonSchema. -impl JsonSchema for [T; 0] { +impl JsonSchema for EmptyArray { no_ref_schema!(); fn schema_name() -> String { @@ -23,39 +25,29 @@ impl JsonSchema for [T; 0] { } } -macro_rules! array_impls { - ($($len:tt)+) => { - $( - impl JsonSchema for [T; $len] { - no_ref_schema!(); - - fn schema_name() -> String { - format!("Array_size_{}_of_{}", $len, T::schema_name()) - } +impl JsonSchema for [T; N] { + no_ref_schema!(); - fn json_schema(gen: &mut SchemaGenerator) -> Schema { - SchemaObject { - instance_type: Some(InstanceType::Array.into()), - array: Some(Box::new(ArrayValidation { - items: Some(gen.subschema_for::().into()), - max_items: Some($len), - min_items: Some($len), - ..Default::default() - })), - ..Default::default() - } - .into() - } - } - )+ + fn schema_name() -> String { + match N { + 0 => "EmptyArray".to_owned(), + n => format!("Array_size_{}_of_{}", n, T::schema_name()), + } } -} -array_impls! { - 1 2 3 4 5 6 7 8 9 10 - 11 12 13 14 15 16 17 18 19 20 - 21 22 23 24 25 26 27 28 29 30 - 31 32 + fn json_schema(gen: &mut SchemaGenerator) -> Schema { + SchemaObject { + instance_type: Some(InstanceType::Array.into()), + array: Some(Box::new(ArrayValidation { + items: Some(gen.subschema_for::().into()), + max_items: Some(N as u32), + min_items: Some(N as u32), + ..Default::default() + })), + ..Default::default() + } + .into() + } } #[cfg(test)] @@ -85,7 +77,7 @@ mod tests { #[test] fn schema_for_empty_array() { - let schema = schema_object_for::<[SomeStruct; 0]>(); + let schema = schema_object_for::>(); assert_eq!( schema.instance_type, Some(SingleOrVec::from(InstanceType::Array)) diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index 99254423..d89c018a 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -36,6 +36,7 @@ macro_rules! forward_impl { } mod array; +pub use array::EmptyArray; #[cfg(feature = "arrayvec05")] mod arrayvec05; #[cfg(feature = "arrayvec07")] diff --git a/schemars/src/lib.rs b/schemars/src/lib.rs index 2094c428..f22b5e7e 100644 --- a/schemars/src/lib.rs +++ b/schemars/src/lib.rs @@ -310,6 +310,7 @@ pub type MapEntry<'a, K, V> = indexmap::map::Entry<'a, K, V>; mod flatten; mod json_schema_impls; +pub use json_schema_impls::EmptyArray; mod ser; #[macro_use] mod macros;