Skip to content

Commit 81e7d99

Browse files
committed
[Rust] Add support for shallow object query params
1 parent 226a784 commit 81e7d99

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

modules/openapi-generator/src/main/resources/rust/reqwest/api.mustache

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,13 @@ pub {{#supportAsync}}async {{/supportAsync}}fn {{{operationId}}}(configuration:
155155
{{/isArray}}
156156
{{^isArray}}
157157
{{^isNullable}}
158+
{{#isModel}}
159+
let params = crate::apis::parse_flat_object(&serde_json::to_value({{{vendorExtensions.x-rust-param-identifier}}})?);
160+
req_builder = req_builder.query(&params);
161+
{{/isModel}}
162+
{{^isModel}}
158163
req_builder = req_builder.query(&[("{{{baseName}}}", &{{{vendorExtensions.x-rust-param-identifier}}}.to_string())]);
164+
{{/isModel}}
159165
{{/isNullable}}
160166
{{#isNullable}}
161167
{{#isDeepObject}}
@@ -165,9 +171,17 @@ pub {{#supportAsync}}async {{/supportAsync}}fn {{{operationId}}}(configuration:
165171
};
166172
{{/isDeepObject}}
167173
{{^isDeepObject}}
174+
{{#isModel}}
175+
if let Some(ref param_value) = {{{vendorExtensions.x-rust-param-identifier}}} {
176+
let params = crate::apis::parse_flat_object(&serde_json::to_value({{{vendorExtensions.x-rust-param-identifier}}})?);
177+
req_builder = req_builder.query(&params);
178+
};
179+
{{/isModel}}
180+
{{^isModel}}
168181
if let Some(ref param_value) = {{{vendorExtensions.x-rust-param-identifier}}} {
169182
req_builder = req_builder.query(&[("{{{baseName}}}", &param_value.to_string())]);
170183
};
184+
{{/isModel}}
171185
{{/isDeepObject}}
172186
{{/isNullable}}
173187
{{/isArray}}
@@ -186,7 +200,13 @@ pub {{#supportAsync}}async {{/supportAsync}}fn {{{operationId}}}(configuration:
186200
req_builder = req_builder.query(&params);
187201
{{/isDeepObject}}
188202
{{^isDeepObject}}
203+
{{#isModel}}
204+
let params = crate::apis::parse_flat_object(&serde_json::to_value(param_value)?);
205+
req_builder = req_builder.query(&params);
206+
{{/isModel}}
207+
{{^isModel}}
189208
req_builder = req_builder.query(&[("{{{baseName}}}", &param_value.to_string())]);
209+
{{/isModel}}
190210
{{/isDeepObject}}
191211
{{/isArray}}
192212
}

modules/openapi-generator/src/main/resources/rust/reqwest/api_mod.mustache

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,33 @@ pub fn urlencode<T: AsRef<str>>(s: T) -> String {
105105
::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
106106
}
107107

108+
pub fn parse_flat_object(value: &serde_json::Value) -> Vec<(String, String)> {
109+
if let serde_json::Value::Object(object) = value {
110+
let mut params = vec![];
111+
112+
for (key, value) in object {
113+
match value {
114+
serde_json::Value::Object(_) => {
115+
unimplemented!(
116+
"Only flat objects are supported, use parse_deep_object() instead"
117+
)
118+
}
119+
serde_json::Value::Array(array) => {
120+
for (i, value) in array.iter().enumerate() {
121+
params.push((format!("{key}[{i}]"), value.to_string()));
122+
}
123+
}
124+
serde_json::Value::String(s) => params.push((key.to_string(), s.clone())),
125+
_ => params.push((key.to_string(), value.to_string())),
126+
}
127+
}
128+
129+
return params;
130+
}
131+
132+
unimplemented!("Only objects are supported")
133+
}
134+
108135
pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String, String)> {
109136
if let serde_json::Value::Object(object) = value {
110137
let mut params = vec![];

0 commit comments

Comments
 (0)