Skip to content

Commit

Permalink
Code changes and tests for #2436 to throw exception when trying to re…
Browse files Browse the repository at this point in the history
…gister adapter for Object or JsonElement
  • Loading branch information
Sachin Patil committed Aug 21, 2023
1 parent 7b8ce2b commit 15a11a9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
15 changes: 11 additions & 4 deletions gson/src/main/java/com/google/gson/GsonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,11 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
@CanIgnoreReturnValue
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
Objects.requireNonNull(type);
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
$Gson$Preconditions.checkArgument((typeAdapter instanceof JsonSerializer<?>
|| typeAdapter instanceof JsonDeserializer<?>
|| typeAdapter instanceof InstanceCreator<?>
|| typeAdapter instanceof TypeAdapter<?>);
|| typeAdapter instanceof TypeAdapter<?>)
&& !isTypeObjectOrJsonElements(type));
if (typeAdapter instanceof InstanceCreator<?>) {
instanceCreators.put(type, (InstanceCreator<?>) typeAdapter);
}
Expand All @@ -687,6 +688,11 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
return this;
}

private boolean isTypeObjectOrJsonElements(Type type) {
return (type == Object.class
|| JsonElement.class.isAssignableFrom((Class<?>) type));
}

/**
* Register a factory for type adapters. Registering a factory is useful when the type
* adapter needs to be configured based on the type of the field being processed. Gson
Expand Down Expand Up @@ -723,9 +729,10 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
@CanIgnoreReturnValue
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) {
Objects.requireNonNull(baseType);
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
$Gson$Preconditions.checkArgument((typeAdapter instanceof JsonSerializer<?>
|| typeAdapter instanceof JsonDeserializer<?>
|| typeAdapter instanceof TypeAdapter<?>);
|| typeAdapter instanceof TypeAdapter<?>)
&& !JsonElement.class.isAssignableFrom(baseType));
if (typeAdapter instanceof JsonDeserializer || typeAdapter instanceof JsonSerializer) {
hierarchyFactories.add(TreeTypeAdapter.newTypeHierarchyFactory(baseType, typeAdapter));
}
Expand Down
32 changes: 31 additions & 1 deletion gson/src/test/java/com/google/gson/GsonBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.gson;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

import com.google.gson.stream.JsonReader;
Expand Down Expand Up @@ -183,7 +184,8 @@ public void testRegisterTypeAdapterForCoreType() {
String.class,
};
for (Type type : types) {
new GsonBuilder().registerTypeAdapter(type, NULL_TYPE_ADAPTER);
GsonBuilder gsonBuilder = new GsonBuilder();
assertThat(gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)).isEqualTo(gsonBuilder);
}
}

Expand Down Expand Up @@ -254,4 +256,32 @@ public void testSetStrictness() throws IOException {
assertThat(gson.newJsonReader(new StringReader("{}")).getStrictness()).isEqualTo(STRICTNESS);
assertThat(gson.newJsonWriter(new StringWriter()).getStrictness()).isEqualTo(STRICTNESS);
}

@Test
public void testRegisterTypeAdapterForObjectAndJsonElements() {
Type[] types = {
Object.class,
JsonElement.class,
JsonArray.class,
};
GsonBuilder gsonBuilder = new GsonBuilder();
for (Type type : types) {
assertThrows(IllegalArgumentException.class, () -> gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER));
}
}


@Test
public void testRegisterTypeHierarchyAdapterJsonElements() {
Type[] types = {
JsonElement.class,
JsonArray.class,
};
GsonBuilder gsonBuilder = new GsonBuilder();
for (Type type : types) {
assertThrows(IllegalArgumentException.class,
() -> gsonBuilder.registerTypeHierarchyAdapter((Class<?>) type, NULL_TYPE_ADAPTER));
}
assertThat(gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER)).isEqualTo(gsonBuilder);
}
}
2 changes: 1 addition & 1 deletion gson/src/test/java/com/google/gson/GsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testClonedTypeAdapterFactoryListsAreIndependent() {
Collections.<ReflectionAccessFilter>emptyList());

Gson clone = original.newBuilder()
.registerTypeAdapter(Object.class, new TestTypeAdapter())
.registerTypeAdapter(int.class, new TestTypeAdapter())
.create();

assertThat(clone.factories).hasSize(original.factories.size() + 1);
Expand Down

0 comments on commit 15a11a9

Please sign in to comment.