Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(connectors): [bambora] use custom deserializer to treat empty string fields as None (#10172)

Akshaya Shankar5b7a105f7cde
diff --git a/crates/hyperswitch_connectors/src/connectors/bambora/transformers.rs b/crates/hyperswitch_connectors/src/connectors/bambora/transformers.rs
index 2f801ecd9f..53cbda6683 100644
--- a/crates/hyperswitch_connectors/src/connectors/bambora/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/bambora/transformers.rs
@@ -24,8 +24,8 @@ use crate::{
PaymentsSyncResponseRouterData, RefundsResponseRouterData, ResponseRouterData,
},
utils::{
- self, AddressDetailsData, BrowserInformationData, CardData as _,
- PaymentsAuthorizeRequestData, PaymentsCompleteAuthorizeRequestData,
+ self, deserialize_option_empty_string_to_none, AddressDetailsData, BrowserInformationData,
+ CardData as _, PaymentsAuthorizeRequestData, PaymentsCompleteAuthorizeRequestData,
PaymentsSyncRequestData, RouterData as _,
},
};
@@ -379,6 +379,7 @@ pub struct AddressData {
address_line2: Option<Secret<String>>,
city: Option<String>,
province: Option<Secret<String>>,
+ #[serde(deserialize_with = "deserialize_option_empty_string_to_none")]
country: Option<enums::CountryAlpha2>,
postal_code: Option<Secret<String>>,
phone_number: Option<Secret<String>>,
diff --git a/crates/hyperswitch_connectors/src/utils.rs b/crates/hyperswitch_connectors/src/utils.rs
index fe532d6f9b..f6e68f390f 100644
--- a/crates/hyperswitch_connectors/src/utils.rs
+++ b/crates/hyperswitch_connectors/src/utils.rs
@@ -7968,6 +7968,26 @@ where
}
}
+/// Custom deserializer for `Option<T>` that treats empty strings or zero-minor amounts as `None`.
+pub fn deserialize_option_empty_string_to_none<'de, D, T>(
+ deserializer: D,
+) -> Result<Option<T>, D::Error>
+where
+ D: serde::de::Deserializer<'de>,
+ T: FromStr,
+ <T as FromStr>::Err: std::fmt::Display,
+{
+ let string_data: Option<String> = Option::deserialize(deserializer)?;
+ match string_data {
+ Some(value) if !value.trim().is_empty() => value.parse::<T>().map(Some).map_err(|e| {
+ serde::de::Error::custom(format!(
+ "Invalid value received from connector: {value} ({e})"
+ ))
+ }),
+ _ => Ok(None),
+ }
+}
+
#[macro_export]
macro_rules! convert_connector_response_to_domain_response {
($connector_type:ty, $response_type:ty, $convert_fn:expr) => {