From 15a11a9a61f3c9ae581aaa9fbd41d4b00bf685c6 Mon Sep 17 00:00:00 2001 From: Sachin Patil Date: Mon, 21 Aug 2023 12:31:30 -0500 Subject: [PATCH 1/9] Code changes and tests for #2436 to throw exception when trying to register adapter for Object or JsonElement --- .../java/com/google/gson/GsonBuilder.java | 15 ++++++--- .../java/com/google/gson/GsonBuilderTest.java | 32 ++++++++++++++++++- .../test/java/com/google/gson/GsonTest.java | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 68eb7d718b..f0c346f834 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -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); } @@ -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 @@ -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)); } diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index 0dfa92b790..48b43aaf6c 100644 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -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; @@ -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); } } @@ -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); + } } diff --git a/gson/src/test/java/com/google/gson/GsonTest.java b/gson/src/test/java/com/google/gson/GsonTest.java index a8e8e88b98..22932a55fd 100644 --- a/gson/src/test/java/com/google/gson/GsonTest.java +++ b/gson/src/test/java/com/google/gson/GsonTest.java @@ -93,7 +93,7 @@ public void testClonedTypeAdapterFactoryListsAreIndependent() { Collections.emptyList()); Gson clone = original.newBuilder() - .registerTypeAdapter(Object.class, new TestTypeAdapter()) + .registerTypeAdapter(int.class, new TestTypeAdapter()) .create(); assertThat(clone.factories).hasSize(original.factories.size() + 1); From 02185489561d6c9ba273da03491355721ff93b20 Mon Sep 17 00:00:00 2001 From: Sachin Patil Date: Fri, 25 Aug 2023 14:15:11 -0500 Subject: [PATCH 2/9] #2436 - Updates to User guide & comments to indicate exception cases and fix for 7 test cases of Parameterized Type --- UserGuide.md | 4 +++- gson/src/main/java/com/google/gson/GsonBuilder.java | 11 ++++++++--- .../test/java/com/google/gson/GsonBuilderTest.java | 5 ++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/UserGuide.md b/UserGuide.md index f519865434..3a42653bdb 100644 --- a/UserGuide.md +++ b/UserGuide.md @@ -405,7 +405,9 @@ gson.registerTypeAdapter(MyType.class, new MyDeserializer()); gson.registerTypeAdapter(MyType.class, new MyInstanceCreator()); ``` -`registerTypeAdapter` call checks if the type adapter implements more than one of these interfaces and register it for all of them. +`registerTypeAdapter` call checks +1. if the type adapter implements more than one of these interfaces and register it for all of them. +2. if the type adapter is for Object class or (JsonElements or any of its subclasses), it throws IllegalArgumentException. #### Writing a Serializer diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index f0c346f834..ccd37ba2b0 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -41,6 +41,8 @@ import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; + +import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.text.DateFormat; import java.util.ArrayDeque; @@ -337,7 +339,7 @@ public GsonBuilder enableComplexMapKeySerialization() { * *

