stripe/stripe-java

StripeClient#subscriptions().list() causes class com.google.gson.internal.LinkedTreeMap cannot be cast to class com.stripe.model.HasId

oabcd3s opened this issue · 5 comments

Describe the bug

The following code throws an exception.

for (final Subscription subscription : stripeClient.subscriptions().list(params).autoPagingIterable()) {
      System.out.println("subscription: " + subscription);
}

The exception is:

Exception in thread "main" java.lang.RuntimeException: Unable to lazy-load stripe objects
	at com.stripe.model.PagingIterator.next(PagingIterator.java:57)
	at com.stripe.model.PagingIterator.next(PagingIterator.java:9)
	at SubscriptionsFailJavaMain.main(SubscriptionsFailJavaMain.java:32)
Caused by: java.lang.ClassCastException: class com.google.gson.internal.LinkedTreeMap cannot be cast to class com.stripe.model.HasId (com.google.gson.internal.LinkedTreeMap and com.stripe.model.HasId are in unnamed module of loader 'app')
	at com.stripe.model.StripeCollection.setResponseGetter(StripeCollection.java:92)
	at com.stripe.model.StripeObject.deserializeStripeObject(StripeObject.java:131)
	at com.stripe.net.LiveStripeResponseGetter.request(LiveStripeResponseGetter.java:72)
	at com.stripe.model.PagingIterator.list(PagingIterator.java:79)
	at com.stripe.model.PagingIterator.next(PagingIterator.java:53)
	... 2 more

To Reproduce

Here is a whole code to reproduce.

// The public API key
final String API_KEY = "sk_test_<redacted>";
final RequestOptions requestOptions = RequestOptions.builder().setApiKey(API_KEY).build();

final StripeClient stripeClient = new StripeClient(API_KEY);

final SubscriptionListParams params = SubscriptionListParams.builder()
    .setLimit(100L)
    .setCreated(
    SubscriptionListParams.Created.builder()
        .setGt(Instant.now().getEpochSecond() - 3 * 86400 /* 3 days */)
        .build()
    ).build();

// // Works!
// for (final Subscription subscription : Subscription.list(params, requestOptions).autoPagingIterable()) {
//   System.out.println("subscription: " + subscription);
// }

// Not works :(
for (final Subscription subscription : stripeClient.subscriptions().list(params).autoPagingIterable()) {
    System.out.println("subscription: " + subscription);
}

Expected behavior

List subscriptions without errors.

Code snippets

No response

OS

macOS

Java version

corretto-11.0.16.1

stripe-java version

23.6.0

API version

23.6.0

Additional context

No response

Hello @oabcd3s, thank you so much for the report. I've been able to reproduce the error and a fix should be forthcoming.

Hi @richardm-stripe, thank you for not only reproducing the error but also for the quick fix in the pull request. Really appreciate your swift action.

This fix should be in the v23.9.0-beta.1. Please let us know if you still see the issue.

Hi @vojkny, I confirmed 23.8.0 fixed this issue.