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

Deserialization with generic types not working since 2.7.0 #1210

Closed
a-k-g opened this issue Apr 20, 2016 · 4 comments
Closed

Deserialization with generic types not working since 2.7.0 #1210

a-k-g opened this issue Apr 20, 2016 · 4 comments

Comments

@a-k-g
Copy link

a-k-g commented Apr 20, 2016

package jackson.fail;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

import java.io.IOException;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

public class GenericWrapperTest {

    @JsonSerialize(as = GenericWrapperImpl.class)
    @JsonDeserialize(as = GenericWrapperImpl.class)
    public interface GenericWrapper<T> {
        T getValue();
    }

    public static class GenericWrapperImpl<T> implements GenericWrapper<T> {
        private final T value;

        @JsonCreator
        public GenericWrapperImpl(@JsonProperty("value") T value) {
            this.value = value;
        }

        @Override
        public T getValue() {
            return value;
        }
    }

    public static class Vehicle {
        private final String name;
        private final String make;

        @JsonCreator
        public Vehicle(@JsonProperty("name") String name, @JsonProperty("make") String make) {
            this.name = name;
            this.make = make;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            Vehicle vehicle = (Vehicle) o;

            if (!name.equals(vehicle.name)) {
                return false;
            }
            return make.equals(vehicle.make);

        }

        @Override
        public int hashCode() {
            int result = name.hashCode();
            result = 31 * result + make.hashCode();
            return result;
        }
    }

    @Test
    public void deserialization() throws IOException {
        Vehicle expected = new Vehicle("Civic", "Honda");
        String json = "{\"value\": { \"name\" : \"Civic\", \"make\" : \"Honda\"}}";
        ObjectMapper mapper = new ObjectMapper();
        final GenericWrapper<Vehicle> deserialized =
                mapper.readValue(json, new TypeReference<GenericWrapper<Vehicle>>() {});
        assertThat(deserialized.getValue(), is(expected));
    }
}

This test fails with 2.7.0+ and works with 2.6.6. The problem seems similar to #921, but isn't the same.

@a-k-g
Copy link
Author

a-k-g commented Apr 20, 2016

Mentioning the actual error seen in 2.7 for searchability of this issue - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to

@cowtowncoder
Copy link
Member

This could be something related to #1186, which was fixed for 2.7.4. So 2.7.4-SNAPSHOT may have a fix. The problem there was the way type specialization works; in this case that'd be for @JsonDeserialize(as=....).

Note, too, that @JsonSerialize(as=...) is unnecessary here and probably should not be used, unless I am missing something.

@a-k-g
Copy link
Author

a-k-g commented Apr 21, 2016

Yeah, looks like 2.7.4 might fix it. Thanks for the quick response! Anywhere I can check when 2.7.4 is scheduled to be released?

Yes, you're right @JsonSerialize(as=...) is unnecessary here.

@cowtowncoder
Copy link
Member

@a-k-g we are following "when it's ready" model, not date-driven, so there's no hard deadline.
But my current hope is to release this over the next weekend. Main remaining issues are with Scala module; when those are complete it's time for 2.7.4.
So I would say by end of April 2016.

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