Skip to content

Commit 0cb07bb

Browse files
committed
DATAMONGO-1919 - Allow reactive-only usage of ReactiveMongoTemplate.
We now support reactive-only usage of Spring Data MongoDB without the need to use the synchronous driver or even having it on the class path. We conditionally register CodeWScope/CodeWithScope types that are bundled with the particular driver distributions. NoOpDbRefResolver is now a top-level type to be used with a reactive-only MongoMappingContext.
1 parent 0bb1588 commit 0cb07bb

File tree

8 files changed

+101
-79
lines changed

8 files changed

+101
-79
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractReactiveMongoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
2323
import org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory;
2424
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
25+
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
2526

2627
import com.mongodb.reactivestreams.client.MongoClient;
2728

@@ -80,7 +81,7 @@ public ReactiveMongoDatabaseFactory reactiveMongoDbFactory() {
8081
@Bean
8182
public MappingMongoConverter mappingMongoConverter() throws Exception {
8283

83-
MappingMongoConverter converter = new MappingMongoConverter(ReactiveMongoTemplate.NO_OP_REF_RESOLVER,
84+
MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE,
8485
mongoMappingContext());
8586
converter.setCustomConversions(customConversions());
8687

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

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import java.util.function.Function;
3232
import java.util.stream.Collectors;
3333

34-
import javax.annotation.Nonnull;
35-
3634
import org.bson.BsonValue;
3735
import org.bson.Document;
3836
import org.bson.codecs.Codec;
@@ -119,7 +117,6 @@
119117
import com.mongodb.CursorType;
120118
import com.mongodb.DBCollection;
121119
import com.mongodb.DBCursor;
122-
import com.mongodb.DBRef;
123120
import com.mongodb.Mongo;
124121
import com.mongodb.MongoException;
125122
import com.mongodb.ReadPreference;
@@ -165,7 +162,7 @@
165162
*/
166163
public class ReactiveMongoTemplate implements ReactiveMongoOperations, ApplicationContextAware {
167164

168-
public static final DbRefResolver NO_OP_REF_RESOLVER = new NoOpDbRefResolver();
165+
public static final DbRefResolver NO_OP_REF_RESOLVER = NoOpDbRefResolver.INSTANCE;
169166

170167
private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveMongoTemplate.class);
171168
private static final String ID_FIELD = "_id";
@@ -3174,54 +3171,6 @@ private static List<? extends Document> toDocuments(final Collection<? extends D
31743171
return new ArrayList<>(documents);
31753172
}
31763173

3177-
/**
3178-
* No-Operation {@link org.springframework.data.mongodb.core.mapping.DBRef} resolver.
3179-
*
3180-
* @author Mark Paluch
3181-
*/
3182-
static class NoOpDbRefResolver implements DbRefResolver {
3183-
3184-
/*
3185-
* (non-Javadoc)
3186-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#resolveDbRef(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.convert.DbRefResolverCallback)
3187-
*/
3188-
@Override
3189-
@Nullable
3190-
public Object resolveDbRef(@Nonnull MongoPersistentProperty property, @Nonnull DBRef dbref,
3191-
@Nonnull DbRefResolverCallback callback, @Nonnull DbRefProxyHandler proxyHandler) {
3192-
return null;
3193-
}
3194-
3195-
/*
3196-
* (non-Javadoc)
3197-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#created(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.mapping.MongoPersistentEntity, java.lang.Object)
3198-
*/
3199-
@Override
3200-
@Nullable
3201-
public DBRef createDbRef(org.springframework.data.mongodb.core.mapping.DBRef annotation,
3202-
MongoPersistentEntity<?> entity, Object id) {
3203-
return null;
3204-
}
3205-
3206-
/*
3207-
* (non-Javadoc)
3208-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#fetch(com.mongodb.DBRef)
3209-
*/
3210-
@Override
3211-
public Document fetch(DBRef dbRef) {
3212-
return null;
3213-
}
3214-
3215-
/*
3216-
* (non-Javadoc)
3217-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#bulkFetch(java.util.List)
3218-
*/
3219-
@Override
3220-
public List<Document> bulkFetch(List<DBRef> dbRefs) {
3221-
return Collections.emptyList();
3222-
}
3223-
}
3224-
32253174
/**
32263175
* {@link MongoTemplate} extension bound to a specific {@link ClientSession} that is applied when interacting with the
32273176
* server through the driver API.

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2323
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
2424
import org.springframework.lang.Nullable;
25+
import org.springframework.util.StringUtils;
2526

2627
import com.mongodb.DBRef;
2728

@@ -59,9 +60,15 @@ Object resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbref, DbR
5960
* @param id will never be {@literal null}.
6061
* @return
6162
*/
62-
DBRef createDbRef(@Nullable org.springframework.data.mongodb.core.mapping.DBRef annotation,
63-
MongoPersistentEntity<?> entity,
64-
Object id);
63+
default DBRef createDbRef(@Nullable org.springframework.data.mongodb.core.mapping.DBRef annotation,
64+
MongoPersistentEntity<?> entity, Object id) {
65+
66+
if (annotation != null && StringUtils.hasText(annotation.db())) {
67+
return new DBRef(annotation.db(), entity.getCollection(), id);
68+
}
69+
70+
return new DBRef(entity.getCollection(), id);
71+
}
6572

6673
/**
6774
* Actually loads the {@link DBRef} from the datasource.

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,6 @@ public Object resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbr
104104
return callback.resolve(property);
105105
}
106106

107-
/*
108-
* (non-Javadoc)
109-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#created(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.mapping.MongoPersistentEntity, java.lang.Object)
110-
*/
111-
@Override
112-
public DBRef createDbRef(@Nullable org.springframework.data.mongodb.core.mapping.DBRef annotation,
113-
MongoPersistentEntity<?> entity, Object id) {
114-
115-
if (annotation != null && StringUtils.hasText(annotation.db())) {
116-
return new DBRef(annotation.db(), entity.getCollection(), id);
117-
}
118-
119-
return new DBRef(entity.getCollection(), id);
120-
}
121-
122107
/*
123108
* (non-Javadoc)
124109
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#fetch(com.mongodb.DBRef)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2018 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+
* http://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.mongodb.core.convert;
17+
18+
import java.util.List;
19+
20+
import javax.annotation.Nonnull;
21+
22+
import org.bson.Document;
23+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
24+
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
25+
import org.springframework.lang.Nullable;
26+
import org.springframework.util.StringUtils;
27+
28+
import com.mongodb.DBRef;
29+
30+
/**
31+
* No-Operation {@link org.springframework.data.mongodb.core.mapping.DBRef} resolver throwing
32+
* {@link UnsupportedOperationException} when attempting to resolve database references.
33+
*
34+
* @author Mark Paluch
35+
* @since 2.1
36+
*/
37+
public enum NoOpDbRefResolver implements DbRefResolver {
38+
39+
INSTANCE;
40+
41+
/*
42+
* (non-Javadoc)
43+
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#resolveDbRef(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.convert.DbRefResolverCallback)
44+
*/
45+
@Override
46+
@Nullable
47+
public Object resolveDbRef(@Nonnull MongoPersistentProperty property, @Nonnull DBRef dbref,
48+
@Nonnull DbRefResolverCallback callback, @Nonnull DbRefProxyHandler proxyHandler) {
49+
throw new UnsupportedOperationException("DBRef resolution not supported!");
50+
}
51+
52+
/*
53+
* (non-Javadoc)
54+
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#fetch(com.mongodb.DBRef)
55+
*/
56+
@Override
57+
public Document fetch(DBRef dbRef) {
58+
throw new UnsupportedOperationException("DBRef resolution not supported!");
59+
}
60+
61+
/*
62+
* (non-Javadoc)
63+
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#bulkFetch(java.util.List)
64+
*/
65+
@Override
66+
public List<Document> bulkFetch(List<DBRef> dbRefs) {
67+
throw new UnsupportedOperationException("DBRef resolution not supported!");
68+
}
69+
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import org.bson.BsonObjectId;
2626
import org.bson.types.Binary;
27-
import org.bson.types.CodeWScope;
2827
import org.bson.types.ObjectId;
2928
import org.springframework.data.mapping.model.SimpleTypeHolder;
3029
import org.springframework.data.mongodb.util.MongoClientVersion;
@@ -37,6 +36,7 @@
3736
*
3837
* @author Oliver Gierke
3938
* @author Christoph Strobl
39+
* @author Mark Paluch
4040
*/
4141
public abstract class MongoSimpleTypes {
4242

@@ -53,15 +53,21 @@ public abstract class MongoSimpleTypes {
5353
simpleTypes.add(DBRef.class);
5454
simpleTypes.add(ObjectId.class);
5555
simpleTypes.add(BsonObjectId.class);
56-
simpleTypes.add(CodeWScope.class);
5756
simpleTypes.add(org.bson.Document.class);
5857
simpleTypes.add(Pattern.class);
5958
simpleTypes.add(Binary.class);
6059
simpleTypes.add(UUID.class);
6160

61+
if (ClassUtils.isPresent("org.bson.types.CodeWScope", MongoSimpleTypes.class.getClassLoader())) {
62+
simpleTypes.add(resolveClassName("org.bson.types.CodeWScope"));
63+
}
64+
65+
if (ClassUtils.isPresent("org.bson.types.CodeWithScope", MongoSimpleTypes.class.getClassLoader())) {
66+
simpleTypes.add(resolveClassName("org.bson.types.CodeWithScope"));
67+
}
68+
6269
if (MongoClientVersion.isMongo34Driver()) {
63-
simpleTypes
64-
.add(ClassUtils.resolveClassName("org.bson.types.Decimal128", MongoSimpleTypes.class.getClassLoader()));
70+
simpleTypes.add(resolveClassName("org.bson.types.Decimal128"));
6571
}
6672

6773
MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
@@ -71,4 +77,8 @@ public abstract class MongoSimpleTypes {
7177
public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true);
7278

7379
private MongoSimpleTypes() {}
80+
81+
private static Class<?> resolveClassName(String className) {
82+
return ClassUtils.resolveClassName(className, MongoSimpleTypes.class.getClassLoader());
83+
}
7484
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.mongodb.client.model.ReplaceOptions;
2525
import lombok.Data;
26+
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
2627
import reactor.core.publisher.Mono;
2728
import reactor.test.StepVerifier;
2829

@@ -43,7 +44,6 @@
4344
import org.springframework.beans.factory.annotation.Value;
4445
import org.springframework.data.annotation.Id;
4546
import org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId;
46-
import org.springframework.data.mongodb.core.ReactiveMongoTemplate.NoOpDbRefResolver;
4747
import org.springframework.data.mongodb.core.aggregation.Aggregation;
4848
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
4949
import org.springframework.data.mongodb.core.mapping.Field;
@@ -101,7 +101,7 @@ public void setUp() {
101101
when(findPublisher.first()).thenReturn(findPublisher);
102102

103103
this.mappingContext = new MongoMappingContext();
104-
this.converter = new MappingMongoConverter(new NoOpDbRefResolver(), mappingContext);
104+
this.converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
105105
this.template = new ReactiveMongoTemplate(factory, converter);
106106

107107
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
import org.springframework.data.geo.Metrics;
4040
import org.springframework.data.geo.Point;
4141
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
42-
import org.springframework.data.mongodb.core.ReactiveMongoTemplate.NoOpDbRefResolver;
4342
import org.springframework.data.mongodb.core.ReactiveMongoTemplate.ReactiveSessionBoundMongoTemplate;
4443
import org.springframework.data.mongodb.core.aggregation.Aggregation;
4544
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
45+
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
4646
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
4747
import org.springframework.data.mongodb.core.query.NearQuery;
4848
import org.springframework.data.mongodb.core.query.Query;
@@ -64,6 +64,7 @@
6464
* Unit tests for {@link ReactiveSessionBoundMongoTemplate}.
6565
*
6666
* @author Christoph Strobl
67+
* @author Mark Paluch
6768
*/
6869
@SuppressWarnings("unchecked")
6970
@RunWith(MockitoJUnitRunner.Silent.class)
@@ -124,7 +125,7 @@ public void setUp() {
124125
factory = new SimpleReactiveMongoDatabaseFactory(client, "foo");
125126

126127
this.mappingContext = new MongoMappingContext();
127-
this.converter = new MappingMongoConverter(new NoOpDbRefResolver(), mappingContext);
128+
this.converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
128129
this.template = new ReactiveSessionBoundMongoTemplate(clientSession, new ReactiveMongoTemplate(factory, converter));
129130
}
130131

0 commit comments

Comments
 (0)