Skip to content

Commit 5d55c66

Browse files
committed
Allow spring.data.mongodb.uri to take precedence over separate props
Closes gh-30067
1 parent 06c3e2a commit 5d55c66

File tree

3 files changed

+26
-41
lines changed

3 files changed

+26
-41
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public class MongoProperties {
5959
private Integer port = null;
6060

6161
/**
62-
* Mongo database URI. Cannot be set with host, port, credentials and replica set
63-
* name.
62+
* Mongo database URI. Overrides host, port, username, password, and database.
6463
*/
6564
private String uri;
6665

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

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import org.springframework.core.Ordered;
2727
import org.springframework.core.env.Environment;
28-
import org.springframework.util.Assert;
2928

3029
/**
3130
* A {@link MongoClientSettingsBuilderCustomizer} that applies properties from a
@@ -49,20 +48,12 @@ public MongoPropertiesClientSettingsBuilderCustomizer(MongoProperties properties
4948

5049
@Override
5150
public void customize(MongoClientSettings.Builder settingsBuilder) {
52-
validateConfiguration();
5351
applyUuidRepresentation(settingsBuilder);
5452
applyHostAndPort(settingsBuilder);
5553
applyCredentials(settingsBuilder);
5654
applyReplicaSet(settingsBuilder);
5755
}
5856

59-
private void validateConfiguration() {
60-
if (hasCustomAddress() || hasCustomCredentials() || hasReplicaSet()) {
61-
Assert.state(this.properties.getUri() == null,
62-
"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified");
63-
}
64-
}
65-
6657
private void applyUuidRepresentation(MongoClientSettings.Builder settingsBuilder) {
6758
settingsBuilder.uuidRepresentation(this.properties.getUuidRepresentation());
6859
}
@@ -72,20 +63,23 @@ private void applyHostAndPort(MongoClientSettings.Builder settings) {
7263
settings.applyConnectionString(new ConnectionString("mongodb://localhost:" + getEmbeddedPort()));
7364
return;
7465
}
75-
76-
if (hasCustomAddress()) {
66+
if (this.properties.getUri() != null) {
67+
settings.applyConnectionString(new ConnectionString(this.properties.getUri()));
68+
return;
69+
}
70+
if (this.properties.getHost() != null || this.properties.getPort() != null) {
7771
String host = getOrDefault(this.properties.getHost(), "localhost");
7872
int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT);
7973
ServerAddress serverAddress = new ServerAddress(host, port);
8074
settings.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress)));
8175
return;
8276
}
83-
84-
settings.applyConnectionString(new ConnectionString(this.properties.determineUri()));
77+
settings.applyConnectionString(new ConnectionString(MongoProperties.DEFAULT_URI));
8578
}
8679

8780
private void applyCredentials(MongoClientSettings.Builder builder) {
88-
if (hasCustomCredentials()) {
81+
if (this.properties.getUri() == null && this.properties.getUsername() != null
82+
&& this.properties.getPassword() != null) {
8983
String database = (this.properties.getAuthenticationDatabase() != null)
9084
? this.properties.getAuthenticationDatabase() : this.properties.getMongoClientDatabase();
9185
builder.credential((MongoCredential.createCredential(this.properties.getUsername(), database,
@@ -94,7 +88,7 @@ private void applyCredentials(MongoClientSettings.Builder builder) {
9488
}
9589

9690
private void applyReplicaSet(MongoClientSettings.Builder builder) {
97-
if (hasReplicaSet()) {
91+
if (this.properties.getReplicaSetName() != null) {
9892
builder.applyToClusterSettings(
9993
(cluster) -> cluster.requiredReplicaSetName(this.properties.getReplicaSetName()));
10094
}
@@ -114,18 +108,6 @@ private Integer getEmbeddedPort() {
114108
return null;
115109
}
116110

117-
private boolean hasCustomCredentials() {
118-
return this.properties.getUsername() != null && this.properties.getPassword() != null;
119-
}
120-
121-
private boolean hasCustomAddress() {
122-
return this.properties.getHost() != null || this.properties.getPort() != null;
123-
}
124-
125-
private boolean hasReplicaSet() {
126-
return this.properties.getReplicaSetName() != null;
127-
}
128-
129111
@Override
130112
public int getOrder() {
131113
return this.order;

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.mock.env.MockEnvironment;
2828

2929
import static org.assertj.core.api.Assertions.assertThat;
30-
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3130

3231
/**
3332
* Tests for {@link MongoPropertiesClientSettingsBuilderCustomizer}.
@@ -153,29 +152,34 @@ void uriIsIgnoredInEmbeddedMode() {
153152
}
154153

155154
@Test
156-
void uriCannotBeSetWithCredentials() {
155+
void uriOverridesUsernameAndPassword() {
157156
this.properties.setUri("mongodb://127.0.0.1:1234/mydb");
158157
this.properties.setUsername("user");
159158
this.properties.setPassword("secret".toCharArray());
160-
assertThatIllegalStateException().isThrownBy(this::customizeSettings).withMessageContaining(
161-
"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified");
159+
MongoClientSettings settings = customizeSettings();
160+
assertThat(settings.getCredential()).isNull();
162161
}
163162

164163
@Test
165-
void uriCannotBeSetWithReplicaSetName() {
166-
this.properties.setUri("mongodb://127.0.0.1:1234/mydb");
167-
this.properties.setReplicaSetName("test");
168-
assertThatIllegalStateException().isThrownBy(this::customizeSettings).withMessageContaining(
169-
"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified");
164+
void uriOverridesDatabase() {
165+
this.properties.setUri("mongodb://secret:[email protected]:1234/mydb");
166+
this.properties.setDatabase("test");
167+
MongoClientSettings settings = customizeSettings();
168+
List<ServerAddress> allAddresses = getAllAddresses(settings);
169+
assertThat(allAddresses).hasSize(1);
170+
assertServerAddress(allAddresses.get(0), "127.0.0.1", 1234);
171+
assertThat(settings.getCredential().getSource()).isEqualTo("mydb");
170172
}
171173

172174
@Test
173-
void uriCannotBeSetWithHostPort() {
175+
void uriOverridesHostAndPort() {
174176
this.properties.setUri("mongodb://127.0.0.1:1234/mydb");
175177
this.properties.setHost("localhost");
176178
this.properties.setPort(4567);
177-
assertThatIllegalStateException().isThrownBy(this::customizeSettings).withMessageContaining(
178-
"Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified");
179+
MongoClientSettings settings = customizeSettings();
180+
List<ServerAddress> addresses = getAllAddresses(settings);
181+
assertThat(addresses.get(0).getHost()).isEqualTo("127.0.0.1");
182+
assertThat(addresses.get(0).getPort()).isEqualTo(1234);
179183
}
180184

181185
@Test

0 commit comments

Comments
 (0)