From 7c991963d32de62c52b34d4ba4e5c344c70b438c Mon Sep 17 00:00:00 2001 From: David Brownman <109395161+xavdid-stripe@users.noreply.github.com> Date: Wed, 5 Feb 2025 11:49:33 -0800 Subject: [PATCH] Ensure `getRawJsonObject` returns data for constructed webhooks (#1946) * construct fake response during webhook parsing * formatting --- src/main/java/com/stripe/net/Webhook.java | 8 ++++++++ src/test/java/com/stripe/net/WebhookTest.java | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/main/java/com/stripe/net/Webhook.java b/src/main/java/com/stripe/net/Webhook.java index 7b5a34febf2..ea67ea34362 100644 --- a/src/main/java/com/stripe/net/Webhook.java +++ b/src/main/java/com/stripe/net/Webhook.java @@ -9,6 +9,7 @@ import java.security.NoSuchAlgorithmException; import java.time.Clock; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; @@ -72,6 +73,13 @@ public static Event constructEvent( StripeObject.deserializeStripeObject( payload, Event.class, ApiResource.getGlobalResponseGetter()); Signature.verifyHeader(payload, sigHeader, secret, tolerance, clock); + // StripeObjects source their raw JSON object from their last response, but constructed webhooks + // don't have that + // in order to make the raw object available on parsed events, we fake the response. + if (event.getLastResponse() == null) { + event.setLastResponse( + new StripeResponse(200, HttpHeaders.of(Collections.emptyMap()), payload)); + } return event; } diff --git a/src/test/java/com/stripe/net/WebhookTest.java b/src/test/java/com/stripe/net/WebhookTest.java index 175f0cd5feb..c6a9c89aa77 100644 --- a/src/test/java/com/stripe/net/WebhookTest.java +++ b/src/test/java/com/stripe/net/WebhookTest.java @@ -1,5 +1,6 @@ package com.stripe.net; +import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -325,4 +326,13 @@ public void testStripeClientConstructEventWithTolerance() Mockito.verify(responseGetter).request(Mockito.any(), Mockito.any()); } + + @Test + public void testConstructEventWithRawJson() + throws StripeException, NoSuchAlgorithmException, InvalidKeyException { + + final Event event = Webhook.constructEvent(payload, generateSigHeader(), secret); + + assertNotNull(event.getRawJsonObject()); + } }