Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update webhook API version validation #1906

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading