From 9435f4ca92cd909f4acf902b433d102d737600be Mon Sep 17 00:00:00 2001 From: Juha Kukkonen Date: Tue, 3 May 2022 23:55:46 +0300 Subject: [PATCH] Fix macro_rules generated components macro_rules generates bit different TokenStream which need to be taken account in Component derive implementation. This allows using macro_rules to generate components. --- tests/utoipa_gen_test.rs | 24 ++++++++++++++++++++---- utoipa-gen/src/ext/actix.rs | 2 +- utoipa-gen/src/schema.rs | 23 +++++++++++++++-------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/tests/utoipa_gen_test.rs b/tests/utoipa_gen_test.rs index 5ccb8ad2..47dc3503 100644 --- a/tests/utoipa_gen_test.rs +++ b/tests/utoipa_gen_test.rs @@ -49,7 +49,7 @@ mod pet_api { /// Get pet by id /// - /// Get pet from database by pet database id + /// Get pet from database by pet database id #[utoipa::path( get, path = "/pets/{id}", @@ -78,9 +78,9 @@ mod pet_api { #[derive(Default, OpenApi)] #[openapi( - handlers(pet_api::get_pet_by_id), - components(Pet), - modifiers(&Foo), + handlers(pet_api::get_pet_by_id), + components(Pet), + modifiers(&Foo), security( (), ("my_auth" = ["read:items", "edit:items"]), @@ -89,6 +89,16 @@ mod pet_api { )] struct ApiDoc; +macro_rules! build_foo { + ($typ: ident, $d: ty, $r: ty) => { + #[derive(Debug, Serialize, Component)] + struct $typ { + data: $d, + resources: $r, + } + }; +} + #[test] #[ignore = "this is just a test bed to run macros"] fn derive_openapi() { @@ -97,6 +107,8 @@ fn derive_openapi() { utoipa::openapi::Paths::new(), ); println!("{}", ApiDoc::openapi().to_pretty_json().unwrap()); + + build_foo!(GetFooBody, Foo, FooResources); } impl Modify for Foo { @@ -126,4 +138,8 @@ impl Modify for Foo { } } +#[derive(Debug, Serialize)] struct Foo; + +#[derive(Debug, Serialize)] +struct FooResources; diff --git a/utoipa-gen/src/ext/actix.rs b/utoipa-gen/src/ext/actix.rs index c03ba2ac..a56eac0a 100644 --- a/utoipa-gen/src/ext/actix.rs +++ b/utoipa-gen/src/ext/actix.rs @@ -108,7 +108,7 @@ impl PathOperations { fn get_type_path(ty: &Type) -> &TypePath { match ty { Type::Path(path) => path, - _ => abort_call_site!("unexpected type, expected Type::Path"), // should not get here by any means with current types + _ => abort_call_site!("unexpected type in actix path operations, expected Type::Path"), // should not get here by any means with current types } } diff --git a/utoipa-gen/src/schema.rs b/utoipa-gen/src/schema.rs index 6718a197..77a1ec53 100644 --- a/utoipa-gen/src/schema.rs +++ b/utoipa-gen/src/schema.rs @@ -39,19 +39,26 @@ struct ComponentPart<'a> { impl<'a> ComponentPart<'a> { pub fn from_type(ty: &'a Type) -> ComponentPart<'a> { ComponentPart::from_type_path( - match ty { - Type::Path(path) => path, - Type::Reference(reference) => match reference.elem.as_ref() { - Type::Path(path) => path, - _ => abort_call_site!("unexpected type in reference, expected Type:Path"), - }, - _ => abort_call_site!("unexpected type, expected Type::Path"), - }, + Self::get_type_path(ty), ComponentPart::convert, ComponentPart::resolve_component_type, ) } + fn get_type_path(ty: &'a Type) -> &'a TypePath { + match ty { + Type::Path(path) => path, + Type::Reference(reference) => match reference.elem.as_ref() { + Type::Path(path) => path, + _ => abort_call_site!("unexpected type in reference, expected Type:Path"), + }, + Type::Group(group) => Self::get_type_path(group.elem.as_ref()), + _ => abort_call_site!( + "unexpected type in component part get type path, expected one of: Path, Reference, Group" + ), + } + } + fn from_ident(ty: &'a Ident) -> ComponentPart<'a> { ComponentPart { child: None,