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

Support for generics? #15

Closed k1ngcyk closed 6 months ago

k1ngcyk commented 6 months ago

In utoipa we can do like:

#[derive(ToSchema)]
#[aliases(
    FooA = Foo<A>,
    FooB = Foo<B>
)]
pub struct Foo<T> {
    bar: T,
}

and generates two schema FooA and FooB,

and utoipauto has compile error with:

error[E0107]: missing generics for struct `Foo`
   --> src/mod.rs:123:5
    |
123 |     #[utoipauto]
    |     ^^^^^^^^^^^^ expected 1 generic argument
    |
note: struct defined here, with 1 generic parameter: `T`
   --> src/foo.rs:12:12
    |
12  | pub struct Foo<T> {
    |            ^^^^^^^^^^^ -
    = note: this error originates in the attribute macro `utoipauto` (in Nightly builds, run with -Z macro-backtrace for more info)
help: add missing generic argument
    |
123 |     #[utoipauto]<T>
    |                 +++
EinarasGar commented 6 months ago

Would like to see generics supported too. Otherwise, amazing crate!

DenuxPlays commented 6 months ago

I need this feature! I tried to add #[utoipa_ignore] and add the schema manually but that didn't worked.

DenuxPlays commented 6 months ago

I am goind to try to solve this. But give me a few days as this is my first time dealing with macros (let alone generic macros)

ProbablyClem commented 6 months ago

That would be a nice feature. However, #[utoipa_ignore] works for me, even with generics. Have you tried restarting rust analyzer? Can you give me a minimal reproducible example ? @DenuxPlays

DenuxPlays commented 6 months ago

That would be a nice feature.

However, #[utoipa_ignore] works for me, even with generics. Have you tried restarting rust analyzer? Can you give me a minimal reproducible example ? @DenuxPlays

No it works just fine. It was actually my code that made it look like it was the macros.

Also I have a plan which (if it works) would made the implementation of this feature very easy but I have to work on it tomorrow.

ProbablyClem commented 6 months ago

For the aliases features, you don't actually have to worry about generics. You can have a look at the parse_from_attr function, that's where the fix will probably be.

You can see that it's where we look at struct's macros. You'll need to search for the aliases macro, and return DiscoverType::Model() or DiscoverType::Response() with the names defined within the aliases macros, instead of the actual generic struct name.

You'll need to learn a bit about the syn crate, but otherwise it shouldn't be that hard, and you'll learn a lot !

Feel free to ask if you have any struggles or questions, I'll do my best to help. Have fun !

DenuxPlays commented 6 months ago

For the aliases features, you don't actually have to worry about generics.

You can have a look at the parse_from_attr function, that's where the fix will probably be.

You can see that it's where we look at struct's macros.

You'll need to search for the aliases macro, and return DiscoverType::Model() or DiscoverType::Response() with the names defined within the aliases macros, instead of the actual generic struct name.

You'll need to learn a bit about the syn crate, but otherwise it shouldn't be that hard, and you'll learn a lot !

Feel free to ask if you have any struggles or questions, I'll do my best to help.

Have fun !

That's what I thought but thank you for the information. I think I can do this and I'll open a pr soon

DenuxPlays commented 6 months ago

They arent any generic responses right?

ProbablyClem commented 6 months ago

I think we could have some, just like models

DenuxPlays commented 6 months ago

I think we could have some, just like models

But here is no aliases macro for Response

DenuxPlays commented 6 months ago

I think we could have some, just like models

I acutally have a question. This my current progress..

if meta.path().is_ident("aliases") {
            println!("{:?}\n", meta);
        }

I cannot figure out on how to acces the inside of the aliases macro. Can you help me with that?

ProbablyClem commented 6 months ago

I can't really help you without implementing it mysef... Take a look at the syn crate

DenuxPlays commented 6 months ago

I can't really help you without implementing it mysef... Take a look at the syn crate

Ah okay Gonna do this something has to work lol

ProbablyClem commented 6 months ago

Hey, support for generics got added in the new 0.1.10 release. Thanks @DenuxPlays

DenuxPlays commented 6 months ago

Hey, support for generics got added in the new 0.1.10 release.

Thanks @DenuxPlays

If there are any bugs reported you can assign me. I can maintain this feature if needed