Skip to content

Commit

Permalink
Update webhook API version validation (#1906)
Browse files Browse the repository at this point in the history
modified apiVersionMatch to verify the release trains match (vs testing for full api version equality), or return false if the event version does not have a release train
  • Loading branch information
jar-stripe authored Oct 23, 2024
1 parent 3058258 commit 9238407
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/main/java/com/stripe/model/EventDataObjectDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
31 changes: 29 additions & 2 deletions src/test/java/com/stripe/functional/EventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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> 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");
Expand All @@ -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> stripeObject = event.getDataObjectDeserializer().getObject();

// See compatibility helper in `EventDataObjectDeserializerTest` for
// handling schema incompatibility
assertFalse(stripeObject.isPresent());
}
}

0 comments on commit 9238407

Please sign in to comment.