From 0c858be59820a857b16b3302534fd2bedab16f16 Mon Sep 17 00:00:00 2001 From: Omid Rad Date: Thu, 25 Apr 2024 13:19:28 +0200 Subject: [PATCH 1/3] Stringify (Serialize) array query parameters --- examples/openapi.rs | 17 ++++++++++++++++- src/rest/generated_rest.rs | 3 +++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/examples/openapi.rs b/examples/openapi.rs index b2792fa..08d35aa 100644 --- a/examples/openapi.rs +++ b/examples/openapi.rs @@ -435,15 +435,30 @@ mod openapi { output.extend(query_parameters.into_iter().flat_map( |(query_parameter, query_parameter_name)| { + let stringify = match &query_parameter.schema { + Kind::Generic(v) => match v { + Generic::Array { .. } => r#" let v = v.join(",");"#, + _ => "", + }, + _ => "", + }; [ format!("if let Some(v) = {query_parameter_name} {{"), + stringify.to_string(), format!( r#" builder = builder.query(&[("{}", v)]);"#, query_parameter.name ), "}".into(), ] - .map(|line| format!(" {line}")) + .into_iter() + .filter_map(|line| { + if line.is_empty() { + None + } else { + Some(format!(" {line}")) + } + }) }, )); diff --git a/src/rest/generated_rest.rs b/src/rest/generated_rest.rs index 80866db..6352805 100644 --- a/src/rest/generated_rest.rs +++ b/src/rest/generated_rest.rs @@ -5405,12 +5405,14 @@ impl KeycloakAdmin { builder = builder.query(&[("max", v)]); } if let Some(v) = operation_types { + let v = v.join(","); builder = builder.query(&[("operationTypes", v)]); } if let Some(v) = resource_path { builder = builder.query(&[("resourcePath", v)]); } if let Some(v) = resource_types { + let v = v.join(","); builder = builder.query(&[("resourceTypes", v)]); } let response = builder.send().await?; @@ -5951,6 +5953,7 @@ impl KeycloakAdmin { builder = builder.query(&[("max", v)]); } if let Some(v) = type_ { + let v = v.join(","); builder = builder.query(&[("type", v)]); } if let Some(v) = user { From 186d8b40de4b23d96b8ce35a95f0905c5335b78a Mon Sep 17 00:00:00 2001 From: Omid Rad Date: Thu, 25 Apr 2024 13:25:04 +0200 Subject: [PATCH 2/3] simplify the code --- examples/openapi.rs | 21 +++++---------------- src/rest/generated_rest.rs | 9 +++------ 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/examples/openapi.rs b/examples/openapi.rs index 08d35aa..2ddb7d9 100644 --- a/examples/openapi.rs +++ b/examples/openapi.rs @@ -435,30 +435,19 @@ mod openapi { output.extend(query_parameters.into_iter().flat_map( |(query_parameter, query_parameter_name)| { - let stringify = match &query_parameter.schema { - Kind::Generic(v) => match v { - Generic::Array { .. } => r#" let v = v.join(",");"#, - _ => "", - }, - _ => "", + let value = match &query_parameter.schema { + Kind::Generic(Generic::Array { .. }) => r#"v.join(",")"#, + _ => "v", }; [ format!("if let Some(v) = {query_parameter_name} {{"), - stringify.to_string(), format!( - r#" builder = builder.query(&[("{}", v)]);"#, + r#" builder = builder.query(&[("{}", {value})]);"#, query_parameter.name ), "}".into(), ] - .into_iter() - .filter_map(|line| { - if line.is_empty() { - None - } else { - Some(format!(" {line}")) - } - }) + .map(|line| format!(" {line}")) }, )); diff --git a/src/rest/generated_rest.rs b/src/rest/generated_rest.rs index 6352805..2798368 100644 --- a/src/rest/generated_rest.rs +++ b/src/rest/generated_rest.rs @@ -5405,15 +5405,13 @@ impl KeycloakAdmin { builder = builder.query(&[("max", v)]); } if let Some(v) = operation_types { - let v = v.join(","); - builder = builder.query(&[("operationTypes", v)]); + builder = builder.query(&[("operationTypes", v.join(","))]); } if let Some(v) = resource_path { builder = builder.query(&[("resourcePath", v)]); } if let Some(v) = resource_types { - let v = v.join(","); - builder = builder.query(&[("resourceTypes", v)]); + builder = builder.query(&[("resourceTypes", v.join(","))]); } let response = builder.send().await?; Ok(error_check(response).await?.json().await?) @@ -5953,8 +5951,7 @@ impl KeycloakAdmin { builder = builder.query(&[("max", v)]); } if let Some(v) = type_ { - let v = v.join(","); - builder = builder.query(&[("type", v)]); + builder = builder.query(&[("type", v.join(","))]); } if let Some(v) = user { builder = builder.query(&[("user", v)]); From 1c02550d5ea8d74aac2b69d33ab781f022ccf74e Mon Sep 17 00:00:00 2001 From: Omid Rad Date: Thu, 25 Apr 2024 14:32:54 +0200 Subject: [PATCH 3/3] change query param format --- examples/openapi.rs | 14 +++++++------- src/rest/generated_rest.rs | 14 +++++++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/examples/openapi.rs b/examples/openapi.rs index 2ddb7d9..ae76e70 100644 --- a/examples/openapi.rs +++ b/examples/openapi.rs @@ -435,16 +435,16 @@ mod openapi { output.extend(query_parameters.into_iter().flat_map( |(query_parameter, query_parameter_name)| { - let value = match &query_parameter.schema { - Kind::Generic(Generic::Array { .. }) => r#"v.join(",")"#, - _ => "v", + let value = match query_parameter.schema { + Kind::Generic(Generic::Array { .. }) => format!( + r#"v.into_iter().map(|e| ("{}", e)).collect::>()"#, + query_parameter.name + ), + _ => format!(r#"[("{}", v)]"#, query_parameter.name), }; [ format!("if let Some(v) = {query_parameter_name} {{"), - format!( - r#" builder = builder.query(&[("{}", {value})]);"#, - query_parameter.name - ), + format!(r#" builder = builder.query(&{value});"#,), "}".into(), ] .map(|line| format!(" {line}")) diff --git a/src/rest/generated_rest.rs b/src/rest/generated_rest.rs index 2798368..cb57a15 100644 --- a/src/rest/generated_rest.rs +++ b/src/rest/generated_rest.rs @@ -5405,13 +5405,21 @@ impl KeycloakAdmin { builder = builder.query(&[("max", v)]); } if let Some(v) = operation_types { - builder = builder.query(&[("operationTypes", v.join(","))]); + builder = builder.query( + &v.into_iter() + .map(|e| ("operationTypes", e)) + .collect::>(), + ); } if let Some(v) = resource_path { builder = builder.query(&[("resourcePath", v)]); } if let Some(v) = resource_types { - builder = builder.query(&[("resourceTypes", v.join(","))]); + builder = builder.query( + &v.into_iter() + .map(|e| ("resourceTypes", e)) + .collect::>(), + ); } let response = builder.send().await?; Ok(error_check(response).await?.json().await?) @@ -5951,7 +5959,7 @@ impl KeycloakAdmin { builder = builder.query(&[("max", v)]); } if let Some(v) = type_ { - builder = builder.query(&[("type", v.join(","))]); + builder = builder.query(&v.into_iter().map(|e| ("type", e)).collect::>()); } if let Some(v) = user { builder = builder.query(&[("user", v)]);