In general using inner classes with Gson should be avoided; they should be converted to {@code static} * nested classes if possible. - * + *Ø * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.3 */ @@ -664,6 +666,7 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) { * @param typeAdapter This object must implement at least one of the {@link TypeAdapter}, * {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern + * @throws IllegalArgumentException if the Type adapter being registered is for Object class or (JsonElements or any of its subclasses) */ @CanIgnoreReturnValue public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { @@ -689,8 +692,9 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { } private boolean isTypeObjectOrJsonElements(Type type) { - return (type == Object.class - || JsonElement.class.isAssignableFrom((Class) type)); + return (!(type instanceof ParameterizedType) && + (type == Object.class + || JsonElement.class.isAssignableFrom((Class) type))); } /** @@ -724,6 +728,7 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) { * @param typeAdapter This object must implement at least one of {@link TypeAdapter}, * {@link JsonSerializer} or {@link JsonDeserializer} interfaces. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern + * @throws IllegalArgumentException if the Type adapter being registered is for a JsonElements or any of its subclasses * @since 1.7 */ @CanIgnoreReturnValue diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index 48b43aaf6c..2a50598b31 100644 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -184,8 +184,7 @@ public void testRegisterTypeAdapterForCoreType() { String.class, }; for (Type type : types) { - GsonBuilder gsonBuilder = new GsonBuilder(); - assertThat(gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)).isEqualTo(gsonBuilder); + new GsonBuilder().registerTypeAdapter(type, NULL_TYPE_ADAPTER); } } @@ -282,6 +281,6 @@ public void testRegisterTypeHierarchyAdapterJsonElements() { assertThrows(IllegalArgumentException.class, () -> gsonBuilder.registerTypeHierarchyAdapter((Class) type, NULL_TYPE_ADAPTER)); } - assertThat(gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER)).isEqualTo(gsonBuilder); + gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER); } } From f96af9ba2891aa48e798300afc21242975037891 Mon Sep 17 00:00:00 2001 From: Sachin Patil Date: Mon, 28 Aug 2023 16:24:18 -0500 Subject: [PATCH 3/9] #2436 - Fixes as per the review comments. --- UserGuide.md | 4 ++-- gson/src/main/java/com/google/gson/GsonBuilder.java | 8 +++----- gson/src/test/java/com/google/gson/GsonBuilderTest.java | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/UserGuide.md b/UserGuide.md index 3a42653bdb..05fe424ef7 100644 --- a/UserGuide.md +++ b/UserGuide.md @@ -406,8 +406,8 @@ gson.registerTypeAdapter(MyType.class, new MyInstanceCreator()); ``` `registerTypeAdapter` call checks -1. if the type adapter implements more than one of these interfaces and register it for all of them. -2. if the type adapter is for Object class or (JsonElements or any of its subclasses), it throws IllegalArgumentException. +1. if the type adapter implements more than one of these interfaces, in that case it registers the adapter for all of them. +2. if the type adapter is for the Object class or JsonElement or any of its subclasses, in that case it throws IllegalArgumentException because overriding the built-in adapters for these types is not supported. #### Writing a Serializer diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index ccd37ba2b0..92763ac451 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -41,7 +41,6 @@ import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; - import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.text.DateFormat; @@ -339,7 +338,6 @@ public GsonBuilder enableComplexMapKeySerialization() { * *

In general using inner classes with Gson should be avoided; they should be converted to {@code static} * nested classes if possible. - *Ø * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.3 */ @@ -666,7 +664,7 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) { * @param typeAdapter This object must implement at least one of the {@link TypeAdapter}, * {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern - * @throws IllegalArgumentException if the Type adapter being registered is for Object class or (JsonElements or any of its subclasses) + * @throws IllegalArgumentException if the Type adapter being registered is for {@code Object} class or {@link JsonElement} or any of its subclasses */ @CanIgnoreReturnValue public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { @@ -675,7 +673,7 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { || typeAdapter instanceof JsonDeserializer || typeAdapter instanceof InstanceCreator || typeAdapter instanceof TypeAdapter) - && !isTypeObjectOrJsonElements(type)); + && !isTypeObjectOrJsonElement(type)); if (typeAdapter instanceof InstanceCreator) { instanceCreators.put(type, (InstanceCreator) typeAdapter); } @@ -691,7 +689,7 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { return this; } - private boolean isTypeObjectOrJsonElements(Type type) { + private boolean isTypeObjectOrJsonElement(Type type) { return (!(type instanceof ParameterizedType) && (type == Object.class || JsonElement.class.isAssignableFrom((Class) type))); diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index 2a50598b31..46655f86ff 100644 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -265,7 +265,7 @@ public void testRegisterTypeAdapterForObjectAndJsonElements() { }; GsonBuilder gsonBuilder = new GsonBuilder(); for (Type type : types) { - assertThrows(IllegalArgumentException.class, () -> gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)); + assertThrows(IllegalArgumentException.class, () -> gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)); } } @@ -279,7 +279,7 @@ public void testRegisterTypeHierarchyAdapterJsonElements() { GsonBuilder gsonBuilder = new GsonBuilder(); for (Type type : types) { assertThrows(IllegalArgumentException.class, - () -> gsonBuilder.registerTypeHierarchyAdapter((Class) type, NULL_TYPE_ADAPTER)); + () -> gsonBuilder.registerTypeHierarchyAdapter((Class) type, NULL_TYPE_ADAPTER)); } gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER); } From d9f42d837c285973dc9cc9cb861d6bfc1c531467 Mon Sep 17 00:00:00 2001 From: Sachin Patil Date: Sat, 16 Sep 2023 16:50:32 -0500 Subject: [PATCH 4/9] #2436 - Refactored as per latest review comments + throwing error message. --- .../java/com/google/gson/GsonBuilder.java | 27 ++++++++++++------- .../java/com/google/gson/GsonBuilderTest.java | 9 +++++-- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 92763ac451..40fcc40655 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -338,6 +338,7 @@ public GsonBuilder enableComplexMapKeySerialization() { * *

