Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix macro_rules generated components #124

Merged
merged 1 commit into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions tests/utoipa_gen_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -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"]),
Expand All @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -126,4 +138,8 @@ impl Modify for Foo {
}
}

#[derive(Debug, Serialize)]
struct Foo;

#[derive(Debug, Serialize)]
struct FooResources;
2 changes: 1 addition & 1 deletion utoipa-gen/src/ext/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
23 changes: 15 additions & 8 deletions utoipa-gen/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down