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

Detecting ToSchema fails when behind a cfg_attr flag #13

Open
tedbennett opened this issue Jan 27, 2024 · 5 comments
Open

Detecting ToSchema fails when behind a cfg_attr flag #13

tedbennett opened this issue Jan 27, 2024 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@tedbennett
Copy link

Hi! 👋

I'm playing around with this crate and ran into an issue where schemas (and I'm guessing paths) aren't detected when behind a cfg_attr flag. E.g:
Works:

#[derive(ToSchema)]
struct Todo {
  // ...
}

Doesn't work:

#[cfg_attr(feature = "openapi", derive(ToSchema))]
struct Todo {
  // ...
}

Essentially, my use case is to only generate the OpenAPI spec when writing it to a file in my CI step, and I don't want to have to compile all my utoipa macros when running the webserver normally.

Thanks for the library, it's really nice!

@tedbennett
Copy link
Author

Oh, thinking about this more, since the detection is done via the macro I'm guessing it might run before the cfg flags are applied? And you probably wouldn't want to include Schemas behind said feature flag if they're switched off...

@ProbablyClem
Copy link
Owner

From this https://stackoverflow.com/a/42551386, it seems that #[cfg_attr(feature = "openapi", derive(ToSchema))] is the same as derive(ToSchema) if the openapi flag is enabled.
So it seems that procedural macros are executed before feature flags, and I can't really find a way to see which feature flags are enabled from within the macro.

There is probably a solution to this but I don't know how to fix it yet...

@ProbablyClem ProbablyClem added the help wanted Extra attention is needed label Mar 2, 2024
@DenuxPlays
Copy link
Collaborator

Also:
ToSchema detection fails also when the struct is contained in a macro. (For example bitflags macro)

@RemiBardon
Copy link

I was about to submit an issue for this… guess I'm stuck like you 😕 Has anyone found a solution/workaround?

@RemiBardon
Copy link

It doesn't look good… I found rust-lang/rust-analyzer#13360 which mentions no solution…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants