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

EnumMap cannot deserialize with type inclusion as property #1932

Closed
earmenda opened this issue Feb 12, 2018 · 2 comments
Closed

EnumMap cannot deserialize with type inclusion as property #1932

earmenda opened this issue Feb 12, 2018 · 2 comments
Milestone

Comments

@earmenda
Copy link

Hi,

I've seen many issues related to Enum keys but it looks like using an EnumMap with property inclusion has some issues.

Version 2.8.11

When using As.WRAPPER_ARRAY as the inclusion I am able to deserialize the Map, but using JsonTypeInfo.As.PROPERTY it throws an exception while deserializing. Here is the code to reproduce and the exception:

import java.util.EnumMap;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;

public class JacksonIssueTest {

    public static class EventTest {

        private EnumMap<EnumMapTestEnum, String> enumMapTest;

        public EventTest() {
        }

        public EventTest(EnumMap<EnumMapTestEnum, String> enumMapTest) {
            this.enumMapTest = enumMapTest;
        }

        public EnumMap<EnumMapTestEnum, String> getEnumMapTest() {
            return enumMapTest;
        }

        public void setEnumMapTest(EnumMap<EnumMapTestEnum, String> enumMapTest) {
            this.enumMapTest = enumMapTest;
        }
    }

    public enum EnumMapTestEnum {
        A, B, C;
    }

    public static void main(String args[]) throws Exception {

        EnumMap<EnumMapTestEnum, String> enumMap = new EnumMap(EnumMapTestEnum.class);
        enumMap.put(EnumMapTestEnum.A, "Test");
        EventTest eventTest = new EventTest(enumMap);

        /*
         * Working
         */
        ObjectMapper objectMapperAsArrayWrapper = new ObjectMapper();
        TypeResolverBuilder<?> mapTyperAsArrayWrapper = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL);
        mapTyperAsArrayWrapper.init(JsonTypeInfo.Id.CLASS, null);
        mapTyperAsArrayWrapper.inclusion(JsonTypeInfo.As.WRAPPER_ARRAY);
        objectMapperAsArrayWrapper.setDefaultTyping(mapTyperAsArrayWrapper);
        String jsonAsArrayWrapper = objectMapperAsArrayWrapper.writeValueAsString(eventTest);
        objectMapperAsArrayWrapper.readValue(jsonAsArrayWrapper, EventTest.class);

       /*
        * Throws Exception
        */
        ObjectMapper objectMapperAsPropertyType = new ObjectMapper();
        TypeResolverBuilder<?> mapTyperAsPropertyType = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL);
        mapTyperAsPropertyType.init(JsonTypeInfo.Id.CLASS, null);
        mapTyperAsPropertyType.inclusion(JsonTypeInfo.As.PROPERTY);
        objectMapperAsPropertyType.setDefaultTyping(mapTyperAsPropertyType);
        String jsonAsPropertyType = objectMapperAsPropertyType.writeValueAsString(eventTest);
        objectMapperAsPropertyType.readValue(jsonAsPropertyType, EventTest.class);

    }
}
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.EnumMap out of FIELD_NAME token
 at [Source: {"@class":"JacksonIssueTest$EventTest","enumMapTest":{"@class":"java.util.EnumMap","0":"Test"}}; line: 1, column: 135] (through reference chain: JacksonIssueTest$EventTest["enumMapTest"])
	at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
	at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1247)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromEmpty(StdDeserializer.java:892)
	at com.fasterxml.jackson.databind.deser.std.EnumMapDeserializer.deserialize(EnumMapDeserializer.java:130)
	at com.fasterxml.jackson.databind.deser.std.EnumMapDeserializer.deserialize(EnumMapDeserializer.java:17)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:129)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:97)
	at com.fasterxml.jackson.databind.deser.std.EnumMapDeserializer.deserializeWithType(EnumMapDeserializer.java:182)
	at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:502)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:104)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:178)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:150)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:129)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:97)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1082)
	at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:63)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
	at JacksonIssueTest.main(JacksonIssueTest.java:62)

Running into this using the default codec provided by Redisson(redis java client).

https://github.com/redisson/redisson/blob/581a7ac60f665e1882b0e351bf6c20186e59313c/redisson/src/main/java/org/redisson/codec/JsonJacksonCodec.java#L142

@cowtowncoder
Copy link
Member

Ok, I can reproduce with minor modifications for 2.9.5 (fix unlikely for 2.8 since that branch is closed).

@cowtowncoder cowtowncoder added this to the 2.9.5 milestone Mar 6, 2018
@cowtowncoder cowtowncoder changed the title EnumMap cannot deserialize with type inclusion as property EnumMap cannot deserialize with type inclusion as property Mar 6, 2018
@cowtowncoder
Copy link
Member

Thank you for reporting this -- it turned out to be an edge, not covered by unit test formerly.

Fix is in 2.9 (for 2.9.5) and master (for later 3.0.0). 2.8 branch is closed at this point, and no full release of all components is planned.

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