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

$Gson$Types.resolve() infinite recursion fix introduces insufficient recursion problem #1876

Closed
sgo-acerta opened this issue Mar 23, 2021 · 2 comments

Comments

@sgo-acerta
Copy link

See the junit test below.
This test will pass on 2.8.1 but fails since 2.8.2

import com.google.gson.*;
import org.junit.Test;

import java.lang.reflect.Type;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class SandboxTest {
    Gson gson = new GsonBuilder().registerTypeAdapter(CustomReason.class, new TestAdapter()).create();

    @Test
    public void reason_can_not_be_parsed() {
        CustomRoot result = gson.fromJson("{\"feedbackList\": [{\"reason\": \"my custom reason\"}]}", CustomRoot.class);
        assertEquals("my custom reason", result.feedbackList.stream().map(it -> it.reason.value).findFirst().get());
    }

    public interface Reason {
    }

    public static class Root<R extends Reason, F extends Feedback<R>> {
        List<F> feedbackList;
    }

    public static class FeedbackRoot<R extends Reason> extends Root<R, Feedback<R>> {

    }

    public static class Feedback<R extends Reason> {
        private R reason;
    }

    public static class CustomReason implements Reason {
        private String value;

        public CustomReason(String value) {
            this.value = value;
        }
    }

    public static class CustomRoot extends FeedbackRoot<CustomReason> {
    }

    private class TestAdapter implements JsonDeserializer<CustomReason> {
        public CustomReason deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            return new CustomReason(jsonElement.getAsString());
        }
    }
}
@Marcono1234
Copy link
Collaborator

Sounds like #1391 would fix this.

@Marcono1234
Copy link
Collaborator

Cannot reproduce this with Gson 2.9.1 so I assume it was fixed indeed. Please let us know if you still experience this issue with the latest Gson versions.

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