Skip to content

Commit

Permalink
added patch for swagger types (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilork authored Oct 7, 2024
1 parent 3afd402 commit 55518ef
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion examples/openapi.patch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ from_type = "TypeVec<TypeMap<String, String>>"
rust_type = "TypeVec<RequiredActionProviderRepresentation>"
[path."/admin/realms/{realm}/identity-provider/providers/{provider_id}:get:"]
from_type = "Value"
rust_type = "IdentityProviderRepresentation"
rust_type = "IdentityProviderRepresentation"
[type."ClientPolicyExecutorRepresentation:configuration"]
rust_type = "Option<TypeMap<String, TypeValue>>"
[type."ClientPolicyConditionRepresentation:configuration"]
rust_type = "Option<TypeMap<String, TypeValue>>"
24 changes: 22 additions & 2 deletions examples/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -1056,10 +1061,17 @@ pub enum {name} {{
convert: Option<String>,
}

#[derive(Debug, Deserialize)]
struct FieldDesc {
rust_type: String,
}

#[derive(Debug, Deserialize)]
struct Toml {
#[serde(default)]
path: IndexMap<String, Arc<PathDesc>>,
#[serde(default)]
r#type: IndexMap<String, Arc<FieldDesc>>,
}

impl Toml {
Expand All @@ -1078,6 +1090,14 @@ pub enum {name} {{
.cloned()
})
}

fn field<S, F>(structure: S, field: F) -> Option<Arc<FieldDesc>>
where
S: Display,
F: Display,
{
OPENAPI_PATCH.with(|toml| toml.r#type.get(&format!("{structure}:{field}",)).cloned())
}
}

thread_local! {
Expand Down
4 changes: 2 additions & 2 deletions src/rest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&[
Expand Down Expand Up @@ -131,7 +131,7 @@ impl KeycloakAdminToken {
client: &reqwest::Client,
) -> Result<KeycloakAdminToken, KeycloakError> {
let response = client
.post(&format!(
.post(format!(
"{url}/realms/{realm}/protocol/openid-connect/token",
))
.form(&[
Expand Down
2 changes: 1 addition & 1 deletion src/rest/url_enc.rs
Original file line number Diff line number Diff line change
@@ -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'`');
Expand Down
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,14 @@ pub struct ClientPoliciesRepresentation {
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct ClientPolicyConditionRepresentation {
pub condition: Option<TypeString>,
pub configuration: Option<TypeVec<TypeValue>>,
pub configuration: Option<TypeMap<String, TypeValue>>,
}

#[skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct ClientPolicyExecutorRepresentation {
pub configuration: Option<TypeVec<TypeValue>>,
pub configuration: Option<TypeMap<String, TypeValue>>,
pub executor: Option<TypeString>,
}

Expand Down

0 comments on commit 55518ef

Please sign in to comment.