Skip to content

Commit

Permalink
kubeconfig: deserialize null vectors as default (#1142)
Browse files Browse the repository at this point in the history
deserialize null vectors as default

Signed-off-by: goenning <[email protected]>
Co-authored-by: Eirik A <[email protected]>
  • Loading branch information
goenning and clux authored Feb 20, 2023
1 parent 9ebd055 commit 7e77855
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions kube-client/src/config/file_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ pub struct Kubeconfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub preferences: Option<Preferences>,
/// Referencable names to cluster configs
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default, deserialize_with = "deserialize_null_as_default")]
pub clusters: Vec<NamedCluster>,
/// Referencable names to user configs
#[serde(rename = "users")]
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default, deserialize_with = "deserialize_null_as_default")]
pub auth_infos: Vec<NamedAuthInfo>,
/// Referencable names to context configs
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(default, deserialize_with = "deserialize_null_as_default")]
pub contexts: Vec<NamedContext>,
/// The name of the context that you would like to use by default
#[serde(rename = "current-context")]
Expand Down Expand Up @@ -152,6 +152,15 @@ where
}
}

fn deserialize_null_as_default<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
T: Default + Deserialize<'de>,
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or_default())
}

/// AuthInfo stores information to tell cluster who you are.
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct AuthInfo {
Expand Down

0 comments on commit 7e77855

Please sign in to comment.