diff options
author | Cyborus <cyborus@noreply.codeberg.org> | 2024-10-23 20:53:11 +0200 |
---|---|---|
committer | Cyborus <cyborus@noreply.codeberg.org> | 2024-10-23 20:53:11 +0200 |
commit | 2539731dd8197055ce6176ccbc4a1bf77e7bc79e (patch) | |
tree | 0de68f13331a2a2a27f9b634c1f6a9c400f9d06f /generator | |
parent | Merge pull request 'Clippy Fixes' (#74) from Pi-Cla/forgejo-api:clippy into main (diff) | |
parent | ci: update ci forgejo to 9.0 (diff) | |
download | forgejo-api-main.tar.xz forgejo-api-main.zip |
Reviewed-on: https://codeberg.org/Cyborus/forgejo-api/pulls/75
Diffstat (limited to 'generator')
-rw-r--r-- | generator/src/methods.rs | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/generator/src/methods.rs b/generator/src/methods.rs index 141fc7d..ffed912 100644 --- a/generator/src/methods.rs +++ b/generator/src/methods.rs @@ -174,10 +174,14 @@ fn fn_args_from_op(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String> { args.push_str(": "); args.push_str(&ty); } - ParameterIn::FormData { param: _ } => { + ParameterIn::FormData { param } => { args.push_str(", "); args.push_str(&crate::sanitize_ident(&full_param.name)); - args.push_str(": Vec<u8>"); + if param.required { + args.push_str(": Vec<u8>"); + } else { + args.push_str(": Option<Vec<u8>>"); + } } } } @@ -360,6 +364,7 @@ fn create_method_request( let mut has_query = false; // let mut has_headers = false; let mut body_method = String::new(); + let mut form_methods = String::new(); if let Some(params) = &op.parameters { for param in params { let param = param.deref(spec)?; @@ -378,11 +383,15 @@ fn create_method_request( body_method = format!(".json(&{name})"); } } - ParameterIn::FormData { param: _ } => { - if !body_method.is_empty() { - eyre::bail!("cannot have more than one body parameter"); + ParameterIn::FormData { param } => { + if param.required { + form_methods += &format!("let builder = builder.multipart(reqwest::multipart::Form::new().part(\"attachment\", reqwest::multipart::Part::bytes({name}).file_name(\"file\").mime_str(\"*/*\").unwrap()));"); + } else { + form_methods += &format!("let builder = match {name} {{ + Some({name}) => builder.multipart(reqwest::multipart::Form::new().part(\"attachment\", reqwest::multipart::Part::bytes({name}).file_name(\"file\").mime_str(\"*/*\").unwrap())), + None => builder, + }};"); } - body_method = format!(".multipart(reqwest::multipart::Form::new().part(\"attachment\", reqwest::multipart::Part::bytes({name}).file_name(\"file\").mime_str(\"*/*\").unwrap()))"); } } } @@ -397,7 +406,15 @@ fn create_method_request( format!("\"{fmt_str}\"") }; - let out = format!("let request = self.{method}({path_arg}){body_method}.build()?;"); + let out = if form_methods.is_empty() { + format!("let request = self.{method}({path_arg}){body_method}.build()?;") + } else { + format!( + "let builder = self.{method}({path_arg}){body_method}; + {form_methods}\ + let request = builder.build()?;" + ) + }; Ok(out) } |