Skip to content

Commit

Permalink
Merge pull request #620 from lasantosr/webhook-event-serde
Browse files Browse the repository at this point in the history
feat(next): Serialize and deserialize webhook events
  • Loading branch information
mzeitlin11 authored Oct 26, 2024
2 parents e246db4 + 595d28c commit 1af8bc9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions async-stripe-webhook/src/generated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,8 @@ const _: () = {
};

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[non_exhaustive]
/// The event data for a webhook event.
pub enum EventObject {
Expand Down Expand Up @@ -1227,6 +1229,10 @@ pub enum EventObject {
/// Occurs whenever a received_debit is created as a result of funds being pulled by another account.
#[cfg(feature = "async-stripe-treasury")]
TreasuryReceivedDebitCreated(stripe_treasury::TreasuryReceivedDebit),
#[cfg_attr(
any(feature = "deserialize", feature = "serialize"),
serde(with = "stripe_types::with_serde_json")
)]
Unknown(miniserde::json::Value),
}
impl EventObject {
Expand Down
8 changes: 8 additions & 0 deletions async-stripe-webhook/src/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use stripe_shared::ApiVersion;
use crate::{EventObject, WebhookError};

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct Event {
/// The connected account that originated the event.
pub account: Option<String>,
Expand All @@ -34,6 +36,8 @@ pub struct Event {
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct EventData {
/// Object containing the API resource relevant to the event.
///
Expand All @@ -43,6 +47,10 @@ pub struct EventData {
///
/// If an array attribute has any updated elements, this object contains the entire array.
/// In Stripe API versions 2017-04-06 or earlier, an updated array attribute in this object includes only the updated array elements.
#[cfg_attr(
any(feature = "deserialize", feature = "serialize"),
serde(with = "stripe_types::with_serde_json_opt")
)]
pub previous_attributes: Option<miniserde::json::Value>,
}

Expand Down
10 changes: 9 additions & 1 deletion openapi/src/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ fn write_event_object(components: &Components, out_path: &Path) -> anyhow::Resul
r#""{evt_type}" => Self::{ident}(FromValueOpt::from_value(data)?),"#
);
}
let _ = writeln!(enum_body, "Unknown(miniserde::json::Value),");
let _ = writedoc! {enum_body, r#"
#[cfg_attr(
any(feature = "deserialize", feature = "serialize"),
serde(with = "stripe_types::with_serde_json")
)]
Unknown(miniserde::json::Value),
"#};

write_derives_line(&mut out, Default::default());
let _ = writedoc! {out, r#"
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[non_exhaustive]
/// The event data for a webhook event.
pub enum EventObject {{
Expand Down

0 comments on commit 1af8bc9

Please sign in to comment.