From 916b3207b6c5d8ffc05288d851fa305c7d55b931 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 16:05:34 -0500 Subject: [PATCH 01/26] correct Decimal implementation; add netidx feature --- .gitignore | 3 +++ schemars/Cargo.toml | 1 + schemars/src/json_schema_impls/decimal.rs | 6 +----- schemars/src/json_schema_impls/mod.rs | 2 ++ schemars/src/json_schema_impls/netidx.rs | 6 ++++++ 5 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 schemars/src/json_schema_impls/netidx.rs diff --git a/.gitignore b/.gitignore index 088ba6ba..40cb88fa 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk + +# IDE +.idea/ diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index ad972dd9..d304b892 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -32,6 +32,7 @@ rust_decimal = { version = "1", default-features = false, optional = true } bigdecimal = { version = "0.3", default-features = false, optional = true } enumset = { version = "1.0", optional = true } smol_str = { version = "0.1.17", optional = true } +netidx-core = { version = "0.18.4", optional = true } [dev-dependencies] pretty_assertions = "1.2.1" diff --git a/schemars/src/json_schema_impls/decimal.rs b/schemars/src/json_schema_impls/decimal.rs index d7fc8a1f..261f2e36 100644 --- a/schemars/src/json_schema_impls/decimal.rs +++ b/schemars/src/json_schema_impls/decimal.rs @@ -8,8 +8,6 @@ macro_rules! decimal_impl { }; ($type:ty => $instance_type:ident, $name:expr) => { impl JsonSchema for $type { - no_ref_schema!(); - fn schema_name() -> String { $name.to_owned() } @@ -26,6 +24,4 @@ macro_rules! decimal_impl { } #[cfg(feature = "rust_decimal")] -decimal_impl!(rust_decimal::Decimal); -#[cfg(feature = "bigdecimal")] -decimal_impl!(bigdecimal::BigDecimal); +decimal_impl!(rust_decimal::Decimal => String, "Decimal"); diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index 490a544e..d951cd9b 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -75,3 +75,5 @@ mod uuid08; #[cfg(feature = "uuid1")] mod uuid1; mod wrapper; +#[cfg(feature = "netidx-core")] +mod netidx; diff --git a/schemars/src/json_schema_impls/netidx.rs b/schemars/src/json_schema_impls/netidx.rs new file mode 100644 index 00000000..85b61f7a --- /dev/null +++ b/schemars/src/json_schema_impls/netidx.rs @@ -0,0 +1,6 @@ +use crate::gen::SchemaGenerator; +use crate::schema::*; +use crate::JsonSchema; +use netidx_core::path::Path; + +forward_impl!((JsonSchema for Path) => String); From 654153813866c5d7e08640055375eaea7a438ef6 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 16:11:44 -0500 Subject: [PATCH 02/26] add netidx Chars --- schemars/src/json_schema_impls/netidx.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/schemars/src/json_schema_impls/netidx.rs b/schemars/src/json_schema_impls/netidx.rs index 85b61f7a..31fe2b8c 100644 --- a/schemars/src/json_schema_impls/netidx.rs +++ b/schemars/src/json_schema_impls/netidx.rs @@ -1,6 +1,7 @@ use crate::gen::SchemaGenerator; use crate::schema::*; use crate::JsonSchema; -use netidx_core::path::Path; +use netidx_core::{path::Path, chars::Chars}; forward_impl!((JsonSchema for Path) => String); +forward_impl!((JsonSchema for Chars) => String); From 5d9731b9f8d5e08396d1a718eb6cf828537c1649 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 16:49:33 -0500 Subject: [PATCH 03/26] add enumflags2 --- schemars/Cargo.toml | 1 + schemars/src/json_schema_impls/enumflags2.rs | 6 ++++++ schemars/src/json_schema_impls/mod.rs | 2 ++ 3 files changed, 9 insertions(+) create mode 100644 schemars/src/json_schema_impls/enumflags2.rs diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index d304b892..f6152f38 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -33,6 +33,7 @@ bigdecimal = { version = "0.3", default-features = false, optional = true } enumset = { version = "1.0", optional = true } smol_str = { version = "0.1.17", optional = true } netidx-core = { version = "0.18.4", optional = true } +enumflags2 = { version = "0.7.7", optional = true } [dev-dependencies] pretty_assertions = "1.2.1" diff --git a/schemars/src/json_schema_impls/enumflags2.rs b/schemars/src/json_schema_impls/enumflags2.rs new file mode 100644 index 00000000..c562f5b7 --- /dev/null +++ b/schemars/src/json_schema_impls/enumflags2.rs @@ -0,0 +1,6 @@ +use crate::gen::SchemaGenerator; +use crate::schema::*; +use crate::JsonSchema; +use enumflags2::BitFlags; + +forward_impl!(( JsonSchema for BitFlags) => u64); diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index d951cd9b..bbb48305 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -77,3 +77,5 @@ mod uuid1; mod wrapper; #[cfg(feature = "netidx-core")] mod netidx; +#[cfg(feature = "enumflags2")] +mod enumflags2; From a1ff764f6296fd4de2b45d6715ec44765ef5c237 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 16:51:02 -0500 Subject: [PATCH 04/26] _ --- schemars/src/json_schema_impls/enumflags2.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schemars/src/json_schema_impls/enumflags2.rs b/schemars/src/json_schema_impls/enumflags2.rs index c562f5b7..6e027b8e 100644 --- a/schemars/src/json_schema_impls/enumflags2.rs +++ b/schemars/src/json_schema_impls/enumflags2.rs @@ -1,6 +1,6 @@ use crate::gen::SchemaGenerator; use crate::schema::*; use crate::JsonSchema; -use enumflags2::BitFlags; +use enumflags2::{BitFlags, _internal::RawBitFlags}; -forward_impl!(( JsonSchema for BitFlags) => u64); +forward_impl!(( JsonSchema for BitFlags where T: RawBitFlags) => u64); From c87ca6867d1e849540f1ba69ddc8dfcd3612712e Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 17:02:31 -0500 Subject: [PATCH 05/26] _ --- schemars/src/json_schema_impls/enumflags2.rs | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/schemars/src/json_schema_impls/enumflags2.rs b/schemars/src/json_schema_impls/enumflags2.rs index 6e027b8e..1df700a6 100644 --- a/schemars/src/json_schema_impls/enumflags2.rs +++ b/schemars/src/json_schema_impls/enumflags2.rs @@ -3,4 +3,31 @@ use crate::schema::*; use crate::JsonSchema; use enumflags2::{BitFlags, _internal::RawBitFlags}; +impl JsonSchema for BitFlags where T: JsonSchema + RawBitFlags { + fn is_referenceable() -> bool { + true + } + + fn schema_name() -> String { + format!("BitFlags_{}", T::schema_name()) + } + + fn json_schema(gen: &mut SchemaGenerator) -> Schema { + let t = gen.subschema_for::(); + let r = u64::json_schema(gen); + match r { + Schema::Object(mut o) => { + match t { + Schema::Object(mut to) => { + o.metadata = to.metadata; + }, + _ => () + } + }, + _ => () + } + r + } +} + forward_impl!(( JsonSchema for BitFlags where T: RawBitFlags) => u64); From 540e4fb315e584eb5a38ce6e3f5a9d5cd0a0ce85 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 17:02:56 -0500 Subject: [PATCH 06/26] _ --- schemars/src/json_schema_impls/enumflags2.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/schemars/src/json_schema_impls/enumflags2.rs b/schemars/src/json_schema_impls/enumflags2.rs index 1df700a6..d83e5abd 100644 --- a/schemars/src/json_schema_impls/enumflags2.rs +++ b/schemars/src/json_schema_impls/enumflags2.rs @@ -29,5 +29,3 @@ impl JsonSchema for BitFlags where T: JsonSchema + RawBitFlags { r } } - -forward_impl!(( JsonSchema for BitFlags where T: RawBitFlags) => u64); From 768545f785a7746c5a5e2849725e59e7f67438a4 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 17:14:09 -0500 Subject: [PATCH 07/26] _ --- schemars/src/json_schema_impls/enumflags2.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/schemars/src/json_schema_impls/enumflags2.rs b/schemars/src/json_schema_impls/enumflags2.rs index d83e5abd..ba1c275a 100644 --- a/schemars/src/json_schema_impls/enumflags2.rs +++ b/schemars/src/json_schema_impls/enumflags2.rs @@ -13,19 +13,14 @@ impl JsonSchema for BitFlags where T: JsonSchema + RawBitFlags { } fn json_schema(gen: &mut SchemaGenerator) -> Schema { - let t = gen.subschema_for::(); - let r = u64::json_schema(gen); - match r { - Schema::Object(mut o) => { - match t { - Schema::Object(mut to) => { - o.metadata = to.metadata; - }, - _ => () - } + let target = gen.subschema_for::(); + let repr = u64::json_schema(gen); + match (repr, target) { + (Schema::Object(mut o), Schema::Object(target)) => { + o.metadata = target.metadata; + Schema::Object(o).into() }, - _ => () + (repr, _) => repr, } - r } } From 2f6ee5122c1e495b7131d8dfab678e72910fea38 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 19:15:10 -0500 Subject: [PATCH 08/26] proc macro for enumflags --- schemars_derive/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/schemars_derive/src/lib.rs b/schemars_derive/src/lib.rs index f8173f42..87e1eb7c 100644 --- a/schemars_derive/src/lib.rs +++ b/schemars_derive/src/lib.rs @@ -163,3 +163,42 @@ fn compile_error(errors: Vec) -> TokenStream { #(#compile_errors)* } } + +#[proc_macro_attribute] +pub fn bitflags_schemars(_attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream { + let ast = syn::parse_macro_input!(item as syn::DeriveInput); + let name = &ast.ident; + let variants = match &ast.data { + syn::Data::Enum(v) => &v.variants, + _ => panic!("bitflags_schemars only works on enums"), + }; + let enum_values: Vec<_> = variants.iter().map(|v| { + let ident = &v.ident; + quote! { json!(BitFlags::from_flag(#name::#ident).bits()) } + }).collect(); + let enum_names: Vec<_> = variants.iter().map(|v| { + let ident = &v.ident; + quote! { stringify!(#ident) } + }).collect(); + let stream = quote! { + impl JsonStream for #name { + fn schema_name() -> String { + stringify!(#name).to_string() + } + + fn json_schema(_gen: &mut SchemaGenerator) -> Schema { + use schemars::schema::{InstanceType, SchemaObject}; + use serde_json::json; + let mut schema = SchemaObject::default(); + schema.instance_type = Some(InstanceType::Number.into()); + schema.enum_values = Some(vec![ #(#enum_values),* ]); + schema.extensions.insert( + "x-enumNames".to_string(), + json!([ #(#enum_names),* ]) + ); + schema.into() + } + } + }; + stream.into() +} From fc519d280a477830943c73730f328c3b62333df0 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 19:46:39 -0500 Subject: [PATCH 09/26] _ --- schemars_derive/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemars_derive/src/lib.rs b/schemars_derive/src/lib.rs index 87e1eb7c..1f1fd634 100644 --- a/schemars_derive/src/lib.rs +++ b/schemars_derive/src/lib.rs @@ -181,7 +181,7 @@ pub fn bitflags_schemars(_attr: proc_macro::TokenStream, item: proc_macro::Token quote! { stringify!(#ident) } }).collect(); let stream = quote! { - impl JsonStream for #name { + impl schemars::JsonStream for #name { fn schema_name() -> String { stringify!(#name).to_string() } From 431c4a6b6bc6ad2543cfa2cb6f2c78d26224d83b Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 19:54:18 -0500 Subject: [PATCH 10/26] _ --- schemars_derive/src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/schemars_derive/src/lib.rs b/schemars_derive/src/lib.rs index 1f1fd634..a399aecb 100644 --- a/schemars_derive/src/lib.rs +++ b/schemars_derive/src/lib.rs @@ -164,9 +164,15 @@ fn compile_error(errors: Vec) -> TokenStream { } } -#[proc_macro_attribute] -pub fn bitflags_schemars(_attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream { +#[proc_macro] +pub fn bitflags_schemars(item: proc_macro::TokenStream) -> proc_macro::TokenStream { let ast = syn::parse_macro_input!(item as syn::DeriveInput); + // let cont = Container::from_ast(&ast).unwrap(); + // let crate_alias = cont.attrs.crate_name.as_ref().map(|path| { + // quote_spanned! {path.span()=> + // use #path as schemars; + // } + // }); let name = &ast.ident; let variants = match &ast.data { syn::Data::Enum(v) => &v.variants, From 6d3e39112ddca01976290ebb9b520f57862547dc Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 19:56:27 -0500 Subject: [PATCH 11/26] _ --- schemars_derive/src/lib.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/schemars_derive/src/lib.rs b/schemars_derive/src/lib.rs index a399aecb..4ff54659 100644 --- a/schemars_derive/src/lib.rs +++ b/schemars_derive/src/lib.rs @@ -164,15 +164,15 @@ fn compile_error(errors: Vec) -> TokenStream { } } -#[proc_macro] -pub fn bitflags_schemars(item: proc_macro::TokenStream) -> proc_macro::TokenStream { +#[proc_macro_attribute] +pub fn bitflags_schemars(_attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream { let ast = syn::parse_macro_input!(item as syn::DeriveInput); - // let cont = Container::from_ast(&ast).unwrap(); - // let crate_alias = cont.attrs.crate_name.as_ref().map(|path| { - // quote_spanned! {path.span()=> - // use #path as schemars; - // } - // }); + let cont = Container::from_ast(&ast).unwrap(); + let crate_alias = cont.attrs.crate_name.as_ref().map(|path| { + quote_spanned! {path.span()=> + use #path as schemars; + } + }); let name = &ast.ident; let variants = match &ast.data { syn::Data::Enum(v) => &v.variants, @@ -187,6 +187,8 @@ pub fn bitflags_schemars(item: proc_macro::TokenStream) -> proc_macro::TokenStre quote! { stringify!(#ident) } }).collect(); let stream = quote! { + #crate_alias + impl schemars::JsonStream for #name { fn schema_name() -> String { stringify!(#name).to_string() From 06377eb083060fe3abd59b482f5c7381998f9ab0 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 20:05:20 -0500 Subject: [PATCH 12/26] _ --- schemars_derive/src/lib.rs | 92 ++++++++++++++--------------- schemars_derive/src/schema_exprs.rs | 8 +++ 2 files changed, 54 insertions(+), 46 deletions(-) diff --git a/schemars_derive/src/lib.rs b/schemars_derive/src/lib.rs index 4ff54659..51f614b2 100644 --- a/schemars_derive/src/lib.rs +++ b/schemars_derive/src/lib.rs @@ -164,49 +164,49 @@ fn compile_error(errors: Vec) -> TokenStream { } } -#[proc_macro_attribute] -pub fn bitflags_schemars(_attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream { - let ast = syn::parse_macro_input!(item as syn::DeriveInput); - let cont = Container::from_ast(&ast).unwrap(); - let crate_alias = cont.attrs.crate_name.as_ref().map(|path| { - quote_spanned! {path.span()=> - use #path as schemars; - } - }); - let name = &ast.ident; - let variants = match &ast.data { - syn::Data::Enum(v) => &v.variants, - _ => panic!("bitflags_schemars only works on enums"), - }; - let enum_values: Vec<_> = variants.iter().map(|v| { - let ident = &v.ident; - quote! { json!(BitFlags::from_flag(#name::#ident).bits()) } - }).collect(); - let enum_names: Vec<_> = variants.iter().map(|v| { - let ident = &v.ident; - quote! { stringify!(#ident) } - }).collect(); - let stream = quote! { - #crate_alias - - impl schemars::JsonStream for #name { - fn schema_name() -> String { - stringify!(#name).to_string() - } - - fn json_schema(_gen: &mut SchemaGenerator) -> Schema { - use schemars::schema::{InstanceType, SchemaObject}; - use serde_json::json; - let mut schema = SchemaObject::default(); - schema.instance_type = Some(InstanceType::Number.into()); - schema.enum_values = Some(vec![ #(#enum_values),* ]); - schema.extensions.insert( - "x-enumNames".to_string(), - json!([ #(#enum_names),* ]) - ); - schema.into() - } - } - }; - stream.into() -} +// #[proc_macro_attribute] +// pub fn bitflags_schemars(_attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream { +// let ast = syn::parse_macro_input!(item as syn::DeriveInput); +// let cont = Container::from_ast(&ast).unwrap(); +// let crate_alias = cont.attrs.crate_name.as_ref().map(|path| { +// quote_spanned! {path.span()=> +// use #path as schemars; +// } +// }); +// let name = &ast.ident; +// let variants = match &ast.data { +// syn::Data::Enum(v) => &v.variants, +// _ => panic!("bitflags_schemars only works on enums"), +// }; +// let enum_values: Vec<_> = variants.iter().map(|v| { +// let ident = &v.ident; +// quote! { json!(BitFlags::from_flag(#name::#ident).bits()) } +// }).collect(); +// let enum_names: Vec<_> = variants.iter().map(|v| { +// let ident = &v.ident; +// quote! { stringify!(#ident) } +// }).collect(); +// let stream = quote! { +// #crate_alias +// +// impl schemars::JsonStream for #name { +// fn schema_name() -> String { +// stringify!(#name).to_string() +// } +// +// fn json_schema(_gen: &mut SchemaGenerator) -> Schema { +// use schemars::schema::{InstanceType, SchemaObject}; +// use serde_json::json; +// let mut schema = SchemaObject::default(); +// schema.instance_type = Some(InstanceType::Number.into()); +// schema.enum_values = Some(vec![ #(#enum_values),* ]); +// schema.extensions.insert( +// "x-enumNames".to_string(), +// json!([ #(#enum_names),* ]) +// ); +// schema.into() +// } +// } +// }; +// stream.into() +// } diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 1f55084b..4e07bd41 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -48,10 +48,18 @@ pub fn expr_for_repr(cont: &Container) -> Result { let enum_ident = &cont.ident; let variant_idents = variants.iter().map(|v| &v.ident); + let variant_names = variants.iter().map(|v| { + let ident = &v.ident; + quote! { stringify!(#ident) } + }); let mut schema_expr = schema_object(quote! { instance_type: Some(schemars::schema::InstanceType::Integer.into()), enum_values: Some(vec![#((#enum_ident::#variant_idents as #repr_type).into()),*]), + extensions: [( + "x-enumNames".to_string(), + json!([ #(#variant_names),* ]) + )].collect() }); cont.attrs.as_metadata().apply_to_schema(&mut schema_expr); From 316809d9784b48d7063a9eca5aa84c6439469e39 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 20:08:41 -0500 Subject: [PATCH 13/26] _ --- schemars_derive/src/schema_exprs.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 4e07bd41..c8b1433b 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -48,10 +48,7 @@ pub fn expr_for_repr(cont: &Container) -> Result { let enum_ident = &cont.ident; let variant_idents = variants.iter().map(|v| &v.ident); - let variant_names = variants.iter().map(|v| { - let ident = &v.ident; - quote! { stringify!(#ident) } - }); + let variant_names = variants.iter().map(|v| &v.ident); let mut schema_expr = schema_object(quote! { instance_type: Some(schemars::schema::InstanceType::Integer.into()), From 8db10f95578b2b47be7c163ec0d673d585efa886 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 20:10:13 -0500 Subject: [PATCH 14/26] _ --- schemars_derive/src/schema_exprs.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index c8b1433b..5a67db55 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -48,7 +48,10 @@ pub fn expr_for_repr(cont: &Container) -> Result { let enum_ident = &cont.ident; let variant_idents = variants.iter().map(|v| &v.ident); - let variant_names = variants.iter().map(|v| &v.ident); + let variant_names = variants.iter().map(|v| { + let ident = &v.ident; + quote! { stringify!(#ident) } + }); let mut schema_expr = schema_object(quote! { instance_type: Some(schemars::schema::InstanceType::Integer.into()), @@ -56,7 +59,7 @@ pub fn expr_for_repr(cont: &Container) -> Result { extensions: [( "x-enumNames".to_string(), json!([ #(#variant_names),* ]) - )].collect() + )].into_iter().collect() }); cont.attrs.as_metadata().apply_to_schema(&mut schema_expr); From 282a6d1be717bea89392de21f4a99c7901c73cbb Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 20:11:50 -0500 Subject: [PATCH 15/26] _ --- schemars_derive/src/schema_exprs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 5a67db55..91e8ce13 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -59,7 +59,7 @@ pub fn expr_for_repr(cont: &Container) -> Result { extensions: [( "x-enumNames".to_string(), json!([ #(#variant_names),* ]) - )].into_iter().collect() + )].into_iter().collect(), }); cont.attrs.as_metadata().apply_to_schema(&mut schema_expr); From 0b88f79fa6636bd4d55d2223ac773c8a4c21b683 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 20:23:21 -0500 Subject: [PATCH 16/26] _ --- schemars_derive/src/schema_exprs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 91e8ce13..d01f384d 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -59,7 +59,7 @@ pub fn expr_for_repr(cont: &Container) -> Result { extensions: [( "x-enumNames".to_string(), json!([ #(#variant_names),* ]) - )].into_iter().collect(), + )].into(), }); cont.attrs.as_metadata().apply_to_schema(&mut schema_expr); From f4ea56fa94519a17b3ebccbb82080052e759e374 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 20:24:30 -0500 Subject: [PATCH 17/26] make it work --- schemars_derive/src/lib.rs | 47 ----------------------------- schemars_derive/src/schema_exprs.rs | 1 + 2 files changed, 1 insertion(+), 47 deletions(-) diff --git a/schemars_derive/src/lib.rs b/schemars_derive/src/lib.rs index 51f614b2..f8173f42 100644 --- a/schemars_derive/src/lib.rs +++ b/schemars_derive/src/lib.rs @@ -163,50 +163,3 @@ fn compile_error(errors: Vec) -> TokenStream { #(#compile_errors)* } } - -// #[proc_macro_attribute] -// pub fn bitflags_schemars(_attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream { -// let ast = syn::parse_macro_input!(item as syn::DeriveInput); -// let cont = Container::from_ast(&ast).unwrap(); -// let crate_alias = cont.attrs.crate_name.as_ref().map(|path| { -// quote_spanned! {path.span()=> -// use #path as schemars; -// } -// }); -// let name = &ast.ident; -// let variants = match &ast.data { -// syn::Data::Enum(v) => &v.variants, -// _ => panic!("bitflags_schemars only works on enums"), -// }; -// let enum_values: Vec<_> = variants.iter().map(|v| { -// let ident = &v.ident; -// quote! { json!(BitFlags::from_flag(#name::#ident).bits()) } -// }).collect(); -// let enum_names: Vec<_> = variants.iter().map(|v| { -// let ident = &v.ident; -// quote! { stringify!(#ident) } -// }).collect(); -// let stream = quote! { -// #crate_alias -// -// impl schemars::JsonStream for #name { -// fn schema_name() -> String { -// stringify!(#name).to_string() -// } -// -// fn json_schema(_gen: &mut SchemaGenerator) -> Schema { -// use schemars::schema::{InstanceType, SchemaObject}; -// use serde_json::json; -// let mut schema = SchemaObject::default(); -// schema.instance_type = Some(InstanceType::Number.into()); -// schema.enum_values = Some(vec![ #(#enum_values),* ]); -// schema.extensions.insert( -// "x-enumNames".to_string(), -// json!([ #(#enum_names),* ]) -// ); -// schema.into() -// } -// } -// }; -// stream.into() -// } diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index d01f384d..89f3a740 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -53,6 +53,7 @@ pub fn expr_for_repr(cont: &Container) -> Result { quote! { stringify!(#ident) } }); + // CR alee: make x-enumNames generation optional let mut schema_expr = schema_object(quote! { instance_type: Some(schemars::schema::InstanceType::Integer.into()), enum_values: Some(vec![#((#enum_ident::#variant_idents as #repr_type).into()),*]), From fd5b6c670aee2f40c867c0a7619d18cb8cc11939 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Sun, 28 May 2023 20:31:23 -0500 Subject: [PATCH 18/26] _ --- schemars/src/json_schema_impls/netidx.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/schemars/src/json_schema_impls/netidx.rs b/schemars/src/json_schema_impls/netidx.rs index 31fe2b8c..8b1057d2 100644 --- a/schemars/src/json_schema_impls/netidx.rs +++ b/schemars/src/json_schema_impls/netidx.rs @@ -1,7 +1,8 @@ use crate::gen::SchemaGenerator; use crate::schema::*; use crate::JsonSchema; -use netidx_core::{path::Path, chars::Chars}; +use netidx_core::{path::Path, chars::Chars, pool::{Poolable, Pooled}}; forward_impl!((JsonSchema for Path) => String); forward_impl!((JsonSchema for Chars) => String); +forward_impl!(( JsonSchema for Pooled where T: Poolable + Sync + Send + JsonSchema + 'static) => T); From 47ce5898f7db7e1abd05900e659e1729b2874350 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Mon, 12 Jun 2023 10:37:26 -0500 Subject: [PATCH 19/26] _ --- schemars/src/json_schema_impls/decimal.rs | 2 ++ schemars/tests/enum_repr.rs | 16 ++++++++++++ .../expected/enum-repr-with-x-enum-names.json | 19 ++++++++++++++ schemars_derive/src/ast/from_serde.rs | 1 + schemars_derive/src/ast/mod.rs | 19 +++++++++++++- schemars_derive/src/attr/mod.rs | 2 +- schemars_derive/src/attr/schemars_to_serde.rs | 5 +++- schemars_derive/src/schema_exprs.rs | 25 +++++++++++++------ 8 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 schemars/tests/expected/enum-repr-with-x-enum-names.json diff --git a/schemars/src/json_schema_impls/decimal.rs b/schemars/src/json_schema_impls/decimal.rs index 261f2e36..93ec9dc8 100644 --- a/schemars/src/json_schema_impls/decimal.rs +++ b/schemars/src/json_schema_impls/decimal.rs @@ -25,3 +25,5 @@ macro_rules! decimal_impl { #[cfg(feature = "rust_decimal")] decimal_impl!(rust_decimal::Decimal => String, "Decimal"); +#[cfg(feature = "bigdecimal")] +decimal_impl!(bigdecimal::BigDecimal); diff --git a/schemars/tests/enum_repr.rs b/schemars/tests/enum_repr.rs index d3183477..68ffc5f3 100644 --- a/schemars/tests/enum_repr.rs +++ b/schemars/tests/enum_repr.rs @@ -17,6 +17,22 @@ fn enum_repr() -> TestResult { test_default_generated_schema::("enum-repr") } +#[derive(JsonSchema_repr)] +#[repr(u8)] +#[schemars(extension = "x-enumNames")] +pub enum EnumWithXEnumNames { + Zero, + One, + Five = 5, + Six, + Three = 3, +} + +#[test] +fn enum_repr_with_x_enum_names() -> TestResult { + test_default_generated_schema::("enum-repr-with-x-enum-names") +} + #[derive(JsonSchema_repr)] #[repr(i64)] #[serde(rename = "Renamed")] diff --git a/schemars/tests/expected/enum-repr-with-x-enum-names.json b/schemars/tests/expected/enum-repr-with-x-enum-names.json new file mode 100644 index 00000000..41fdcef1 --- /dev/null +++ b/schemars/tests/expected/enum-repr-with-x-enum-names.json @@ -0,0 +1,19 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "EnumWithXEnumNames", + "type": "integer", + "enum": [ + 0, + 1, + 5, + 6, + 3 + ], + "x-enumNames": [ + "Zero", + "One", + "Five", + "Six", + "Three" + ] +} \ No newline at end of file diff --git a/schemars_derive/src/ast/from_serde.rs b/schemars_derive/src/ast/from_serde.rs index 453c247f..245464f5 100644 --- a/schemars_derive/src/ast/from_serde.rs +++ b/schemars_derive/src/ast/from_serde.rs @@ -29,6 +29,7 @@ impl<'a> FromSerde for Container<'a> { original: serde.original, // FIXME this allows with/schema_with attribute on containers attrs: Attrs::new(&serde.original.attrs, errors), + extensions: Vec::new(), }) } } diff --git a/schemars_derive/src/ast/mod.rs b/schemars_derive/src/ast/mod.rs index 7b9052e4..d512a7da 100644 --- a/schemars_derive/src/ast/mod.rs +++ b/schemars_derive/src/ast/mod.rs @@ -12,6 +12,7 @@ pub struct Container<'a> { pub generics: syn::Generics, pub original: &'a syn::DeriveInput, pub attrs: Attrs, + pub extensions: Vec, } pub enum Data<'a> { @@ -39,10 +40,26 @@ pub struct Field<'a> { impl<'a> Container<'a> { pub fn from_ast(item: &'a syn::DeriveInput) -> Result, Vec> { + let mut extensions: Vec = Vec::new(); + item.attrs.iter().for_each(|a| { + if a.path.is_ident("schemars") { + if let Ok(syn::Meta::List(lst)) = a.parse_meta() { + for it in lst.nested { + if let syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { path, lit: syn::Lit::Str(s), .. })) = it { + if path.is_ident("extension") { + extensions.push(s.value()); + } + } + } + } + } + }); + let ctxt = Ctxt::new(); let result = serde_ast::Container::from_ast(&ctxt, item, Derive::Deserialize) .ok_or(()) - .and_then(|serde| Self::from_serde(&ctxt, serde)); + .and_then(|serde| Self::from_serde(&ctxt, serde)) + .map(|c| Container { extensions, ..c }); ctxt.check() .map(|_| result.expect("from_ast set no errors on Ctxt, so should have returned Ok")) diff --git a/schemars_derive/src/attr/mod.rs b/schemars_derive/src/attr/mod.rs index f790694a..97a3eb94 100644 --- a/schemars_derive/src/attr/mod.rs +++ b/schemars_derive/src/attr/mod.rs @@ -27,7 +27,7 @@ pub struct Attrs { pub examples: Vec, pub repr: Option, pub crate_name: Option, - pub is_renamed: bool, + pub is_renamed: bool } #[derive(Debug)] diff --git a/schemars_derive/src/attr/schemars_to_serde.rs b/schemars_derive/src/attr/schemars_to_serde.rs index 7878a2c1..403f194b 100644 --- a/schemars_derive/src/attr/schemars_to_serde.rs +++ b/schemars_derive/src/attr/schemars_to_serde.rs @@ -28,6 +28,8 @@ pub(crate) static SERDE_KEYWORDS: &[&str] = &[ // JsonSchema on remote types, but we parse that ourselves rather than using serde_derive_internals. "serialize_with", "with", + // Special case - `extension` is removed from serde attrs, so is only respected when present in schemars attr. + "extension" ]; // If a struct/variant/field has any #[schemars] attributes, then create copies of them @@ -75,7 +77,7 @@ fn process_attrs(ctxt: &Ctxt, attrs: &mut Vec) { .flatten() .filter_map(|meta| { let keyword = get_meta_ident(ctxt, &meta).ok()?; - if keyword.ends_with("with") || !SERDE_KEYWORDS.contains(&keyword.as_ref()) { + if keyword.ends_with("with") || keyword.ends_with("extension") || !SERDE_KEYWORDS.contains(&keyword.as_ref()) { None } else { Some((meta, keyword)) @@ -98,6 +100,7 @@ fn process_attrs(ctxt: &Ctxt, attrs: &mut Vec) { if !schemars_meta_names.contains(&i) && SERDE_KEYWORDS.contains(&i.as_ref()) && i != "bound" + && i != "extensions" { serde_meta.push(meta); } diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 89f3a740..bccefb1f 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -48,19 +48,28 @@ pub fn expr_for_repr(cont: &Container) -> Result { let enum_ident = &cont.ident; let variant_idents = variants.iter().map(|v| &v.ident); - let variant_names = variants.iter().map(|v| { - let ident = &v.ident; - quote! { stringify!(#ident) } + + let extensions = cont.extensions.iter().filter_map(|e| { + if e == "x-enumNames" { + let variant_names = variants.iter().map(|v| { + let ident = &v.ident; + quote! { stringify!(#ident) } + }); + Some(quote! { + ( + "x-enumNames".to_string(), + serde_json::json!([ #(#variant_names),* ]) + ) + }) + } else { + None + } }); - // CR alee: make x-enumNames generation optional let mut schema_expr = schema_object(quote! { instance_type: Some(schemars::schema::InstanceType::Integer.into()), enum_values: Some(vec![#((#enum_ident::#variant_idents as #repr_type).into()),*]), - extensions: [( - "x-enumNames".to_string(), - json!([ #(#variant_names),* ]) - )].into(), + extensions: [ #(#extensions),* ].into(), }); cont.attrs.as_metadata().apply_to_schema(&mut schema_expr); From 823941978b229d60c50f3824155577757c1e4981 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Mon, 12 Jun 2023 10:39:23 -0500 Subject: [PATCH 20/26] _ --- schemars/Cargo.toml | 1 - schemars/src/json_schema_impls/decimal.rs | 4 +++- schemars/src/json_schema_impls/netidx.rs | 8 -------- 3 files changed, 3 insertions(+), 10 deletions(-) delete mode 100644 schemars/src/json_schema_impls/netidx.rs diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index f6152f38..c9bb5279 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -32,7 +32,6 @@ rust_decimal = { version = "1", default-features = false, optional = true } bigdecimal = { version = "0.3", default-features = false, optional = true } enumset = { version = "1.0", optional = true } smol_str = { version = "0.1.17", optional = true } -netidx-core = { version = "0.18.4", optional = true } enumflags2 = { version = "0.7.7", optional = true } [dev-dependencies] diff --git a/schemars/src/json_schema_impls/decimal.rs b/schemars/src/json_schema_impls/decimal.rs index 93ec9dc8..d7fc8a1f 100644 --- a/schemars/src/json_schema_impls/decimal.rs +++ b/schemars/src/json_schema_impls/decimal.rs @@ -8,6 +8,8 @@ macro_rules! decimal_impl { }; ($type:ty => $instance_type:ident, $name:expr) => { impl JsonSchema for $type { + no_ref_schema!(); + fn schema_name() -> String { $name.to_owned() } @@ -24,6 +26,6 @@ macro_rules! decimal_impl { } #[cfg(feature = "rust_decimal")] -decimal_impl!(rust_decimal::Decimal => String, "Decimal"); +decimal_impl!(rust_decimal::Decimal); #[cfg(feature = "bigdecimal")] decimal_impl!(bigdecimal::BigDecimal); diff --git a/schemars/src/json_schema_impls/netidx.rs b/schemars/src/json_schema_impls/netidx.rs deleted file mode 100644 index 8b1057d2..00000000 --- a/schemars/src/json_schema_impls/netidx.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::gen::SchemaGenerator; -use crate::schema::*; -use crate::JsonSchema; -use netidx_core::{path::Path, chars::Chars, pool::{Poolable, Pooled}}; - -forward_impl!((JsonSchema for Path) => String); -forward_impl!((JsonSchema for Chars) => String); -forward_impl!(( JsonSchema for Pooled where T: Poolable + Sync + Send + JsonSchema + 'static) => T); From 3ce9b7184bd7525f31aa872d7f7d057bd594c1b7 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Mon, 12 Jun 2023 10:40:15 -0500 Subject: [PATCH 21/26] _ --- schemars/src/json_schema_impls/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index bbb48305..69f98888 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -51,6 +51,8 @@ mod core; mod decimal; #[cfg(feature = "either")] mod either; +#[cfg(feature = "enumflags2")] +mod enumflags2; #[cfg(feature = "enumset")] mod enumset; mod ffi; @@ -75,7 +77,3 @@ mod uuid08; #[cfg(feature = "uuid1")] mod uuid1; mod wrapper; -#[cfg(feature = "netidx-core")] -mod netidx; -#[cfg(feature = "enumflags2")] -mod enumflags2; From e5fa050996f3c5d504bdda9c4367b6de390cf295 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Mon, 12 Jun 2023 10:54:08 -0500 Subject: [PATCH 22/26] _ --- schemars/Cargo.toml | 4 ++++ schemars/tests/enumflags2.rs | 22 ++++++++++++++++++++++ schemars/tests/expected/enum-state.json | 17 +++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 schemars/tests/enumflags2.rs create mode 100644 schemars/tests/expected/enum-state.json diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index c9bb5279..bd60db10 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -108,5 +108,9 @@ required-features = ["enumset"] name = "smol_str" required-features = ["smol_str"] +[[test]] +name = "enumflags2" +required-features = ["enumflags2"] + [package.metadata.docs.rs] all-features = true diff --git a/schemars/tests/enumflags2.rs b/schemars/tests/enumflags2.rs new file mode 100644 index 00000000..b71d25af --- /dev/null +++ b/schemars/tests/enumflags2.rs @@ -0,0 +1,22 @@ +mod util; + +use schemars::JsonSchema_repr; +use enumflags2::bitflags; +use util::*; + +#[derive(Copy, Clone, JsonSchema_repr)] +#[repr(u8)] +#[schemars(extension = "x-enumNames")] +#[bitflags] +pub enum EnumState { + A, + B, + C, + D +} + +#[test] +fn enum_repr() -> TestResult { + test_default_generated_schema::("enum-state") +} + diff --git a/schemars/tests/expected/enum-state.json b/schemars/tests/expected/enum-state.json new file mode 100644 index 00000000..4fddae95 --- /dev/null +++ b/schemars/tests/expected/enum-state.json @@ -0,0 +1,17 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "EnumState", + "type": "integer", + "enum": [ + 1, + 2, + 4, + 8 + ], + "x-enumNames": [ + "A", + "B", + "C", + "D" + ] +} \ No newline at end of file From d5c44adb77ae6f9cb8d1bec41ec58d66fdbf53d9 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Mon, 12 Jun 2023 10:58:16 -0500 Subject: [PATCH 23/26] _ --- schemars/tests/enumflags2.rs | 8 +++++-- .../tests/expected/enum-bitflags-state.json | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 schemars/tests/expected/enum-bitflags-state.json diff --git a/schemars/tests/enumflags2.rs b/schemars/tests/enumflags2.rs index b71d25af..6695f51c 100644 --- a/schemars/tests/enumflags2.rs +++ b/schemars/tests/enumflags2.rs @@ -1,7 +1,7 @@ mod util; use schemars::JsonSchema_repr; -use enumflags2::bitflags; +use enumflags2::{bitflags, BitFlags}; use util::*; #[derive(Copy, Clone, JsonSchema_repr)] @@ -16,7 +16,11 @@ pub enum EnumState { } #[test] -fn enum_repr() -> TestResult { +fn enum_state() -> TestResult { test_default_generated_schema::("enum-state") } +#[test] +fn enum_bitflags_state() -> TestResult { + test_default_generated_schema::>("enum-bitflags-state") +} diff --git a/schemars/tests/expected/enum-bitflags-state.json b/schemars/tests/expected/enum-bitflags-state.json new file mode 100644 index 00000000..29b0dac9 --- /dev/null +++ b/schemars/tests/expected/enum-bitflags-state.json @@ -0,0 +1,24 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "BitFlags_EnumState", + "type": "integer", + "format": "uint64", + "minimum": 0.0, + "definitions": { + "EnumState": { + "type": "integer", + "enum": [ + 1, + 2, + 4, + 8 + ], + "x-enumNames": [ + "A", + "B", + "C", + "D" + ] + } + } +} From 355e331a6b463ad55f7228bf5ac1479a70e3cc3d Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Mon, 12 Jun 2023 13:24:54 -0500 Subject: [PATCH 24/26] _ --- schemars_derive/src/schema_exprs.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index bccefb1f..33cc0059 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -49,7 +49,7 @@ pub fn expr_for_repr(cont: &Container) -> Result { let enum_ident = &cont.ident; let variant_idents = variants.iter().map(|v| &v.ident); - let extensions = cont.extensions.iter().filter_map(|e| { + let extensions: Vec = cont.extensions.iter().filter_map(|e| { if e == "x-enumNames" { let variant_names = variants.iter().map(|v| { let ident = &v.ident; @@ -64,13 +64,20 @@ pub fn expr_for_repr(cont: &Container) -> Result { } else { None } - }); + }).collect(); - let mut schema_expr = schema_object(quote! { - instance_type: Some(schemars::schema::InstanceType::Integer.into()), - enum_values: Some(vec![#((#enum_ident::#variant_idents as #repr_type).into()),*]), - extensions: [ #(#extensions),* ].into(), - }); + let mut schema_expr = if extensions.is_empty() { + schema_object(quote! { + instance_type: Some(schemars::schema::InstanceType::Integer.into()), + enum_values: Some(vec![#((#enum_ident::#variant_idents as #repr_type).into()),*]), + }) + } else { + schema_object(quote! { + instance_type: Some(schemars::schema::InstanceType::Integer.into()), + enum_values: Some(vec![#((#enum_ident::#variant_idents as #repr_type).into()),*]), + extensions: [ #(#extensions),* ].into(), + }) + }; cont.attrs.as_metadata().apply_to_schema(&mut schema_expr); Ok(schema_expr) From 50acf939be382916ffae8e565025919f7bf711c2 Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Mon, 12 Jun 2023 13:34:56 -0500 Subject: [PATCH 25/26] _ --- schemars_derive/src/schema_exprs.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 33cc0059..42a1851f 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -66,18 +66,12 @@ pub fn expr_for_repr(cont: &Container) -> Result { } }).collect(); - let mut schema_expr = if extensions.is_empty() { - schema_object(quote! { - instance_type: Some(schemars::schema::InstanceType::Integer.into()), - enum_values: Some(vec![#((#enum_ident::#variant_idents as #repr_type).into()),*]), - }) - } else { + let mut schema_expr = schema_object(quote! { instance_type: Some(schemars::schema::InstanceType::Integer.into()), enum_values: Some(vec![#((#enum_ident::#variant_idents as #repr_type).into()),*]), extensions: [ #(#extensions),* ].into(), - }) - }; + }); cont.attrs.as_metadata().apply_to_schema(&mut schema_expr); Ok(schema_expr) From 89dedb37ade2c31ba7c6b8eb917b9b86caeee05c Mon Sep 17 00:00:00 2001 From: Andrew Lee <126016409+andrew-lee-architect@users.noreply.github.com> Date: Mon, 12 Jun 2023 13:51:52 -0500 Subject: [PATCH 26/26] _ --- schemars_derive/src/schema_exprs.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 42a1851f..a25c6aa9 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -66,12 +66,18 @@ pub fn expr_for_repr(cont: &Container) -> Result { } }).collect(); - let mut schema_expr = + let mut schema_expr = if extensions.is_empty() { schema_object(quote! { instance_type: Some(schemars::schema::InstanceType::Integer.into()), enum_values: Some(vec![#((#enum_ident::#variant_idents as #repr_type).into()),*]), - extensions: [ #(#extensions),* ].into(), - }); + }) + } else { + schema_object(quote! { + instance_type: Some(schemars::schema::InstanceType::Integer.into()), + enum_values: Some(vec![#((#enum_ident::#variant_idents as #repr_type).into()),*]), + extensions: [ #(#extensions),* ].iter().map(|i| i.clone()).collect(), + }) + }; cont.attrs.as_metadata().apply_to_schema(&mut schema_expr); Ok(schema_expr)