Skip to content

Commit 13b89db

Browse files
committed
Require explicit UUID representation configuration
Signed-off-by: Hyunsang Han <[email protected]>
1 parent e3af5c4 commit 13b89db

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* Base class for Spring Data MongoDB to be extended for JavaConfiguration usage.
4747
*
4848
* @author Mark Paluch
49+
* @author Hyunsang Han
4950
* @since 2.0
5051
*/
5152
public abstract class MongoConfigurationSupport {
@@ -226,9 +227,15 @@ protected boolean autoIndexCreation() {
226227
protected MongoClientSettings mongoClientSettings() {
227228

228229
MongoClientSettings.Builder builder = MongoClientSettings.builder();
229-
builder.uuidRepresentation(UuidRepresentation.STANDARD);
230230
configureClientSettings(builder);
231-
return builder.build();
231+
232+
MongoClientSettings settings = builder.build();
233+
234+
if (settings.getUuidRepresentation() == null) {
235+
throw new IllegalStateException("UUID representation must be explicitly configured.");
236+
}
237+
238+
return settings;
232239
}
233240

234241
/**

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.function.Function;
2424
import java.util.stream.Collectors;
2525

26-
import org.bson.UuidRepresentation;
2726
import org.jspecify.annotations.Nullable;
2827
import org.springframework.beans.factory.config.AbstractFactoryBean;
2928
import org.springframework.dao.DataAccessException;
@@ -52,6 +51,7 @@
5251
*
5352
* @author Christoph Strobl
5453
* @author Mark Paluch
54+
* @author Hyunsang Han
5555
*/
5656
public class MongoClientFactoryBean extends AbstractFactoryBean<MongoClient> implements PersistenceExceptionTranslator {
5757

@@ -162,7 +162,6 @@ protected MongoClientSettings computeClientSetting() {
162162
getOrDefault(port, "" + ServerAddress.defaultPort())));
163163

164164
Builder builder = MongoClientSettings.builder().applyConnectionString(connectionString);
165-
builder.uuidRepresentation(UuidRepresentation.STANDARD);
166165

167166
if (mongoClientSettings != null) {
168167

@@ -305,7 +304,13 @@ protected MongoClientSettings computeClientSetting() {
305304
});
306305
}
307306

308-
return builder.build();
307+
MongoClientSettings settings = builder.build();
308+
309+
if (settings.getUuidRepresentation() == null) {
310+
throw new IllegalStateException("UUID representation must be explicitly configured.");
311+
}
312+
313+
return settings;
309314
}
310315

311316
private <T> void applySettings(Consumer<T> settingsBuilder, @Nullable T value) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractMongoConfigurationUnitTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
import org.springframework.data.spel.ExtensionAwareEvaluationContextProvider;
4646
import org.springframework.test.util.ReflectionTestUtils;
4747

48+
import org.bson.UuidRepresentation;
49+
50+
import com.mongodb.MongoClientSettings;
4851
import com.mongodb.client.MongoClient;
4952

5053
/**
@@ -53,6 +56,7 @@
5356
* @author Oliver Gierke
5457
* @author Thomas Darimont
5558
* @author Mark Paluch
59+
* @author Hyunsang Han
5660
*/
5761
public class AbstractMongoConfigurationUnitTests {
5862

@@ -114,6 +118,40 @@ public void lifecycleCallbacksAreInvokedInAppropriateOrder() {
114118
context.close();
115119
}
116120

121+
@Test // GH-5037
122+
public void requiresExplicitUuidRepresentationConfiguration() {
123+
124+
assertThatThrownBy(() -> {
125+
AbstractMongoClientConfiguration config = new AbstractMongoClientConfiguration() {
126+
@Override
127+
protected String getDatabaseName() {
128+
return "test";
129+
}
130+
};
131+
config.mongoClientSettings();
132+
}).isInstanceOf(IllegalStateException.class)
133+
.hasMessageContaining("UUID representation must be explicitly configured");
134+
}
135+
136+
@Test // GH-5037
137+
public void worksWithExplicitUuidRepresentationConfiguration() {
138+
139+
AbstractMongoClientConfiguration config = new AbstractMongoClientConfiguration() {
140+
@Override
141+
protected String getDatabaseName() {
142+
return "test";
143+
}
144+
145+
@Override
146+
protected void configureClientSettings(MongoClientSettings.Builder builder) {
147+
builder.uuidRepresentation(UuidRepresentation.STANDARD);
148+
}
149+
};
150+
151+
MongoClientSettings settings = config.mongoClientSettings();
152+
assertThat(settings.getUuidRepresentation()).isEqualTo(UuidRepresentation.STANDARD);
153+
}
154+
117155
@Test // DATAMONGO-725
118156
public void shouldBeAbleToConfigureCustomTypeMapperViaJavaConfig() {
119157

@@ -158,6 +196,11 @@ protected String getDatabaseName() {
158196
return "database";
159197
}
160198

199+
@Override
200+
protected void configureClientSettings(MongoClientSettings.Builder builder) {
201+
builder.uuidRepresentation(UuidRepresentation.STANDARD);
202+
}
203+
161204
@Override
162205
public MongoClient mongoClient() {
163206
return Mockito.mock(MongoClient.class);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.junit.jupiter.api.Test;
2323

24+
import org.bson.UuidRepresentation;
25+
2426
import com.mongodb.ConnectionString;
2527
import com.mongodb.MongoClientSettings;
2628
import com.mongodb.ServerAddress;
@@ -29,6 +31,7 @@
2931
* Unit tests for {@link MongoClientFactoryBean}.
3032
*
3133
* @author Christoph Strobl
34+
* @author Hyunsang Han
3235
*/
3336
class MongoClientFactoryBeanUnitTests {
3437

@@ -89,4 +92,26 @@ void hostAndPortPlusConnectionStringError() {
8992
factoryBean.setPort(27017);
9093
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(factoryBean::createInstance);
9194
}
95+
96+
@Test // GH-5037
97+
void requiresExplicitUuidRepresentationConfiguration() {
98+
99+
MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
100+
101+
assertThatThrownBy(factoryBean::computeClientSetting)
102+
.isInstanceOf(IllegalStateException.class)
103+
.hasMessageContaining("UUID representation must be explicitly configured");
104+
}
105+
106+
@Test // GH-5037
107+
void worksWithExplicitUuidRepresentationConfiguration() {
108+
109+
MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
110+
factoryBean.setMongoClientSettings(
111+
MongoClientSettings.builder().uuidRepresentation(UuidRepresentation.STANDARD).build());
112+
113+
MongoClientSettings settings = factoryBean.computeClientSetting();
114+
115+
assertThat(settings.getUuidRepresentation()).isEqualTo(UuidRepresentation.STANDARD);
116+
}
92117
}

0 commit comments

Comments
 (0)