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

EitherSerializer / EitherDeserializer does not respect default typing #132

Open
kreed-rmn opened this issue Apr 16, 2019 · 1 comment
Open

Comments

@kreed-rmn
Copy link

kreed-rmn commented Apr 16, 2019

If an object mapper is configured with default typing, for example,

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

a client cannot deserialize json without providing the explicit Either.class class type to the mapper's readValue method. In other words, the serialization mechanism is not including the type information if this is enabled.

A simple example, like so fails:

@Test
public void enabledDefaultTyping()
        throws IOException {
    ObjectMapper mapper = mapper();
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

    Either<String, Integer> right = Either.right(1);
    String json = mapper().writer().writeValueAsString(right);

    System.out.println(json);

    // explicitly use Object.class here to have the mapper utilize
    // the default typing information included in serialization
    Either<String, Integer> restored = (Either<String, Integer>) mapper().readValue(json, Object.class);
    Assert.assertTrue(restored.isRight());
    Assert.assertEquals((long) restored.get(), 1);
}

with

java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class io.vavr.control.Either (java.util.ArrayList is in module java.base of loader 'bootstrap'; io.vavr.control.Either is in unnamed module of loader 'app')

More complicated types can change this message.

The serialized value for the above code is ["right",1] which does not include the type information.

I'm currently working on a fix for this, but wanted to open this issue to bring it up. The context for this is that I'm using the GenericJackson2JsonRedisSerializer from spring-data-redis to serialize results to Redis using the @Cacheable mechanism from Spring. Some of the methods we cache return Either<.,.> and we'd like to be able to cache the results.

@freerider7777
Copy link

Seems still not working

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