From 55518ef7b0814286f38307184b1c94c05364d142 Mon Sep 17 00:00:00 2001 From: Alexander Korolev Date: Mon, 7 Oct 2024 22:32:22 +0200 Subject: [PATCH] added patch for swagger types (#140) --- examples/openapi.patch.toml | 6 +++++- examples/openapi.rs | 24 ++++++++++++++++++++++-- src/rest/mod.rs | 4 ++-- src/rest/url_enc.rs | 2 +- src/types.rs | 4 ++-- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/examples/openapi.patch.toml b/examples/openapi.patch.toml index cf57f2f..401bf55 100644 --- a/examples/openapi.patch.toml +++ b/examples/openapi.patch.toml @@ -9,4 +9,8 @@ from_type = "TypeVec>" rust_type = "TypeVec" [path."/admin/realms/{realm}/identity-provider/providers/{provider_id}:get:"] from_type = "Value" -rust_type = "IdentityProviderRepresentation" \ No newline at end of file +rust_type = "IdentityProviderRepresentation" +[type."ClientPolicyExecutorRepresentation:configuration"] +rust_type = "Option>" +[type."ClientPolicyConditionRepresentation:configuration"] +rust_type = "Option>" \ No newline at end of file diff --git a/examples/openapi.rs b/examples/openapi.rs index 3032fa6..fea6fe8 100644 --- a/examples/openapi.rs +++ b/examples/openapi.rs @@ -811,6 +811,11 @@ pub struct {name} {{ fields .into_iter() .map(|(field, field_name, field_case, field_type, deprecated)| { + let field_desc = Toml::field(name, &field_name); + let fld_type = field_desc + .as_ref() + .map(|field_desc| Cow::Borrowed(field_desc.rust_type.as_str())) + .unwrap_or(field_type); let is_rename = match field_case { FieldCase::Custom => true, FieldCase::Unknown => false, @@ -819,11 +824,11 @@ pub struct {name} {{ }; let field_desc = if !is_rename { - format!(r##" pub {field_name}: {field_type},"##) + format!(r##" pub {field_name}: {fld_type},"##) } else { format!( r##" #[serde(rename = "{field}")] - pub {field_name}: {field_type},"##, + pub {field_name}: {fld_type},"##, ) }; if !deprecated { @@ -1056,10 +1061,17 @@ pub enum {name} {{ convert: Option, } + #[derive(Debug, Deserialize)] + struct FieldDesc { + rust_type: String, + } + #[derive(Debug, Deserialize)] struct Toml { #[serde(default)] path: IndexMap>, + #[serde(default)] + r#type: IndexMap>, } impl Toml { @@ -1078,6 +1090,14 @@ pub enum {name} {{ .cloned() }) } + + fn field(structure: S, field: F) -> Option> + where + S: Display, + F: Display, + { + OPENAPI_PATCH.with(|toml| toml.r#type.get(&format!("{structure}:{field}",)).cloned()) + } } thread_local! { diff --git a/src/rest/mod.rs b/src/rest/mod.rs index 149c783..ca461ec 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -68,7 +68,7 @@ impl KeycloakServiceAccountAdminTokenRetriever { let realm = &self.realm; let response = self .reqwest_client - .post(&format!( + .post(format!( "{url}/realms/{realm}/protocol/openid-connect/token", )) .form(&[ @@ -131,7 +131,7 @@ impl KeycloakAdminToken { client: &reqwest::Client, ) -> Result { let response = client - .post(&format!( + .post(format!( "{url}/realms/{realm}/protocol/openid-connect/token", )) .form(&[ diff --git a/src/rest/url_enc.rs b/src/rest/url_enc.rs index 4e0efa2..767b526 100644 --- a/src/rest/url_enc.rs +++ b/src/rest/url_enc.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -pub fn encode_url_param<'v>(value: &'v str) -> impl Display + 'v { +pub fn encode_url_param(value: &str) -> impl Display + '_ { use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS}; const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`'); diff --git a/src/types.rs b/src/types.rs index 9080393..6cb0c42 100644 --- a/src/types.rs +++ b/src/types.rs @@ -371,14 +371,14 @@ pub struct ClientPoliciesRepresentation { #[cfg_attr(feature = "schemars", derive(JsonSchema))] pub struct ClientPolicyConditionRepresentation { pub condition: Option, - pub configuration: Option>, + pub configuration: Option>, } #[skip_serializing_none] #[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)] #[cfg_attr(feature = "schemars", derive(JsonSchema))] pub struct ClientPolicyExecutorRepresentation { - pub configuration: Option>, + pub configuration: Option>, pub executor: Option, }