diff --git a/src/main/java/com/stripe/model/EventDataObjectDeserializer.java b/src/main/java/com/stripe/model/EventDataObjectDeserializer.java index 2cdef7bd9d2..a4b69fc153b 100644 --- a/src/main/java/com/stripe/model/EventDataObjectDeserializer.java +++ b/src/main/java/com/stripe/model/EventDataObjectDeserializer.java @@ -197,7 +197,25 @@ public StripeObject deserializeUnsafeWith(CompatibilityTransformer transformer) } private boolean apiVersionMatch() { - return getIntegrationApiVersion().equals(this.apiVersion); + + // Preserved for testing; we have tests that hook getIntegrationApiVersion + // to test with other api versions. + String currentApiVersion = getIntegrationApiVersion(); + if (!currentApiVersion.contains(".")) { + return this.apiVersion.equals(currentApiVersion); + } + + // If the event api version is from before we started adding + // a major release identifier, there's no way its compatible with this + // version + if (!this.apiVersion.contains(".")) { + return false; + } + + // versions are yyyy-MM-dd.releaseIdentifier + String eventReleaseTrain = this.apiVersion.split("\\.", 2)[1]; + String currentReleaseTrain = getIntegrationApiVersion().split("\\.", 2)[1]; + return eventReleaseTrain.equals(currentReleaseTrain); } /** Internal method to allow for testing with different Stripe version. */ diff --git a/src/test/java/com/stripe/functional/EventTest.java b/src/test/java/com/stripe/functional/EventTest.java index 55e8fa1f258..04cc79e94e8 100644 --- a/src/test/java/com/stripe/functional/EventTest.java +++ b/src/test/java/com/stripe/functional/EventTest.java @@ -55,7 +55,7 @@ public void testListWithTypedParams() throws StripeException { } @Test - public void tesGetDataObjectWithSameApiVersion() throws StripeException { + public void testGetDataObjectWithSameApiVersion() throws StripeException { final Event event = Event.retrieve(EVENT_ID); // Suppose event has the same API version as the library's pinned version event.setApiVersion(Stripe.API_VERSION); @@ -66,7 +66,20 @@ public void tesGetDataObjectWithSameApiVersion() throws StripeException { } @Test - public void tesGetDataObjectWithDifferentApiVersion() throws StripeException { + public void testGetDataObjectWithNewApiVersionInSameReleaseTrain() throws StripeException { + String expectedReleaseTrain = Stripe.API_VERSION.split("\\.")[1]; + final Event event = Event.retrieve(EVENT_ID); + // Suppose event has a different API version within the same release train as the + // library's pinned version + event.setApiVersion("2999-10-10." + expectedReleaseTrain); + + Optional stripeObject = event.getDataObjectDeserializer().getObject(); + + assertTrue(stripeObject.isPresent()); + } + + @Test + public void testGetDataObjectWithLegacyApiVersion() throws StripeException { final Event event = Event.retrieve(EVENT_ID); // Suppose event has different API version from the library's pinned version event.setApiVersion("2017-05-25"); @@ -77,4 +90,18 @@ public void tesGetDataObjectWithDifferentApiVersion() throws StripeException { // handling schema incompatibility assertFalse(stripeObject.isPresent()); } + + @Test + public void testGetDataObjectWithReleaseTrainMismatch() throws StripeException { + final Event event = Event.retrieve(EVENT_ID); + // Suppose event has different API version and different release train from + // the libraries pinned version + event.setApiVersion("2999-10-10.the_larch"); + + Optional stripeObject = event.getDataObjectDeserializer().getObject(); + + // See compatibility helper in `EventDataObjectDeserializerTest` for + // handling schema incompatibility + assertFalse(stripeObject.isPresent()); + } }