Skip to content

Commit bea01ef

Browse files
christophstroblmp911de
authored andcommitted
Fix error deserializing enum values with flattening Jackson2HashMapper.
This commit makes sure the mapper does not attempt to read type alias when deserializing enums. Original pull request: #2980 Closes #2979
1 parent 5fb0665 commit bea01ef

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public boolean useForType(JavaType type) {
177177
return false;
178178
}
179179

180-
if (flatten && type.isTypeOrSubTypeOf(Number.class)) {
180+
if (flatten && (type.isTypeOrSubTypeOf(Number.class) || type.isEnumType())) {
181181
return false;
182182
}
183183

src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperUnitTests.java

+43
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ void bigDecimalShouldBeTreatedCorrectly() {
203203
assertBackAndForwardMapping(source);
204204
}
205205

206+
@Test // GH-2979
207+
void enumsShouldBeTreatedCorrectly() {
208+
209+
WithEnumValue source = new WithEnumValue();
210+
source.value = SpringDataEnum.REDIS;
211+
212+
assertBackAndForwardMapping(source);
213+
}
214+
206215
public static class WithList {
207216

208217
List<String> strings;
@@ -452,4 +461,38 @@ public int hashCode() {
452461
return Objects.hash(getValue());
453462
}
454463
}
464+
465+
enum SpringDataEnum {
466+
COMMONS, REDIS
467+
}
468+
469+
static class WithEnumValue {
470+
471+
SpringDataEnum value;
472+
473+
public SpringDataEnum getValue() {
474+
return value;
475+
}
476+
477+
public void setValue(SpringDataEnum value) {
478+
this.value = value;
479+
}
480+
481+
@Override
482+
public boolean equals(Object o) {
483+
if (o == this) {
484+
return true;
485+
}
486+
if (o == null || getClass() != o.getClass()) {
487+
return false;
488+
}
489+
WithEnumValue that = (WithEnumValue) o;
490+
return value == that.value;
491+
}
492+
493+
@Override
494+
public int hashCode() {
495+
return Objects.hash(value);
496+
}
497+
}
455498
}

0 commit comments

Comments
 (0)