Skip to content

Commit c30a078

Browse files
authored
fix(cashu): skip serializing empty NUT15 settings in mint info (#1158)
Add is_empty method to nut15::Settings and configure skip_serializing_if attribute to prevent empty NUT15 objects from appearing in serialized mint info responses.
1 parent b43d2ae commit c30a078

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

crates/cashu/src/nuts/nut06.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ pub struct Nuts {
313313
/// NUT15 Settings
314314
#[serde(default)]
315315
#[serde(rename = "15")]
316+
#[serde(skip_serializing_if = "nut15::Settings::is_empty")]
316317
pub nut15: nut15::Settings,
317318
/// NUT17 Settings
318319
#[serde(default)]
@@ -676,4 +677,38 @@ mod tests {
676677

677678
assert_eq!(info, mint_info);
678679
}
680+
681+
#[test]
682+
fn test_nut15_not_serialized_when_empty() {
683+
// Test with default (empty) NUT15
684+
let mint_info = MintInfo {
685+
name: Some("Test Mint".to_string()),
686+
nuts: Nuts::default(),
687+
..Default::default()
688+
};
689+
690+
let json = serde_json::to_string(&mint_info).unwrap();
691+
let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
692+
693+
// NUT15 should not be present in the nuts object when methods is empty
694+
assert!(parsed["nuts"]["15"].is_null());
695+
696+
// Test with non-empty NUT15
697+
let mint_info_with_nut15 = MintInfo {
698+
name: Some("Test Mint".to_string()),
699+
nuts: Nuts::default().nut15(vec![MppMethodSettings {
700+
method: crate::PaymentMethod::Bolt11,
701+
unit: crate::CurrencyUnit::Sat,
702+
}]),
703+
..Default::default()
704+
};
705+
706+
let json = serde_json::to_string(&mint_info_with_nut15).unwrap();
707+
let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
708+
709+
// NUT15 should be present when methods is not empty
710+
assert!(!parsed["nuts"]["15"].is_null());
711+
assert!(parsed["nuts"]["15"]["methods"].is_array());
712+
assert_eq!(parsed["nuts"]["15"]["methods"].as_array().unwrap().len(), 1);
713+
}
679714
}

crates/cashu/src/nuts/nut15.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ pub struct Settings {
3434
pub methods: Vec<MppMethodSettings>,
3535
}
3636

37+
impl Settings {
38+
/// Check if methods is empty
39+
pub fn is_empty(&self) -> bool {
40+
self.methods.is_empty()
41+
}
42+
}
43+
3744
// Custom deserialization to handle both array and object formats
3845
impl<'de> Deserialize<'de> for Settings {
3946
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@@ -89,4 +96,18 @@ mod tests {
8996
let json = serde_json::to_string(&settings).unwrap();
9097
assert_eq!(json, r#"{"methods":[{"method":"bolt11","unit":"sat"}]}"#);
9198
}
99+
100+
#[test]
101+
fn test_nut15_settings_empty() {
102+
let settings = Settings { methods: vec![] };
103+
assert!(settings.is_empty());
104+
105+
let settings_with_data = Settings {
106+
methods: vec![MppMethodSettings {
107+
method: PaymentMethod::Bolt11,
108+
unit: CurrencyUnit::Sat,
109+
}],
110+
};
111+
assert!(!settings_with_data.is_empty());
112+
}
92113
}

0 commit comments

Comments
 (0)