ProbablyClem / utoipauto

Rust Macros to automate the addition of Paths/Schemas to Utoipa crate, simulating Reflection during the compilation phase
Apache License 2.0
108 stars 10 forks source link

Recursive expansion seems to be broken? #14

Closed j7nw4r closed 6 months ago

j7nw4r commented 7 months ago

Hello. Given this simple example: https://github.com/j7nw4r/utoipauto-bug the macro expansion seems to be broken. It fails with the error message:

error: unexpected attribute, expected any of: handlers, components, modifiers, security, tags, external_docs, servers, expected identifier at the #[utoipauto(paths = "./src")] annotation.

I believe this is because the expansion mistakenly expands to the below and includes an erroneous # [ openapi ].

#[derive(OpenApi)] 
#[openapi(paths   (), components   ( schemas   (), responses   () ), # [ openapi   ])]
struct ApiDoc;

I'm not super familiar with proc-macros, but if pointed in the correct direction, maybe I can look into it.

ProbablyClem commented 6 months ago

Hey, thanks for your issue. You're right, I'll look into it.

As a workaround, you can write #[openapi()] (with the parentheses).

j7nw4r commented 6 months ago

Hmm. That doesn't seem to work for me either. I'll continue manually adding for now.

Thanks for your help.

ProbablyClem commented 6 months ago

The thing I'm 100% sur works is to add a description and info field since it's what is in the tests and what I use in other projects. #[openapi(info(title = "Percentage API", version = "1.0.0"))]

I'll try to fix it when I can

ProbablyClem commented 6 months ago

However, can you clarify what do you mean when you say that '#[openapi()]' doesn't work ? For me it works...

j7nw4r commented 6 months ago

In my bug example, when I do:

#[utoipauto(paths = "./src")]
#[derive(OpenApi)]
#[openapi()]
struct ApiDoc;

I get a:

`derive` can only be applied to structs, enums, and unions [E0774]

from the compiler. I do not get the same error when I remove the #[utoipauto(paths = "./src")] macro. It seems that the first level macro expansion for the utoipauto macro is:

#[derive(OpenApi)]
#[openapi()]
struct ApiDoc;

So I'm not sure what's the actual issue.

I'm using rustrover, if that helps.

j7nw4r commented 6 months ago

Hmm as I look into it more. Your suggestion (#[openapi()]) works. I think the issue with suggestion actually lies with RustRover interpretation of the code. I will use your suggestion. Thank you.

The first issue still stands though.

j7nw4r commented 6 months ago

@ProbablyClem If you can point me in a direction, I can fix it.

ProbablyClem commented 6 months ago

There's the update_openapi_macro_attributes function in utoipauto-core that'sformating the output

j7nw4r commented 6 months ago

@ProbablyClem Can you look at this PR please? https://github.com/ProbablyClem/utoipauto/pull/17

j7nw4r commented 6 months ago

@ProbablyClem I realized I snuck a change in that accidentally changed functionality. Fix is here: https://github.com/ProbablyClem/utoipauto/pull/18