-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #1190 by marking RequestOptions transient
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
1 parent
4262fcc
commit 2973ada
Showing
3 changed files
with
28 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/stripe/net/ReflectionCheckingTypeAdapterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |