Skip to content

Commit

Permalink
Replace ReflectionCheckingTypeAdapterFactory with a ReflectionAccessF…
Browse files Browse the repository at this point in the history
…ilter (#1496)

* Replace ReflectionCheckingTypeAdapterFactory with a Filter

* Add test
  • Loading branch information
richardm-stripe authored Dec 21, 2022
1 parent 3ce22a7 commit e9fcfaa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ repositories {
dependencies {
errorprone group: "com.google.errorprone", name: "error_prone_core", version: "2.10.0"
errorproneJavac group: "com.google.errorprone", name: "javac", version:"9+181-r4173-1"
implementation group: "com.google.code.gson", name: "gson", version:"2.9.0"
implementation group: "com.google.code.gson", name: "gson", version:"2.9.1"
testImplementation group: "com.google.guava", name: "guava", version:"31.0.1-jre"
testImplementation group: "com.squareup.okhttp3", name: "mockwebserver", version: "4.9.1"
testImplementation group: "org.mockito", name: "mockito-core", version:"4.1.0"
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/stripe/net/ApiResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ReflectionAccessFilter;
import com.google.gson.TypeAdapterFactory;
import com.stripe.Stripe;
import com.stripe.exception.InvalidRequestException;
Expand Down Expand Up @@ -36,7 +37,17 @@ private static Gson createGson() {
.registerTypeAdapter(Event.Data.class, new EventDataDeserializer())
.registerTypeAdapter(Event.Request.class, new EventRequestDeserializer())
.registerTypeAdapter(ExpandableField.class, new ExpandableFieldDeserializer())
.registerTypeAdapter(StripeRawJsonObject.class, new StripeRawJsonObjectDeserializer());
.registerTypeAdapter(StripeRawJsonObject.class, new StripeRawJsonObjectDeserializer())
.addReflectionAccessFilter(
new ReflectionAccessFilter() {
@Override
public ReflectionAccessFilter.FilterResult check(Class<?> rawClass) {
if (rawClass.getTypeName().startsWith("com.stripe.")) {
return ReflectionAccessFilter.FilterResult.ALLOW;
}
return ReflectionAccessFilter.FilterResult.BLOCK_ALL;
}
});

for (TypeAdapterFactory factory : ApiResourceTypeAdapterFactoryProvider.getAll()) {
builder.registerTypeAdapterFactory(factory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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

This file was deleted.

16 changes: 16 additions & 0 deletions src/test/java/com/stripe/net/ApiResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.gson.JsonIOException;
import com.stripe.exception.InvalidRequestException;
import java.net.Proxy;
import org.junit.jupiter.api.Test;

class ApiResourceTest {
Expand All @@ -23,4 +26,17 @@ public void testUrlEncodeIdThrowingOnNull() {
ApiResource.urlEncodeId(null);
});
}

static class MyClass extends ApiResource {
public Proxy proxy;
}

@Test
public void testReflectionFilter() {
JsonIOException e =
assertThrows(JsonIOException.class, () -> ApiResource.GSON.fromJson("{}", MyClass.class));

// Assert that the error message involves a ReflectionAccessFilter.
assertTrue(e.getMessage().contains("ReflectionAccessFilter"));
}
}

0 comments on commit e9fcfaa

Please sign in to comment.