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

Can not infer generic type arguments after upgrading to 2.8.6 #1788

Closed
hstaudacher opened this issue Sep 21, 2020 · 2 comments
Closed

Can not infer generic type arguments after upgrading to 2.8.6 #1788

hstaudacher opened this issue Sep 21, 2020 · 2 comments

Comments

@hstaudacher
Copy link

We recently updated our gson version from 2.7 to 2.8.6 and noticed some serialization test failures. The reason for this is that we have a data structure that makes extensive use of generics. On certain scenario fails were ObjectTypeAdaper always serializes NUMBER as Double.

Here is a unit test that passes with 2.7 and fails with 2.8.6

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.junit.Test;

import com.google.gson.Gson;

public class GsonTest {

	static abstract class ParentPojo<T, V> {

		List<T> list;

		V value;

		public ParentPojo(List<T> list, V value) {
			this.list = list;
			this.value = value;
		}

	}

	static class InBetweenPojo<T extends Number & Comparable<T>> extends ParentPojo<T, Optional<T>> {

		public InBetweenPojo(Optional<T> value) {
			super(new ArrayList<>(), value);
		}

	}

	static class Pojo extends InBetweenPojo<Long> {

		public Pojo(Optional<Long> value) {
			super(value);
		}
	}

	@Test
	public void canInferTypeArguments() {
		Long v = 12345678910L;

		Gson gson = new Gson();
		String json = gson.toJson(new Pojo(Optional.of(v)));
		Pojo pojo = gson.fromJson(json, Pojo.class);

		assertThat(pojo.value.get()).isInstanceOf(Long.class);
	}

}
@Marcono1234
Copy link
Collaborator

Sounds similar to #1731. This might be the same bug which has the pull request #1391 fixing it.
Also note that Gson currently has no built-in type adapter for java.util.Optional so unless you are providing your own custom one you are relying on implementation details of the JDK.

Note however that I am not a maintainer and therefore do not know when that pull request will be merged.

@Marcono1234
Copy link
Collaborator

It looks like at least the specific test case you have given works fine in Gson 2.9.1; therefore I am closing this issue. Please create a new issue in case you still experience a similar problem.

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

No branches or pull requests

2 participants