In general using inner classes with Gson should be avoided; they should be converted to {@code static} * nested classes if possible. + * * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern * @since 1.3 */ @@ -664,16 +665,20 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) { * @param typeAdapter This object must implement at least one of the {@link TypeAdapter}, * {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern - * @throws IllegalArgumentException if the Type adapter being registered is for {@code Object} class or {@link JsonElement} or any of its subclasses + * @throws IllegalArgumentException if the type adapter being registered is for {@code Object} class or {@link JsonElement} or any of its subclasses */ @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) - && !isTypeObjectOrJsonElement(type)); + || typeAdapter instanceof TypeAdapter); + + if(isTypeObjectOrJsonElement(type)){ + throw new IllegalArgumentException("Cannot override built-in adapter for " + type); + } + if (typeAdapter instanceof InstanceCreator) { instanceCreators.put(type, (InstanceCreator) typeAdapter); } @@ -692,7 +697,7 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { private boolean isTypeObjectOrJsonElement(Type type) { return (!(type instanceof ParameterizedType) && (type == Object.class - || JsonElement.class.isAssignableFrom((Class) type))); + || (type instanceof Class && JsonElement.class.isAssignableFrom((Class) type)))); } /** @@ -726,16 +731,20 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) { * @param typeAdapter This object must implement at least one of {@link TypeAdapter}, * {@link JsonSerializer} or {@link JsonDeserializer} interfaces. * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern - * @throws IllegalArgumentException if the Type adapter being registered is for a JsonElements or any of its subclasses + * @throws IllegalArgumentException if the type adapter being registered is for {@link JsonElement} or any of its subclasses * @since 1.7 */ @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) - && !JsonElement.class.isAssignableFrom(baseType)); + || typeAdapter instanceof TypeAdapter); + + if(JsonElement.class.isAssignableFrom(baseType)){ + throw new IllegalArgumentException("Cannot override built-in adapter for " + baseType); + } + if (typeAdapter instanceof JsonDeserializer || typeAdapter instanceof JsonSerializer) { hierarchyFactories.add(TreeTypeAdapter.newTypeHierarchyFactory(baseType, typeAdapter)); } diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index 46655f86ff..14991ca8ca 100644 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -258,6 +258,7 @@ public void testSetStrictness() throws IOException { @Test public void testRegisterTypeAdapterForObjectAndJsonElements() { + final String ERROR_MESSAGE = "Cannot override built-in adapter for "; Type[] types = { Object.class, JsonElement.class, @@ -265,21 +266,25 @@ public void testRegisterTypeAdapterForObjectAndJsonElements() { }; GsonBuilder gsonBuilder = new GsonBuilder(); for (Type type : types) { - assertThrows(IllegalArgumentException.class, () -> gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)); + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)); + assertThat(e).hasMessageThat().isEqualTo(ERROR_MESSAGE + type); } } @Test public void testRegisterTypeHierarchyAdapterJsonElements() { + final String ERROR_MESSAGE = "Cannot override built-in adapter for "; Type[] types = { JsonElement.class, JsonArray.class, }; GsonBuilder gsonBuilder = new GsonBuilder(); for (Type type : types) { - assertThrows(IllegalArgumentException.class, + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> gsonBuilder.registerTypeHierarchyAdapter((Class) type, NULL_TYPE_ADAPTER)); + assertThat(e).hasMessageThat().isEqualTo(ERROR_MESSAGE + type); } gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER); } From 5c0ea740eba22ef3d94ee37731c1a1f041be7f60 Mon Sep 17 00:00:00 2001 From: Sachin Patil Date: Sat, 16 Sep 2023 16:55:24 -0500 Subject: [PATCH 5/9] #2436 - added a clarifying comment in a positive test case. --- gson/src/test/java/com/google/gson/GsonBuilderTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index 14991ca8ca..a81c766437 100644 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -286,6 +286,7 @@ public void testRegisterTypeHierarchyAdapterJsonElements() { () -> gsonBuilder.registerTypeHierarchyAdapter((Class) type, NULL_TYPE_ADAPTER)); assertThat(e).hasMessageThat().isEqualTo(ERROR_MESSAGE + type); } + // But registering type hierarchy adapter for Object should be allowed gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER); } } From 263a91894be56bd1c0b02e7bddc5e68faeabee38 Mon Sep 17 00:00:00 2001 From: Sachin Patil Date: Sun, 24 Sep 2023 16:33:37 -0500 Subject: [PATCH 6/9] #2436 - formatting and minor changes as per review. --- gson/src/main/java/com/google/gson/GsonBuilder.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 40fcc40655..21ff96df61 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -675,7 +675,7 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { || typeAdapter instanceof InstanceCreator || typeAdapter instanceof TypeAdapter); - if(isTypeObjectOrJsonElement(type)){ + if (isTypeObjectOrJsonElement(type)){ throw new IllegalArgumentException("Cannot override built-in adapter for " + type); } @@ -695,9 +695,9 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) { } private boolean isTypeObjectOrJsonElement(Type type) { - return (!(type instanceof ParameterizedType) && + return type instanceof Class && (type == Object.class - || (type instanceof Class && JsonElement.class.isAssignableFrom((Class) type)))); + || JsonElement.class.isAssignableFrom((Class) type)); } /** From d853261936b0d7d0fcd6558d5d0af2b030ac07cd Mon Sep 17 00:00:00 2001 From: SP <34406014+sachinp97@users.noreply.github.com> Date: Thu, 28 Sep 2023 20:12:11 -0500 Subject: [PATCH 7/9] Update gson/src/main/java/com/google/gson/GsonBuilder.java Co-authored-by: Marcono1234 --- gson/src/main/java/com/google/gson/GsonBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 21ff96df61..8a3f273eb0 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -741,7 +741,7 @@ public GsonBuilder registerTypeHierarchyAdapter(Class baseType, Object typeAd || typeAdapter instanceof JsonDeserializer || typeAdapter instanceof TypeAdapter); - if(JsonElement.class.isAssignableFrom(baseType)){ + if (JsonElement.class.isAssignableFrom(baseType)) { throw new IllegalArgumentException("Cannot override built-in adapter for " + baseType); } From d9025c86bd834b9f0dd9f84b10ace48b98f94a3b Mon Sep 17 00:00:00 2001 From: SP <34406014+sachinp97@users.noreply.github.com> Date: Thu, 28 Sep 2023 20:12:41 -0500 Subject: [PATCH 8/9] Update gson/src/test/java/com/google/gson/GsonBuilderTest.java Co-authored-by: Marcono1234 --- gson/src/test/java/com/google/gson/GsonBuilderTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index a81c766437..d9b0192768 100644 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -260,14 +260,14 @@ public void testSetStrictness() throws IOException { public void testRegisterTypeAdapterForObjectAndJsonElements() { final String ERROR_MESSAGE = "Cannot override built-in adapter for "; Type[] types = { - Object.class, - JsonElement.class, - JsonArray.class, + Object.class, + JsonElement.class, + JsonArray.class, }; GsonBuilder gsonBuilder = new GsonBuilder(); for (Type type : types) { IllegalArgumentException e = assertThrows(IllegalArgumentException.class, - () -> gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)); + () -> gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)); assertThat(e).hasMessageThat().isEqualTo(ERROR_MESSAGE + type); } } From ba40b41d12fcbaca7acb24636970e215d7c9990b Mon Sep 17 00:00:00 2001 From: SP <34406014+sachinp97@users.noreply.github.com> Date: Thu, 28 Sep 2023 20:13:15 -0500 Subject: [PATCH 9/9] Update gson/src/test/java/com/google/gson/GsonBuilderTest.java Co-authored-by: Marcono1234 --- .../test/java/com/google/gson/GsonBuilderTest.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/gson/src/test/java/com/google/gson/GsonBuilderTest.java b/gson/src/test/java/com/google/gson/GsonBuilderTest.java index d9b0192768..278540b612 100644 --- a/gson/src/test/java/com/google/gson/GsonBuilderTest.java +++ b/gson/src/test/java/com/google/gson/GsonBuilderTest.java @@ -276,14 +276,15 @@ public void testRegisterTypeAdapterForObjectAndJsonElements() { @Test public void testRegisterTypeHierarchyAdapterJsonElements() { final String ERROR_MESSAGE = "Cannot override built-in adapter for "; - Type[] types = { - JsonElement.class, - JsonArray.class, + Class[] types = { + JsonElement.class, + JsonArray.class, }; GsonBuilder gsonBuilder = new GsonBuilder(); - for (Type type : types) { + for (Class type : types) { IllegalArgumentException e = assertThrows(IllegalArgumentException.class, - () -> gsonBuilder.registerTypeHierarchyAdapter((Class) type, NULL_TYPE_ADAPTER)); + () -> gsonBuilder.registerTypeHierarchyAdapter(type, NULL_TYPE_ADAPTER)); + assertThat(e).hasMessageThat().isEqualTo(ERROR_MESSAGE + type); } // But registering type hierarchy adapter for Object should be allowed