Skip to content

Commit 7413a03

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1517 - Polishing.
Remove ReflectiveSimpleTypes in favor of MongoSimpleTypes. Add add integration test.
1 parent 8e3d7f9 commit 7413a03

File tree

5 files changed

+65
-63
lines changed

5 files changed

+65
-63
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public CustomConversions(List<?> converters) {
9393

9494
this.readingPairs = new LinkedHashSet<ConvertiblePair>();
9595
this.writingPairs = new LinkedHashSet<ConvertiblePair>();
96-
this.customSimpleTypes = new HashSet<Class<?>>(ReflectiveSimpleTypes.getSupportedSimpleTypes());
96+
this.customSimpleTypes = new HashSet<Class<?>>();
9797
this.customReadTargetTypes = new ConcurrentHashMap<ConvertiblePair, CacheValue<Class<?>>>();
9898
this.customWriteTargetTypes = new ConcurrentHashMap<ConvertiblePair, CacheValue<Class<?>>>();
9999
this.rawWriteTargetTypes = new ConcurrentHashMap<Class<?>, CacheValue<Class<?>>>();

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReflectiveSimpleTypes.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011 by the original author(s).
2+
* Copyright (c) 2011-2017 by the original author(s).
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,8 @@
2626
import org.bson.types.CodeWScope;
2727
import org.bson.types.ObjectId;
2828
import org.springframework.data.mapping.model.SimpleTypeHolder;
29+
import org.springframework.data.mongodb.util.MongoClientVersion;
30+
import org.springframework.util.ClassUtils;
2931

3032
import com.mongodb.DBObject;
3133
import com.mongodb.DBRef;
@@ -34,6 +36,7 @@
3436
* Simple constant holder for a {@link SimpleTypeHolder} enriched with Mongo specific simple types.
3537
*
3638
* @author Oliver Gierke
39+
* @author Christoph Strobl
3740
*/
3841
public abstract class MongoSimpleTypes {
3942

@@ -54,12 +57,17 @@ public abstract class MongoSimpleTypes {
5457
simpleTypes.add(Pattern.class);
5558
simpleTypes.add(Binary.class);
5659
simpleTypes.add(UUID.class);
60+
61+
if (MongoClientVersion.isMongo34Driver()) {
62+
simpleTypes
63+
.add(ClassUtils.resolveClassName("org.bson.types.Decimal128", MongoSimpleTypes.class.getClassLoader()));
64+
}
65+
5766
MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
5867
}
5968

6069
private static final Set<Class<?>> MONGO_SIMPLE_TYPES;
6170
public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true);
6271

63-
private MongoSimpleTypes() {
64-
}
72+
private MongoSimpleTypes() {}
6573
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/MongoClientVersion.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,16 +28,28 @@ public class MongoClientVersion {
2828

2929
private static final boolean IS_MONGO_30 = ClassUtils.isPresent("com.mongodb.binding.SingleServerBinding",
3030
MongoClientVersion.class.getClassLoader());
31+
32+
private static final boolean IS_MONGO_34 = ClassUtils.isPresent("org.bson.types.Decimal128",
33+
MongoClientVersion.class.getClassLoader());
34+
3135
private static final boolean IS_ASYNC_CLIENT = ClassUtils.isPresent("com.mongodb.async.client.MongoClient",
3236
MongoClientVersion.class.getClassLoader());
3337

3438
/**
35-
* @return |literal true} if MongoDB Java driver version 3.0 or later is on classpath.
39+
* @return {@literal true} if MongoDB Java driver version 3.0 or later is on classpath.
3640
*/
3741
public static boolean isMongo3Driver() {
3842
return IS_MONGO_30;
3943
}
4044

45+
/**
46+
* @return {@literal true} if MongoDB Java driver version 3.4 or later is on classpath.
47+
* @since 1.10
48+
*/
49+
public static boolean isMongo34Driver() {
50+
return IS_MONGO_34;
51+
}
52+
4153
/**
4254
* @return {lliteral true} if MongoDB Java driver is on classpath.
4355
*/

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
import static org.springframework.data.mongodb.core.query.Query.*;
2525
import static org.springframework.data.mongodb.core.query.Update.*;
2626

27+
import lombok.Data;
28+
import lombok.EqualsAndHashCode;
29+
import lombok.NoArgsConstructor;
30+
31+
import java.lang.reflect.InvocationTargetException;
2732
import java.math.BigDecimal;
2833
import java.math.BigInteger;
2934
import java.util.ArrayList;
@@ -82,10 +87,12 @@
8287
import org.springframework.data.mongodb.core.query.Criteria;
8388
import org.springframework.data.mongodb.core.query.Query;
8489
import org.springframework.data.mongodb.core.query.Update;
90+
import org.springframework.data.mongodb.util.MongoClientVersion;
8591
import org.springframework.data.util.CloseableIterator;
8692
import org.springframework.test.annotation.DirtiesContext;
8793
import org.springframework.test.context.ContextConfiguration;
8894
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
95+
import org.springframework.util.ClassUtils;
8996
import org.springframework.util.ObjectUtils;
9097
import org.springframework.util.StringUtils;
9198

@@ -101,10 +108,6 @@
101108
import com.mongodb.WriteConcern;
102109
import com.mongodb.WriteResult;
103110

104-
import lombok.Data;
105-
import lombok.EqualsAndHashCode;
106-
import lombok.NoArgsConstructor;
107-
108111
/**
109112
* Integration test for {@link MongoTemplate}.
110113
*
@@ -125,6 +128,9 @@ public class MongoTemplateTests {
125128
.parse("2.4");
126129
private static final org.springframework.data.util.Version TWO_DOT_EIGHT = org.springframework.data.util.Version
127130
.parse("2.8");
131+
private static final org.springframework.data.util.Version THREE_DOT_FOUR= org.springframework.data.util.Version
132+
.parse("3.4");
133+
128134

129135
@Autowired MongoTemplate template;
130136
@Autowired MongoDbFactory factory;
@@ -3141,6 +3147,28 @@ public void onBeforeSave(BeforeSaveEvent<Document> event) {
31413147
assertThat(document.id, is(notNullValue()));
31423148
}
31433149

3150+
/**
3151+
* @see DATAMONGO-1517
3152+
*/
3153+
@Test
3154+
public void decimal128TypeShouldBeSavedAndLoadedCorrectly()
3155+
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
3156+
3157+
assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_FOUR), is(true));
3158+
assumeThat(MongoClientVersion.isMongo34Driver(), is(true));
3159+
3160+
Class<?> decimal128Type = ClassUtils.resolveClassName("org.bson.types.Decimal128", null);
3161+
3162+
WithObjectTypeProperty source = new WithObjectTypeProperty();
3163+
source.id = "decimal128-property-value";
3164+
source.value = decimal128Type.getConstructor(BigDecimal.class).newInstance(new BigDecimal(100));
3165+
3166+
template.save(source);
3167+
3168+
WithObjectTypeProperty loaded = template.findOne(query(where("id").is(source.id)), WithObjectTypeProperty.class);
3169+
assertThat(loaded.getValue(), instanceOf(decimal128Type));
3170+
}
3171+
31443172
static class TypeWithNumbers {
31453173

31463174
@Id String id;
@@ -3510,4 +3538,11 @@ static class WithGeoJson {
35103538
String description;
35113539
GeoJsonPoint point;
35123540
}
3541+
3542+
@Data
3543+
static class WithObjectTypeProperty {
3544+
3545+
@Id String id;
3546+
Object value;
3547+
}
35133548
}

0 commit comments

Comments
 (0)