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
This commit is contained in:
Phillip Webb 2024-08-29 15:04:14 -07:00
parent c98363d016
commit 10855056cc
4 changed files with 40 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2023 the original author or authors. * Copyright 2012-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,6 +26,7 @@ import com.mongodb.ServerAddress;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/** /**
* A {@link MongoClientSettingsBuilderCustomizer} that applies properties from a * A {@link MongoClientSettingsBuilderCustomizer} that applies properties from a
@ -90,7 +91,7 @@ public class MongoPropertiesClientSettingsBuilderCustomizer implements MongoClie
} }
private void applyReplicaSet(MongoClientSettings.Builder builder) { private void applyReplicaSet(MongoClientSettings.Builder builder) {
if (this.properties.getReplicaSetName() != null) { if (StringUtils.hasText(this.properties.getReplicaSetName())) {
builder.applyToClusterSettings( builder.applyToClusterSettings(
(cluster) -> cluster.requiredReplicaSetName(this.properties.getReplicaSetName())); (cluster) -> cluster.requiredReplicaSetName(this.properties.getReplicaSetName()));
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2023 the original author or authors. * Copyright 2012-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,8 @@ import java.util.List;
import com.mongodb.ConnectionString; import com.mongodb.ConnectionString;
import org.springframework.util.StringUtils;
/** /**
* Adapts {@link MongoProperties} to {@link MongoConnectionDetails}. * Adapts {@link MongoProperties} to {@link MongoConnectionDetails}.
* *
@ -90,7 +92,7 @@ public class PropertiesMongoConnectionDetails implements MongoConnectionDetails
private List<String> getOptions() { private List<String> getOptions() {
List<String> options = new ArrayList<>(); List<String> options = new ArrayList<>();
if (this.properties.getReplicaSetName() != null) { if (StringUtils.hasText(this.properties.getReplicaSetName())) {
options.add("replicaSet=" + this.properties.getReplicaSetName()); options.add("replicaSet=" + this.properties.getReplicaSetName());
} }
if (this.properties.getUsername() != null && this.properties.getAuthenticationDatabase() != null) { if (this.properties.getUsername() != null && this.properties.getAuthenticationDatabase() != null) {

View File

@ -22,6 +22,7 @@ import java.util.List;
import com.mongodb.MongoClientSettings; import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential; import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress; import com.mongodb.ServerAddress;
import com.mongodb.connection.ClusterType;
import org.bson.UuidRepresentation; import org.bson.UuidRepresentation;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -81,6 +82,23 @@ class MongoPropertiesClientSettingsBuilderCustomizerTests {
this.properties.setReplicaSetName("test"); this.properties.setReplicaSetName("test");
MongoClientSettings settings = customizeSettings(); MongoClientSettings settings = customizeSettings();
assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isEqualTo("test"); assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isEqualTo("test");
assertThat(settings.getClusterSettings().getRequiredClusterType()).isEqualTo(ClusterType.REPLICA_SET);
}
@Test
void replicaSetCanBeNull() {
this.properties.setReplicaSetName(null);
MongoClientSettings settings = customizeSettings();
assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isNull();
assertThat(settings.getClusterSettings().getRequiredClusterType()).isEqualTo(ClusterType.UNKNOWN);
}
@Test
void replicaSetCanBeEmptyString() {
this.properties.setReplicaSetName("");
MongoClientSettings settings = customizeSettings();
assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isNull();
assertThat(settings.getClusterSettings().getRequiredClusterType()).isEqualTo(ClusterType.UNKNOWN);
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2023 the original author or authors. * Copyright 2012-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -104,6 +104,20 @@ class PropertiesMongoConnectionDetailsTests {
assertThat(connectionString.getRequiredReplicaSetName()).isEqualTo("test"); assertThat(connectionString.getRequiredReplicaSetName()).isEqualTo("test");
} }
@Test
void replicaSetCanBeNull() {
this.properties.setReplicaSetName(null);
ConnectionString connectionString = getConnectionString();
assertThat(connectionString.getRequiredReplicaSetName()).isNull();
}
@Test
void replicaSetCanBeBlank() {
this.properties.setReplicaSetName("");
ConnectionString connectionString = getConnectionString();
assertThat(connectionString.getRequiredReplicaSetName()).isNull();
}
@Test @Test
void whenAdditionalHostsAreConfiguredThenTheyAreIncludedInHostsOfConnectionString() { void whenAdditionalHostsAreConfiguredThenTheyAreIncludedInHostsOfConnectionString() {
this.properties.setHost("mongo1.example.com"); this.properties.setHost("mongo1.example.com");