Skip to content

Commit

Permalink
Support Properties subclasses in GsonTypes.getMapKeyAndValueTypes (#2758
Browse files Browse the repository at this point in the history
)
  • Loading branch information
panic08 authored Oct 12, 2024
1 parent ec5a5e4 commit 7e98ebd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gson/src/main/java/com/google/gson/internal/$Gson$Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ public static Type[] getMapKeyAndValueTypes(Type context, Class<?> contextRawTyp
* class should extend Hashtable<String, String>, but it's declared to
* extend Hashtable<Object, Object>.
*/
if (context == Properties.class) {
return new Type[] {String.class, String.class}; // TODO: test subclasses of Properties!
if (Properties.class.isAssignableFrom(contextRawType)) {
return new Type[] {String.class, String.class};
}

Type mapType = getSupertype(context, contextRawType, Map.class);
Expand Down
14 changes: 14 additions & 0 deletions gson/src/test/java/com/google/gson/internal/GsonTypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Properties;
import org.junit.Test;

@SuppressWarnings("ClassNamedLikeTypeParameter") // for dummy classes A, B, ...
Expand Down Expand Up @@ -133,4 +134,17 @@ public <T> T method() {
return null;
}
}

@Test
public void testGetMapKeyAndValueTypesForPropertiesSubclass() throws Exception {
class CustomProperties extends Properties {
private static final long serialVersionUID = 4112578634029874840L;
}

Type[] types =
$Gson$Types.getMapKeyAndValueTypes(CustomProperties.class, CustomProperties.class);

assertThat(types[0]).isEqualTo(String.class);
assertThat(types[1]).isEqualTo(String.class);
}
}

0 comments on commit 7e98ebd

Please sign in to comment.