Skip to content

Commit 9a92d84

Browse files
christophstrobljxblum
authored andcommitted
Fix Jackson (hash) mapping for BigDecimal/BigInteger scalar values and Java Time types.
Once again, the SD Redis Jackson2HashMapper can now de/serialize Objects with BigDecimal/BigInteger values along with java.time types, such as java.time.LocalDateTime. Additionally, splits the unit tests for flattening and unflattening of the hash mapping into 2 separate test classes to be picked up in the SD Redis test suite. Resolves spring-projects#2593
1 parent 1ad01d4 commit 9a92d84

File tree

4 files changed

+75
-18
lines changed

4 files changed

+75
-18
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.fasterxml.jackson.databind.JsonDeserializer;
5555
import com.fasterxml.jackson.databind.JsonNode;
5656
import com.fasterxml.jackson.databind.JsonSerializer;
57+
import com.fasterxml.jackson.databind.MapperFeature;
5758
import com.fasterxml.jackson.databind.ObjectMapper;
5859
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
5960
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -183,6 +184,10 @@ public boolean useForType(JavaType t) {
183184
return false;
184185
}
185186

187+
if(flatten && t.isTypeOrSubTypeOf(Number.class)) {
188+
return false;
189+
}
190+
186191
if (EVERYTHING.equals(_appliesFor)) {
187192
return !TreeNode.class.isAssignableFrom(t.getRawClass());
188193
}
@@ -196,6 +201,9 @@ public boolean useForType(JavaType t) {
196201
typingMapper.activateDefaultTyping(typingMapper.getPolymorphicTypeValidator(), DefaultTyping.EVERYTHING,
197202
As.PROPERTY);
198203
typingMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
204+
if(flatten) {
205+
typingMapper.disable(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES);
206+
}
199207

200208
// Prevent splitting time types into arrays. E
201209
typingMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
@@ -240,7 +248,7 @@ public Object fromHash(Map<String, Object> hash) {
240248
Map<String, Object> unflattenedHash = doUnflatten(hash);
241249
byte[] unflattenedHashedBytes = untypedMapper.writeValueAsBytes(unflattenedHash);
242250
Object hashedObject = typingMapper.reader().forType(Object.class)
243-
.readValue(unflattenedHashedBytes);;
251+
.readValue(unflattenedHashedBytes);
244252

245253
return hashedObject;
246254
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.mapping;
17+
18+
import org.springframework.data.redis.hash.Jackson2HashMapper;
19+
20+
/**
21+
* @author Christoph Strobl
22+
* @since 2023/06
23+
*/
24+
public class Jackson2HashMapperFlatteningUnitTests extends Jackson2HashMapperUnitTests {
25+
26+
Jackson2HashMapperFlatteningUnitTests() {
27+
super(new Jackson2HashMapper(true));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.mapping;
17+
18+
import org.springframework.data.redis.hash.Jackson2HashMapper;
19+
20+
/**
21+
* @author Christoph Strobl
22+
* @since 2023/06
23+
*/
24+
public class Jackson2HashMapperNonFlatteningUnitTests extends Jackson2HashMapperUnitTests {
25+
26+
Jackson2HashMapperNonFlatteningUnitTests() {
27+
super(new Jackson2HashMapper(false));
28+
}
29+
}

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

+8-17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.redis.mapping;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
1820
import lombok.Data;
1921

2022
import java.math.BigDecimal;
@@ -28,9 +30,11 @@
2830
import java.util.LinkedHashMap;
2931
import java.util.List;
3032
import java.util.Map;
33+
import java.util.stream.Stream;
3134

35+
import org.junit.jupiter.api.DynamicNode;
3236
import org.junit.jupiter.api.Test;
33-
37+
import org.junit.jupiter.api.TestFactory;
3438
import org.springframework.data.redis.Address;
3539
import org.springframework.data.redis.Person;
3640
import org.springframework.data.redis.hash.HashMapper;
@@ -44,23 +48,10 @@
4448
*/
4549
public abstract class Jackson2HashMapperUnitTests extends AbstractHashMapperTests {
4650

47-
private final Jackson2HashMapper mapper;
48-
49-
Jackson2HashMapperUnitTests(Jackson2HashMapper mapper) {
50-
this.mapper = mapper;
51-
}
52-
53-
static class FlatteningJackson2HashMapperUnitTests extends Jackson2HashMapperUnitTests {
54-
FlatteningJackson2HashMapperUnitTests() {
55-
super(new Jackson2HashMapper(true));
56-
}
57-
}
58-
59-
static class NonFlatteningJackson2HashMapperUnitTests extends Jackson2HashMapperUnitTests {
51+
private Jackson2HashMapper mapper;
6052

61-
NonFlatteningJackson2HashMapperUnitTests() {
62-
super(new Jackson2HashMapper(false));
63-
}
53+
public Jackson2HashMapperUnitTests(Jackson2HashMapper param){
54+
this.mapper = param;
6455
}
6556

6657
@Override

0 commit comments

Comments
 (0)