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(progenitor-macro): if 'client.inner' is not set by user do not provide it to user-specified functions. #933

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 2 additions & 4 deletions example-macro/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use progenitor::generate_api;

generate_api!(
spec = "../sample_openapi/keeper.json",
inner_type = (),
pre_hook = (|_, request| {
pre_hook = (|request| {
println!("doing this {:?}", request);
}),
pre_hook_async = crate::add_auth_headers,
Expand All @@ -14,7 +13,6 @@ generate_api!(
);

async fn add_auth_headers(
_: &(),
req: &mut reqwest::Request,
) -> Result<(), reqwest::header::InvalidHeaderValue> {
// You can perform asynchronous, fallible work in a request hook, then
Expand All @@ -29,7 +27,7 @@ async fn add_auth_headers(
Ok(())
}

fn all_done(_: &(), _result: &reqwest::Result<reqwest::Response>) {}
fn all_done(_result: &reqwest::Result<reqwest::Response>) {}

mod buildomat {
use progenitor::generate_api;
Expand Down
15 changes: 9 additions & 6 deletions progenitor-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,21 +338,21 @@ impl Generator {
&self.settings.tag,
) {
(InterfaceStyle::Positional, TagStyle::Merged) => {
self.generate_tokens_positional_merged(&raw_methods)
self.generate_tokens_positional_merged(&raw_methods, self.settings.inner_type.is_some())
}
(InterfaceStyle::Positional, TagStyle::Separate) => {
unimplemented!("positional arguments with separate tags are currently unsupported")
}
(InterfaceStyle::Builder, TagStyle::Merged) => {
self.generate_tokens_builder_merged(&raw_methods)
self.generate_tokens_builder_merged(&raw_methods, self.settings.inner_type.is_some())
}
(InterfaceStyle::Builder, TagStyle::Separate) => {
let tag_info = spec
.tags
.iter()
.map(|tag| (&tag.name, tag))
.collect::<BTreeMap<_, _>>();
self.generate_tokens_builder_separate(&raw_methods, tag_info)
self.generate_tokens_builder_separate(&raw_methods, tag_info, self.settings.inner_type.is_some())
}
}?;

Expand Down Expand Up @@ -503,10 +503,11 @@ impl Generator {
fn generate_tokens_positional_merged(
&mut self,
input_methods: &[method::OperationMethod],
has_inner: bool,
) -> Result<TokenStream> {
let methods = input_methods
.iter()
.map(|method| self.positional_method(method))
.map(|method| self.positional_method(method, has_inner))
.collect::<Result<Vec<_>>>()?;

// The allow(unused_imports) on the `pub use` is necessary with Rust 1.76+, in case the
Expand All @@ -530,10 +531,11 @@ impl Generator {
fn generate_tokens_builder_merged(
&mut self,
input_methods: &[method::OperationMethod],
has_inner: bool,
) -> Result<TokenStream> {
let builder_struct = input_methods
.iter()
.map(|method| self.builder_struct(method, TagStyle::Merged))
.map(|method| self.builder_struct(method, TagStyle::Merged, has_inner))
.collect::<Result<Vec<_>>>()?;

let builder_methods = input_methods
Expand Down Expand Up @@ -577,10 +579,11 @@ impl Generator {
&mut self,
input_methods: &[method::OperationMethod],
tag_info: BTreeMap<&String, &openapiv3::Tag>,
has_inner: bool,
) -> Result<TokenStream> {
let builder_struct = input_methods
.iter()
.map(|method| self.builder_struct(method, TagStyle::Separate))
.map(|method| self.builder_struct(method, TagStyle::Separate, has_inner))
.collect::<Result<Vec<_>>>()?;

let (traits_and_impls, trait_preludes) =
Expand Down
17 changes: 12 additions & 5 deletions progenitor-impl/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ impl Generator {
pub(crate) fn positional_method(
&mut self,
method: &OperationMethod,
has_inner: bool,
) -> Result<TokenStream> {
let operation_id = format_ident!("{}", method.operation_id);

Expand Down Expand Up @@ -658,7 +659,7 @@ impl Generator {
success: success_type,
error: error_type,
body,
} = self.method_sig_body(method, quote! { self })?;
} = self.method_sig_body(method, quote! { self }, has_inner)?;

let method_impl = quote! {
#[doc = #doc_comment]
Expand Down Expand Up @@ -814,6 +815,7 @@ impl Generator {
&self,
method: &OperationMethod,
client: TokenStream,
has_inner: bool,
) -> Result<MethodSigBody> {
let param_names = method
.params
Expand Down Expand Up @@ -1129,22 +1131,26 @@ impl Generator {
}
};

let inner = match has_inner {
true => quote! { &#client.inner, },
false => quote! { },
};
let pre_hook = self.settings.pre_hook.as_ref().map(|hook| {
quote! {
(#hook)(&#client.inner, &#request_ident);
(#hook)(#inner &#request_ident);
}
});
let pre_hook_async = self.settings.pre_hook_async.as_ref().map(|hook| {
quote! {
match (#hook)(&#client.inner, &mut #request_ident).await {
match (#hook)(#inner &mut #request_ident).await {
Ok(_) => (),
Err(e) => return Err(Error::PreHookError(e.to_string())),
}
}
});
let post_hook = self.settings.post_hook.as_ref().map(|hook| {
quote! {
(#hook)(&#client.inner, &#result_ident);
(#hook)(#inner &#result_ident);
}
});

Expand Down Expand Up @@ -1474,6 +1480,7 @@ impl Generator {
&mut self,
method: &OperationMethod,
tag_style: TagStyle,
has_inner: bool
) -> Result<TokenStream> {
let struct_name = sanitize(&method.operation_id, Case::Pascal);
let struct_ident = format_ident!("{}", struct_name);
Expand Down Expand Up @@ -1718,7 +1725,7 @@ impl Generator {
success,
error,
body,
} = self.method_sig_body(method, quote! { #client_ident })?;
} = self.method_sig_body(method, quote! { #client_ident }, has_inner)?;

let send_doc = format!(
"Sends a `{}` request to `{}`",
Expand Down