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

TypeAdapterRuntimeTypeWrapper prefers cyclic adapter for base type over reflective adapter for sub type #1833

Closed
Marcono1234 opened this issue Dec 2, 2020 · 0 comments · Fixed by #1787
Labels

Comments

@Marcono1234
Copy link
Collaborator

The internal class TypeAdapterRuntimeTypeWrapper is supposed to prefer custom adapters for the compile type over the reflective adapter for the runtime type. However, when the compile type and the runtime type only have a reflective adapter, then it should prefer the runtime type adapter.

The problem is that this logic is not working for classes with cyclic dependencies which therefore have a Gson$FutureTypeAdapter wrapping a reflective adapter because the following line does not consider this:

} else if (!(delegate instanceof ReflectiveTypeAdapterFactory.Adapter)) {

For example:

class Base {
  public Base f;
}

class Sub extends Base {
  public int i;

  public Sub(int i) {
    this.i = i;
  }
}
Base b = new Base();
b.f = new Sub(2);
String json = new Gson().toJson(b);
// Fails because reflective adapter for base class is used, therefore json is: {"f":{}}
assertEquals("{\"f\":{\"i\":2}}", json);

Note: This is similar to the problem #1787 tries to fix for TreeTypeAdapter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant