More fixes for deriving JsonSchema inside macro

This commit is contained in:
Graham Esau 2021-04-05 16:40:20 +01:00
parent 4d3400152e
commit ebd7ff32ce
3 changed files with 77 additions and 5 deletions

View file

@ -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"
}
}
}

View file

@ -20,3 +20,48 @@ build_struct!(A { v: i32 });
fn macro_built_struct() -> TestResult {
test_default_generated_schema::<A>("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::<OuterEnum>("macro_built_enum")
}

View file

@ -59,19 +59,20 @@ pub fn expr_for_repr(cont: &Container) -> Result<TokenStream, syn::Error> {
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<TokenStream> {
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();