Skip to content

Commit dc5887f

Browse files
committed
Consider java.math and java.util types as simple types.
Closes #374
1 parent 42381f0 commit dc5887f

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/main/java/org/springframework/data/keyvalue/core/mapping/context/KeyValueMappingContext.java

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

18+
import java.util.Collections;
19+
1820
import org.springframework.data.keyvalue.core.mapping.BasicKeyValuePersistentEntity;
1921
import org.springframework.data.keyvalue.core.mapping.KeySpaceResolver;
2022
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
@@ -39,6 +41,10 @@ public class KeyValueMappingContext<E extends KeyValuePersistentEntity<?, P>, P
3941

4042
private @Nullable KeySpaceResolver fallbackKeySpaceResolver;
4143

44+
public KeyValueMappingContext() {
45+
setSimpleTypeHolder(new KeyValueSimpleTypeHolder());
46+
}
47+
4248
/**
4349
* Configures the {@link KeySpaceResolver} to be used if not explicit key space is annotated to the domain type.
4450
*
@@ -59,4 +65,24 @@ protected <T> E createPersistentEntity(TypeInformation<T> typeInformation) {
5965
protected P createPersistentProperty(Property property, E owner, SimpleTypeHolder simpleTypeHolder) {
6066
return (P) new KeyValuePersistentProperty<>(property, owner, simpleTypeHolder);
6167
}
68+
69+
/**
70+
* @since 2.5.1
71+
*/
72+
private static class KeyValueSimpleTypeHolder extends SimpleTypeHolder {
73+
74+
public KeyValueSimpleTypeHolder() {
75+
super(Collections.emptySet(), true);
76+
}
77+
78+
@Override
79+
public boolean isSimpleType(Class<?> type) {
80+
81+
if (type.getName().startsWith("java.math.") || type.getName().startsWith("java.util.")) {
82+
return true;
83+
}
84+
85+
return super.isSimpleType(type);
86+
}
87+
}
6288
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2021 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.keyvalue.core.mapping.context;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.math.BigDecimal;
21+
import java.math.BigInteger;
22+
import java.util.UUID;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
27+
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty;
28+
29+
/**
30+
* Unit test for {@link KeyValueMappingContext}.
31+
*
32+
* @author Mark Paluch
33+
*/
34+
class KeyValueMappingContextUnitTests<P extends KeyValuePersistentProperty<P>> {
35+
36+
@Test
37+
void shouldNotCreateEntitiesForJavaStandardTypes() {
38+
39+
KeyValueMappingContext<KeyValuePersistentEntity<?, P>, P> mappingContext = new KeyValueMappingContext<>();
40+
41+
assertThat(mappingContext.getPersistentEntity(BigInteger.class)).isNull();
42+
assertThat(mappingContext.getPersistentEntity(BigDecimal.class)).isNull();
43+
assertThat(mappingContext.getPersistentEntity(UUID.class)).isNull();
44+
}
45+
}

0 commit comments

Comments
 (0)