Skip to content

Jackson2HashMapper fails to deserialize enum property when flatten=true #2979

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
jpyoder opened this issue Aug 27, 2024 · 1 comment
Closed
Assignees
Labels
type: bug A general bug

Comments

@jpyoder
Copy link

jpyoder commented Aug 27, 2024

Prior to version 3.0.0 of spring-data-redis, Jackson2HashMapper serialized and deserialized enum properties (with flatten=true) without a problem. From 3.0.0 and up, deserialization from the hash fails if flatten=true.

A simple test proves this out - see demo.zip for a full example. Basically, for this code, the call to fromHash will fail with an exception.

class Wrapper {
    //Getters and setters omitted for brevity
    private EnumVal val;
}

enum EnumVal {
    FOO;
}
...

Wrapper wrapper = new Wrapper();
wrapper.setVal(EnumVal.FOO);
final Jackson2HashMapper flatteningMapper = new Jackson2HashMapper(true);
var result = flatteningMapper.toHash(wrapper);
var flattenedResult = flatteningMapper.fromHash(result);

The stack trace is:

Unexpected token (null), expected START_ARRAY: need Array value to contain `As.WRAPPER_ARRAY` type information for class com.example.demo.DemoApplicationTests$EnumVal
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 23] (through reference chain: com.example.demo.DemoApplicationTests$Wrapper["val"])
org.springframework.data.mapping.MappingException: Unexpected token (null), expected START_ARRAY: need Array value to contain `As.WRAPPER_ARRAY` type information for class com.example.demo.DemoApplicationTests$EnumVal
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 23] (through reference chain: com.example.demo.DemoApplicationTests$Wrapper["val"])
	at org.springframework.data.redis.hash.Jackson2HashMapper.fromHash(Jackson2HashMapper.java:268)
	at com.example.demo.DemoApplicationTests.testDeserialization(DemoApplicationTests.java:49)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (null), expected START_ARRAY: need Array value to contain `As.WRAPPER_ARRAY` type information for class com.example.demo.DemoApplicationTests$EnumVal
 at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 23] (through reference chain: com.example.demo.DemoApplicationTests$Wrapper["val"])
	at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
	at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:1913)
	at com.fasterxml.jackson.databind.DeserializationContext.reportWrongTokenException(DeserializationContext.java:1699)
	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._locateTypeId(AsArrayTypeDeserializer.java:141)
	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:96)
	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromScalar(AsArrayTypeDeserializer.java:66)
	at com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer.deserializeWithType(StdScalarDeserializer.java:66)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:138)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:310)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:215)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:170)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:136)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromAny(AsPropertyTypeDeserializer.java:240)
	at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializerNR.deserializeWithType(UntypedObjectDeserializerNR.java:112)
	at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:74)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
	at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2125)
	at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1603)
	at org.springframework.data.redis.hash.Jackson2HashMapper.fromHash(Jackson2HashMapper.java:260)
	... 4 more
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 27, 2024
@christophstrobl
Copy link
Member

Thank you @jpyoder - we'll have a look.

@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 28, 2024
@mp911de mp911de added this to the 3.2.10 (2023.1.10) milestone Aug 30, 2024
mp911de pushed a commit that referenced this issue Aug 30, 2024
This commit makes sure the mapper does not attempt to read type alias when deserializing enums.

Original pull request: #2980
Closes #2979
mp911de pushed a commit that referenced this issue Aug 30, 2024
This commit makes sure the mapper does not attempt to read type alias when deserializing enums.

Original pull request: #2980
Closes #2979
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: bug A general bug
Projects
None yet
4 participants