Skip to content

Commit

Permalink
🐛 deserialize Metadata fields to default if null
Browse files Browse the repository at this point in the history
  • Loading branch information
pkb-pmj committed Jul 9, 2023
1 parent 543a5b6 commit 0886117
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/linked_roles/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

/// Application Role Connection Metadata Type.
Expand Down Expand Up @@ -47,15 +47,25 @@ pub struct Metadata {
/// name of the metadata field (max 100 characters)
pub name: String,
/// translations of the name
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_default")]
pub name_localizations: HashMap<String, String>,
/// description of the metadata field (max 200 characters)
pub description: String,
/// translations of the description
#[serde(default)]
#[serde(deserialize_with = "deserialize_null_default")]
pub description_localizations: HashMap<String, String>,
}

/// https://github.com/serde-rs/serde/issues/1098#issuecomment-760711617
fn deserialize_null_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())
}

/// User Application Role Connection Structure.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct RoleConnection<T = HashMap<String, String>> {
Expand Down

0 comments on commit 0886117

Please sign in to comment.