Skip to content

Commit

Permalink
Fix #1190 by marking RequestOptions transient (#1197)
Browse files Browse the repository at this point in the history
Also try to prevent similar problems in the future by checking that
we only use the reflection-based type adapter for classes in `com.stripe.`.
  • Loading branch information
ramon-stripe committed Apr 22, 2021
1 parent 4262fcc commit 6029987
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/stripe/model/StripeCollection.java
Expand Up @@ -50,7 +50,7 @@ public abstract class StripeCollection<T extends HasId> extends StripeObject

@Getter(onMethod_ = {@Override})
@Setter(onMethod = @__({@Override}))
private RequestOptions requestOptions;
private transient RequestOptions requestOptions;

@Getter(onMethod_ = {@Override})
@Setter(onMethod = @__({@Override}))
Expand Down
Expand Up @@ -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<TypeAdapterFactory> getAll() {
Expand Down
@@ -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 <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!type.getType().getTypeName().startsWith("com.stripe.")) {
TypeAdapter<T> 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;
}
}

0 comments on commit 6029987

Please sign in to comment.