Skip to content

Introduce spring.data.mongodb.protocol to allow the MongoDB protocol to be customized when not using spring.data.mongodb.uri #44366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just looking at this and laughed that it matches the polish I've made in 44384. What a coincidence! Nope, you forced push to adapt... You really didn't have to do it, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that you've merged #44384 and checked what phrasing was used to update this change accordingly.

*/
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