diff --git a/src/main.rs b/src/main.rs index 7b39fc7..2a26830 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1063,6 +1063,7 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result { a(" */"); let mut bounds: Vec = Vec::new(); + let mut query: Vec<(String, bool)> = Vec::new(); let (body_param, body_func) = if let Some(b) = &o.request_body { let b = b.item()?; @@ -1104,6 +1105,11 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result { parameter_data, style: openapiv3::PathStyle::Simple, } => { + /* + * Path parameters MUST be required. + */ + assert!(parameter_data.required); + let nam = ¶meter_data.name; let tid = ts.select(None, parameter_data.schema()?)?; let typ = ts.render_type(&tid, false)?; @@ -1121,21 +1127,21 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result { } } - /* - * XXX Parameter types should probably go through - * the type space... - */ let nam = ¶meter_data.name; - let typ = parameter_data.render_type()?; + let tid = ts.select(None, parameter_data.schema()?)?; + let tid = if parameter_data.required { + tid + } else { + /* + * If this is an optional parameter, we need an + * Option version of the type. + */ + ts.id_for_optional(&tid) + }; + let typ = ts.render_type(&tid, false)?; a(&format!(" {}: {},", nam, typ)); - /* - * XXX Form-style query parameters appear in the - * function arguments, but there's nothing that - * currently puts them in the generated URL. Don't - * silently generate an incorrect client. - */ - bail!("XXX query parameters do not work yet"); + query.push((nam.to_string(), !parameter_data.required)); } x => bail!("unhandled parameter type: {:#?}", x), } @@ -1210,6 +1216,31 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result { let tmp = template::parse(p)?; a(&tmp.compile()); + /* + * If there is a query string, generate that now. + */ + if !query.is_empty() { + a(" let mut query = Vec::new();"); + for (qn, opt) in query.iter() { + if *opt { + a(&format!(" if let Some(v) = &{} {{", qn)); + a(&format!( + " \ + query.push((\"{}\".to_string(), v.to_string()));", + qn + )); + a(" }}"); + } else { + a(&format!( + " \ + query.push((\"{}\".to_string(), {}.to_string()));", + qn, qn + )); + } + } + a(""); + } + /* * Perform the request. */ @@ -1220,6 +1251,9 @@ fn gen(api: &OpenAPI, ts: &mut TypeSpace) -> Result { if let Some(f) = &body_func { a(&format!(" .{}(body)", f)); } + if !query.is_empty() { + a(" .query(&query)"); + } a(" .send()"); a(" .await?"); a(" .error_for_status()?;"); /* XXX */