diff --git a/build.gradle b/build.gradle index 697392287cc..77ed44fe9b0 100644 --- a/build.gradle +++ b/build.gradle @@ -119,6 +119,8 @@ test { events "passed", "skipped", "failed" exceptionFormat "full" } + + systemProperty "stripe.disallowGlobalResponseGetterFallback", "true" } spotless { diff --git a/src/main/java/com/stripe/net/ApiResource.java b/src/main/java/com/stripe/net/ApiResource.java index fa12254837e..d9a4a85888e 100644 --- a/src/main/java/com/stripe/net/ApiResource.java +++ b/src/main/java/com/stripe/net/ApiResource.java @@ -32,11 +32,13 @@ public void setResponseGetter(StripeResponseGetter srg) { protected StripeResponseGetter getResponseGetter() { if (this.responseGetter == null) { - throw new IllegalStateException( - "The StripeResponseGetter has not been set on this resource. " - + "This should not happen and is likely a bug in the Stripe Java library. " - + "Please open a github issue on https://github.com/stripe/stripe-java or contact " - + "Stripe Support at https://support.stripe.com/contact/"); + if (isGlobalFallbackDisallowed()) { + throw new IllegalStateException( + "The resource you're trying to use was deserialized without the use of ApiResource.GSON.\n" + + "ApiResource.GSON contains type adapters that are important for correct deserialization and functioning of the resource.\n" + + "To deserialize Events use Webhook.constructEvent, for other resources use ApiResource.GSON.fromJson(...)`"); + } + return getGlobalResponseGetter(); } return this.responseGetter; } @@ -145,4 +147,9 @@ public static ExpandableField setExpandableFieldId( return new ExpandableField<>(newId, currentObject.getExpanded()); } + + private static boolean isGlobalFallbackDisallowed() { + return Objects.equals( + System.getProperty("stripe.disallowGlobalResponseGetterFallback"), "true"); + } } diff --git a/src/test/java/com/stripe/net/ApiResourceTest.java b/src/test/java/com/stripe/net/ApiResourceTest.java index 3a449047283..103904414d5 100644 --- a/src/test/java/com/stripe/net/ApiResourceTest.java +++ b/src/test/java/com/stripe/net/ApiResourceTest.java @@ -57,6 +57,9 @@ public void testInternalDeserializeSetsResponseGetter() { Charge charge = ApiResource.INTERNAL_GSON.fromJson(json, Charge.class); IllegalStateException e = assertThrows(IllegalStateException.class, () -> charge.update(new HashMap<>())); - assertTrue(e.getMessage().contains("contact Stripe Support")); + assertTrue( + e.getMessage() + .contains( + "The resource you're trying to use was deserialized without the use of ApiResource.GSON")); } }