diff --git a/src/main/java/com/stripe/model/StripeCollection.java b/src/main/java/com/stripe/model/StripeCollection.java index 87865083b21..d592d4aa0c9 100644 --- a/src/main/java/com/stripe/model/StripeCollection.java +++ b/src/main/java/com/stripe/model/StripeCollection.java @@ -50,7 +50,7 @@ public abstract class StripeCollection extends StripeObject @Getter(onMethod_ = {@Override}) @Setter(onMethod = @__({@Override})) - private RequestOptions requestOptions; + private transient RequestOptions requestOptions; @Getter(onMethod_ = {@Override}) @Setter(onMethod = @__({@Override})) diff --git a/src/main/java/com/stripe/net/ApiResourceTypeAdapterFactoryProvider.java b/src/main/java/com/stripe/net/ApiResourceTypeAdapterFactoryProvider.java index dd8cada57e5..16076c6e931 100644 --- a/src/main/java/com/stripe/net/ApiResourceTypeAdapterFactoryProvider.java +++ b/src/main/java/com/stripe/net/ApiResourceTypeAdapterFactoryProvider.java @@ -18,6 +18,7 @@ final class ApiResourceTypeAdapterFactoryProvider { factories.add(new BalanceTransactionSourceTypeAdapterFactory()); factories.add(new ExternalAccountTypeAdapterFactory()); factories.add(new PaymentSourceTypeAdapterFactory()); + factories.add(new ReflectionCheckingTypeAdapterFactory()); } public static List getAll() { diff --git a/src/main/java/com/stripe/net/ReflectionCheckingTypeAdapterFactory.java b/src/main/java/com/stripe/net/ReflectionCheckingTypeAdapterFactory.java new file mode 100644 index 00000000000..40acde38b58 --- /dev/null +++ b/src/main/java/com/stripe/net/ReflectionCheckingTypeAdapterFactory.java @@ -0,0 +1,26 @@ +package com.stripe.net; + +import com.google.gson.Gson; +import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.internal.bind.ReflectiveTypeAdapterFactory; +import com.google.gson.reflect.TypeToken; + +/** + * {@link TypeAdapterFactory} that checks that we don't use {@link ReflectiveTypeAdapterFactory} + * accidentally for classes outside {@code com.stripe} packages. This usually happens when we forget + * to mark a field {@code transient}. + */ +class ReflectionCheckingTypeAdapterFactory implements TypeAdapterFactory { + @Override + public TypeAdapter create(Gson gson, TypeToken type) { + if (!type.getType().getTypeName().startsWith("com.stripe.")) { + TypeAdapter adapter = gson.getDelegateAdapter(this, type); + if (adapter instanceof ReflectiveTypeAdapterFactory.Adapter) { + throw new IllegalArgumentException( + "Refusing to create type reflection-based type adapter for external class: " + type); + } + } + return null; + } +}