Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fn deserialize_null_as_default for deserializing JSON null values #1115

Merged
merged 11 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
52 changes: 52 additions & 0 deletions sdk/core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,55 @@ where
.or_else(|_| T::deserialize(serde_json::Value::String(v.to_lowercase())))
.map_err(de::Error::custom)
}

/// Deserialize JSON null as default
/// https://github.com/serde-rs/serde/issues/1098
pub 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())
}

#[cfg(test)]
mod tests {
use super::*;
use serde::Serialize;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
pub struct SiteConfig {
#[serde(
rename = "appSettings",
default,
deserialize_with = "deserialize_null_as_default",
skip_serializing_if = "Vec::is_empty"
)]
pub app_settings: Vec<NameValuePair>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
pub struct NameValuePair {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
}

#[test]
fn deserialize_empty() -> crate::Result<()> {
let bytes = br#"{}"#;
let site_config: SiteConfig = serde_json::from_slice(bytes)?;
assert_eq!(Vec::<NameValuePair>::default(), site_config.app_settings);
Ok(())
}

#[test]
fn deserialize_null() -> crate::Result<()> {
let bytes = br#"{ "appSettings": null }"#;
let site_config: SiteConfig = serde_json::from_slice(bytes)?;
assert_eq!(Vec::<NameValuePair>::default(), site_config.app_settings);
Ok(())
}
}
2 changes: 1 addition & 1 deletion services/autorust/codegen/src/codegen_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ fn create_struct(cg: &CodeGen, schema: &SchemaGen, struct_name: &str, pageable:
// Must specify `default` when using `with` for `Option`
serde_attrs.push(quote! { default, with = "azure_core::date::rfc1123::option"});
} else if type_name.is_vec() {
serde_attrs.push(quote! { default, skip_serializing_if = "Vec::is_empty"});
serde_attrs.push(quote! { default, deserialize_with = "azure_core::util::deserialize_null_as_default", skip_serializing_if = "Vec::is_empty"});
} else {
serde_attrs.push(quote! { default, skip_serializing_if = "Option::is_none"});
}
Expand Down
30 changes: 25 additions & 5 deletions services/mgmt/activedirectory/src/package_2017_04_01/models.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 31 additions & 6 deletions services/mgmt/activedirectory/src/package_2020_03/models.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading