Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ReflectionCheckingTypeAdapterFactory with a ReflectionAccessFilter #1496

Merged
merged 3 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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"));
}
}