oxidecomputer / progenitor

An OpenAPI client generator
502 stars 62 forks source link

Add settings option to easily rename produced `Client` struct #837

Open mfpekala opened 3 months ago

mfpekala commented 3 months ago

There does not seem to be an easy way to change the name of the produced struct. I'm developing an application which may interface with multiple openapi servers, and it would be convenient to give Client a more descriptive name.

ahl commented 3 months ago

Usually what I've seen in this situation is something like this:

mod foo {
    generate_api!(
        spec = "foo.json",
        interface = Builder,
    }l
}

mod bar {
    generate_api!(
        spec = "bar.json",
        interface = Builder,
    }l
}

use foo::Client as FooClient;
use bar::Client as BarClient;

Would something like that work for you? If not, how would you ideally imagine specifying the client type name?

mfpekala commented 3 months ago

I've got a project where I've put the progenitor-generated clients inside of their own crates, and am generating each client inside that crate's build.rs so it auto-updates. In the future I may put more logic around each client and would prefer to leave them as separate crates so that the main logic doesn't have to worry about generation at all.

So the build.rs inside this crate ends up looking pretty standard (I think):

let settings = progenitor::GenerationSettings::default();
let mut generator = progenitor::Generator::new(&settings);
let tokens = generator.generate_tokens(&spec).unwrap();
let ast = syn::parse2(tokens).unwrap();
let mut content = prettyplease::unparse(&ast);
// Then write this content to file...

I would love if there was something on GenerationSettings like with_client_struct_name that could edit the settings in this example to provide a custom name.

The solution you gave is definitely very clean, and I ended up at something similar for my situation, but I can't help but feel like this makes sense as something in GenerationSettings. Not a necessity at all but something that could be nice. Thanks!

ahl commented 3 months ago

Not unreasonable. Let me think about it and see if I can come up with something we can commit to.