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

Another approach to fix 2563 #2573

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,16 @@ public Gson() {

// built-in type adapters that cannot be overridden
factories.add(TypeAdapters.JSON_ELEMENT_FACTORY);
factories.add(ObjectTypeAdapter.getFactory(objectToNumberStrategy));
factories.add(ObjectTypeAdapter.getFactory(objectToNumberStrategy, true));

// the excluder must precede all adapters that handle user-defined types
factories.add(excluder);

// users' type adapters
factories.addAll(factoriesToBeAdded);

factories.add(ObjectTypeAdapter.getFactory(objectToNumberStrategy, false));

// type adapters for basic platform types
factories.add(TypeAdapters.STRING_FACTORY);
factories.add(TypeAdapters.INTEGER_FACTORY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
Expand All @@ -39,7 +41,7 @@
*/
public final class ObjectTypeAdapter extends TypeAdapter<Object> {
/** Gson default factory using {@link ToNumberPolicy#DOUBLE}. */
private static final TypeAdapterFactory DOUBLE_FACTORY = newFactory(ToNumberPolicy.DOUBLE);
private static final TypeAdapterFactory DOUBLE_FACTORY = newFactory(ToNumberPolicy.DOUBLE, true);

private final Gson gson;
private final ToNumberStrategy toNumberStrategy;
Expand All @@ -49,24 +51,41 @@ private ObjectTypeAdapter(Gson gson, ToNumberStrategy toNumberStrategy) {
this.toNumberStrategy = toNumberStrategy;
}

private static TypeAdapterFactory newFactory(final ToNumberStrategy toNumberStrategy) {
private static TypeAdapterFactory newFactory(
final ToNumberStrategy toNumberStrategy, final boolean skipTypeVariable) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (type.getRawType() == Object.class) {
if (type.getRawType() == Object.class
&& (!skipTypeVariable || !isTypeVariableWithBound(type.getType()))) {
return (TypeAdapter<T>) new ObjectTypeAdapter(gson, toNumberStrategy);
}
return null;
}

private boolean isTypeVariableWithBound(Type type) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rewrite this method as

private boolean isTypeVariableWithBound(Type type) {
  return type instanceof TypeVariable<?> && 
         type != Object.class && 
         Arrays.stream(((TypeVariable<?>) type).getBounds()).anyMatch(Class.class::isInstance);
}

if (type instanceof TypeVariable<?>) {
TypeVariable<?> tv = (TypeVariable<?>) type;
Type bound = tv.getBounds()[0];
return bound != Object.class && bound instanceof Class<?>;
} else {
return false;
}
}
};
}

public static TypeAdapterFactory getFactory(ToNumberStrategy toNumberStrategy) {
return getFactory(toNumberStrategy, false);
}

public static TypeAdapterFactory getFactory(
ToNumberStrategy toNumberStrategy, boolean skipTypeVariable) {
if (toNumberStrategy == ToNumberPolicy.DOUBLE) {
return DOUBLE_FACTORY;
} else {
Comment on lines 85 to 87
Copy link
Collaborator

@Marcono1234 Marcono1234 Dec 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? DOUBLE_FACTORY is created with skipTypeVariable = true, but here the skipTypeVariable argument of this method is not checked, so it might actually be false.

return newFactory(toNumberStrategy);
return newFactory(toNumberStrategy, skipTypeVariable);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.google.gson.functional;

import static com.google.common.truth.Truth.assertThat;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import org.junit.Before;
import org.junit.Test;

/**
* Test deserialization of generic wrapper with type bound.
*
* @author sevcenko
*/
public class InferenceFromTypeVariableTest {
private Gson gson;

@Before
public void setUp() throws Exception {
gson = new GsonBuilder().registerTypeAdapterFactory(new ResolveGenericBoundFactory()).create();
}

public static class Foo {
private final String text;

public Foo(String text) {
this.text = text;
}

public String getText() {
return text;
}
}

public static class BarDynamic<T extends Foo> {
private final T foo;

public BarDynamic(T foo) {
this.foo = foo;
}

public T getFoo() {
return foo;
}
}

static class ResolveGenericBoundFactory implements TypeAdapterFactory {

@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (type.getType() instanceof TypeVariable<?>) {
TypeVariable<?> tv = (TypeVariable<?>) type.getType();
Type[] bounds = tv.getBounds();
if (bounds.length == 1 && bounds[0] != Object.class) {
Type bound = bounds[0];
return (TypeAdapter<T>) gson.getAdapter(TypeToken.get(bound));
}
}
return null;
}
}

@Test
public void testSubClassSerialization() {
BarDynamic<Foo> bar = new BarDynamic<>(new Foo("foo!"));
assertThat(gson.toJson(bar)).isEqualTo("{\"foo\":{\"text\":\"foo!\"}}");
// without #2563 fix, this would deserialize foo as Object and fails to assign it to foo field
BarDynamic<?> deserialized = gson.fromJson(gson.toJson(bar), BarDynamic.class);
assertThat(deserialized.getFoo().getText()).isEqualTo("foo!");
}
}