From ebd7ff32cee5199234e29b7b185cb5953d9c00f3 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Mon, 5 Apr 2021 16:40:20 +0100 Subject: [PATCH] More fixes for deriving JsonSchema inside macro --- schemars/tests/expected/macro_built_enum.json | 23 ++++++++++ schemars/tests/macro.rs | 45 +++++++++++++++++++ schemars_derive/src/schema_exprs.rs | 14 +++--- 3 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 schemars/tests/expected/macro_built_enum.json diff --git a/schemars/tests/expected/macro_built_enum.json b/schemars/tests/expected/macro_built_enum.json new file mode 100644 index 0000000..8564ef7 --- /dev/null +++ b/schemars/tests/expected/macro_built_enum.json @@ -0,0 +1,23 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "OuterEnum", + "anyOf": [ + { + "type": "object", + "required": [ + "InnerStruct" + ], + "properties": { + "InnerStruct": { + "$ref": "#/definitions/InnerStruct" + } + }, + "additionalProperties": false + } + ], + "definitions": { + "InnerStruct": { + "type": "object" + } + } +} \ No newline at end of file diff --git a/schemars/tests/macro.rs b/schemars/tests/macro.rs index 0ed39d7..9991494 100644 --- a/schemars/tests/macro.rs +++ b/schemars/tests/macro.rs @@ -20,3 +20,48 @@ build_struct!(A { v: i32 }); fn macro_built_struct() -> TestResult { test_default_generated_schema::("macro_built_struct") } + +macro_rules! build_enum { + ( + $(#[$outer_derive:meta])* + $outer:ident { + $($(#[$inner_derive:meta])* + $inner:ident { + $( $(#[$field_attribute:meta])* + $field:ident : $ty:ty),* + })* + } + ) => { + + $( + $(#[$inner_derive])* + pub struct $inner { + $( + $(#[$field_attribute])* + pub $field: $ty + ),* + } + )* + + $(#[$outer_derive])* + pub enum $outer { + $( + $inner($inner) + ),* + } + } +} + +build_enum!( + #[derive(Debug, JsonSchema)] + OuterEnum { + #[derive(Debug, JsonSchema)] + InnerStruct {} + } + +); + +#[test] +fn macro_built_enum() -> TestResult { + test_default_generated_schema::("macro_built_enum") +} diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 3b25d05..a9e4deb 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -59,19 +59,20 @@ pub fn expr_for_repr(cont: &Container) -> Result { fn expr_for_field(field: &Field, allow_ref: bool) -> TokenStream { let (ty, type_def) = type_for_schema(field, 0); let span = field.original.span(); + let gen = quote!(gen); if allow_ref { quote_spanned! {span=> { #type_def - gen.subschema_for::<#ty>() + #gen.subschema_for::<#ty>() } } } else { quote_spanned! {span=> { #type_def - <#ty as schemars::JsonSchema>::json_schema(gen) + <#ty as schemars::JsonSchema>::json_schema(#gen) } } } @@ -316,8 +317,9 @@ fn expr_for_adjacent_tagged_enum<'a>( fn expr_for_untagged_enum_variant(variant: &Variant, deny_unknown_fields: bool) -> TokenStream { if let Some(WithAttr::Type(with)) = &variant.attrs.with { + let gen = quote!(gen); return quote_spanned! {variant.original.span()=> - gen.subschema_for::<#with>() + #gen.subschema_for::<#with>() }; } @@ -334,8 +336,9 @@ fn expr_for_untagged_enum_variant_for_flatten( deny_unknown_fields: bool, ) -> Option { if let Some(WithAttr::Type(with)) = &variant.attrs.with { + let gen = quote!(gen); return Some(quote_spanned! {variant.original.span()=> - <#with as schemars::JsonSchema>::json_schema(gen) + <#with as schemars::JsonSchema>::json_schema(#gen) }); } @@ -429,8 +432,9 @@ fn expr_for_struct( type_defs.push(type_def); } + let gen = quote!(gen); quote_spanned! {ty.span()=> - .flatten(<#ty as schemars::JsonSchema>::json_schema_for_flatten(gen)) + .flatten(<#ty as schemars::JsonSchema>::json_schema_for_flatten(#gen)) } }) .collect();