Skip to content

Commit

Permalink
Merge pull request spring-projects#44366 from nosan
Browse files Browse the repository at this point in the history
* pr/44366:
  Add spring.data.mongodb.protocol property

Closes spring-projectsgh-44366
  • Loading branch information
mhalbritter committed Feb 25, 2025
2 parents 2b854c7 + 35db02b commit bde4fb0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public class MongoProperties {
*/
public static final String DEFAULT_URI = "mongodb://localhost/test";

/**
* Protocol to be used for the MongoDB connection. Ignored if 'uri' is set.
*/
private String protocol = "mongodb";

/**
* Mongo server host. Ignored if 'uri' is set.
*/
Expand Down Expand Up @@ -117,6 +122,14 @@ public class MongoProperties {
*/
private Boolean autoIndexCreation;

public void setProtocol(String protocol) {
this.protocol = protocol;
}

public String getProtocol() {
return this.protocol;
}

public String getHost() {
return this.host;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public PropertiesMongoConnectionDetails(MongoProperties properties, SslBundles s

@Override
public ConnectionString getConnectionString() {
// mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]
// protocol://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]
if (this.properties.getUri() != null) {
return new ConnectionString(this.properties.getUri());
}
StringBuilder builder = new StringBuilder("mongodb://");
StringBuilder builder = new StringBuilder(getProtocol()).append("://");
if (this.properties.getUsername() != null) {
builder.append(encode(this.properties.getUsername()));
builder.append(":");
Expand Down Expand Up @@ -83,6 +83,14 @@ public ConnectionString getConnectionString() {
return new ConnectionString(builder.toString());
}

private String getProtocol() {
String protocol = this.properties.getProtocol();
if (StringUtils.hasText(protocol)) {
return protocol;
}
return "mongodb";
}

private String encode(String input) {
return URLEncoder.encode(input, StandardCharsets.UTF_8);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2924,6 +2924,22 @@
}
]
},
{
"name": "spring.data.mongodb.protocol",
"values": [
{
"value": "mongodb"
},
{
"value": "mongodb+srv"
}
],
"providers": [
{
"name": "any"
}
]
},
{
"name": "spring.data.redis.lettuce.read-from",
"values": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.internal.MongoClientImpl;
import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.connection.SslSettings;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -98,6 +99,22 @@ void configuresSslWithBundle() {
});
}

@Test
void configuresProtocol() {
this.contextRunner.withPropertyValues("spring.data.mongodb.protocol=mongodb+srv").run((context) -> {
MongoClientSettings settings = getSettings(context);
assertThat(settings.getClusterSettings().getMode()).isEqualTo(ClusterConnectionMode.MULTIPLE);
});
}

@Test
void defaultProtocol() {
this.contextRunner.run((context) -> {
MongoClientSettings settings = getSettings(context);
assertThat(settings.getClusterSettings().getMode()).isEqualTo(ClusterConnectionMode.SINGLE);
});
}

@Test
void configuresWithoutSslWhenDisabledWithBundle() {
this.contextRunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ void databaseHasDefaultWhenNotConfigured() {
assertThat(connectionString.getDatabase()).isEqualTo("test");
}

@Test
void protocolCanBeConfigured() {
this.properties.setProtocol("mongodb+srv");
ConnectionString connectionString = this.connectionDetails.getConnectionString();
assertThat(connectionString.getConnectionString()).startsWith("mongodb+srv://");
}

@Test
void authenticationDatabaseCanBeConfigured() {
this.properties.setUsername("user");
Expand Down

0 comments on commit bde4fb0

Please sign in to comment.