Skip to content

Commit 1085505

Browse files
committed
Support blank MongoDB 'replica-set-name' properties
Update `null` checks to `StringUtils.hasText` to allow the `replica-set-name' property to be overridden with an empty string. Fixes gh-42055
1 parent c98363d commit 1085505

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -26,6 +26,7 @@
2626

2727
import org.springframework.core.Ordered;
2828
import org.springframework.util.CollectionUtils;
29+
import org.springframework.util.StringUtils;
2930

3031
/**
3132
* A {@link MongoClientSettingsBuilderCustomizer} that applies properties from a
@@ -90,7 +91,7 @@ private void applyCredentials(MongoClientSettings.Builder builder) {
9091
}
9192

9293
private void applyReplicaSet(MongoClientSettings.Builder builder) {
93-
if (this.properties.getReplicaSetName() != null) {
94+
if (StringUtils.hasText(this.properties.getReplicaSetName())) {
9495
builder.applyToClusterSettings(
9596
(cluster) -> cluster.requiredReplicaSetName(this.properties.getReplicaSetName()));
9697
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/PropertiesMongoConnectionDetails.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -23,6 +23,8 @@
2323

2424
import com.mongodb.ConnectionString;
2525

26+
import org.springframework.util.StringUtils;
27+
2628
/**
2729
* Adapts {@link MongoProperties} to {@link MongoConnectionDetails}.
2830
*
@@ -90,7 +92,7 @@ public GridFs getGridFs() {
9092

9193
private List<String> getOptions() {
9294
List<String> options = new ArrayList<>();
93-
if (this.properties.getReplicaSetName() != null) {
95+
if (StringUtils.hasText(this.properties.getReplicaSetName())) {
9496
options.add("replicaSet=" + this.properties.getReplicaSetName());
9597
}
9698
if (this.properties.getUsername() != null && this.properties.getAuthenticationDatabase() != null) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesClientSettingsBuilderCustomizerTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.mongodb.MongoClientSettings;
2323
import com.mongodb.MongoCredential;
2424
import com.mongodb.ServerAddress;
25+
import com.mongodb.connection.ClusterType;
2526
import org.bson.UuidRepresentation;
2627
import org.junit.jupiter.api.Test;
2728

@@ -81,6 +82,23 @@ void replicaSetCanBeCustomized() {
8182
this.properties.setReplicaSetName("test");
8283
MongoClientSettings settings = customizeSettings();
8384
assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isEqualTo("test");
85+
assertThat(settings.getClusterSettings().getRequiredClusterType()).isEqualTo(ClusterType.REPLICA_SET);
86+
}
87+
88+
@Test
89+
void replicaSetCanBeNull() {
90+
this.properties.setReplicaSetName(null);
91+
MongoClientSettings settings = customizeSettings();
92+
assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isNull();
93+
assertThat(settings.getClusterSettings().getRequiredClusterType()).isEqualTo(ClusterType.UNKNOWN);
94+
}
95+
96+
@Test
97+
void replicaSetCanBeEmptyString() {
98+
this.properties.setReplicaSetName("");
99+
MongoClientSettings settings = customizeSettings();
100+
assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isNull();
101+
assertThat(settings.getClusterSettings().getRequiredClusterType()).isEqualTo(ClusterType.UNKNOWN);
84102
}
85103

86104
@Test

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/PropertiesMongoConnectionDetailsTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -104,6 +104,20 @@ void replicaSetCanBeConfiguredWithDatabase() {
104104
assertThat(connectionString.getRequiredReplicaSetName()).isEqualTo("test");
105105
}
106106

107+
@Test
108+
void replicaSetCanBeNull() {
109+
this.properties.setReplicaSetName(null);
110+
ConnectionString connectionString = getConnectionString();
111+
assertThat(connectionString.getRequiredReplicaSetName()).isNull();
112+
}
113+
114+
@Test
115+
void replicaSetCanBeBlank() {
116+
this.properties.setReplicaSetName("");
117+
ConnectionString connectionString = getConnectionString();
118+
assertThat(connectionString.getRequiredReplicaSetName()).isNull();
119+
}
120+
107121
@Test
108122
void whenAdditionalHostsAreConfiguredThenTheyAreIncludedInHostsOfConnectionString() {
109123
this.properties.setHost("mongo1.example.com");

0 commit comments

Comments
 (0)