Skip to content

Commit

Permalink
Allow Mongo database property to override database in URI
Browse files Browse the repository at this point in the history
This commit restores the previous behavior of MongoDB auto-
configuration, where the value of the `spring.data.mongodb.database`
property takes precedence over the database specified in a
`spring.data.mongodb.uri` property.

Fixes gh-35566
  • Loading branch information
scottfrederick committed May 31, 2023
1 parent 715204d commit 593fa1d
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @author Stephane Nicoll
* @author Moritz Halbritter
* @author Phillip Webb
* @author Scott Frederick
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(MongoDatabaseFactory.class)
Expand All @@ -44,7 +45,11 @@ class MongoDatabaseFactoryConfiguration {
@Bean
MongoDatabaseFactorySupport<?> mongoDatabaseFactory(MongoClient mongoClient, MongoProperties properties,
MongoConnectionDetails connectionDetails) {
return new SimpleMongoClientDatabaseFactory(mongoClient, connectionDetails.getConnectionString().getDatabase());
String database = properties.getDatabase();
if (database == null) {
database = connectionDetails.getConnectionString().getDatabase();
}
return new SimpleMongoClientDatabaseFactory(mongoClient, database);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @since 2.0.0
*/
@AutoConfiguration(after = MongoReactiveAutoConfiguration.class)
Expand All @@ -84,9 +85,13 @@ public class MongoReactiveDataAutoConfiguration {

@Bean
@ConditionalOnMissingBean(ReactiveMongoDatabaseFactory.class)
public SimpleReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory(MongoClient mongo) {
return new SimpleReactiveMongoDatabaseFactory(mongo,
this.connectionDetails.getConnectionString().getDatabase());
public SimpleReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory(MongoClient mongo,
MongoProperties properties) {
String database = properties.getDatabase();
if (database == null) {
database = this.connectionDetails.getConnectionString().getDatabase();
}
return new SimpleReactiveMongoDatabaseFactory(mongo, database);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MongoDataAutoConfigurationTests {

Expand Down Expand Up @@ -226,6 +227,64 @@ void autoConfiguresIfUserProvidesMongoDatabaseFactoryButNoClient() {
.run((context) -> assertThat(context).hasSingleBean(MongoTemplate.class));
}

@Test
void databaseHasDefault() {
this.contextRunner.run((context) -> {
MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class);
assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
assertThat(factory.getMongoDatabase().getName()).isEqualTo("test");
});
}

@Test
void databasePropertyIsUsed() {
this.contextRunner.withPropertyValues("spring.data.mongodb.database=mydb").run((context) -> {
MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class);
assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb");
});
}

@Test
void databaseInUriPropertyIsUsed() {
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/mydb")
.run((context) -> {
MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class);
assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb");
});
}

@Test
void databasePropertyOverridesUriProperty() {
this.contextRunner
.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/notused",
"spring.data.mongodb.database=mydb")
.run((context) -> {
MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class);
assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb");
});
}

@Test
void databasePropertyIsUsedWhenNoDatabaseInUri() {
this.contextRunner
.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/",
"spring.data.mongodb.database=mydb")
.run((context) -> {
MongoDatabaseFactory factory = context.getBean(MongoDatabaseFactory.class);
assertThat(factory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
assertThat(factory.getMongoDatabase().getName()).isEqualTo("mydb");
});
}

@Test
void contextFailsWhenDatabaseNotSet() {
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/")
.run((context) -> assertThat(context).getFailure().hasMessageContaining("Database name must not be empty"));
}

@Test
void definesPropertiesBasedConnectionDetailsByDefault() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(PropertiesMongoConnectionDetails.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory;
import org.springframework.data.mongodb.gridfs.ReactiveGridFsTemplate;
import org.springframework.test.util.ReflectionTestUtils;

Expand All @@ -42,6 +43,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MongoReactiveDataAutoConfigurationTests {

Expand Down Expand Up @@ -90,6 +92,64 @@ void backsOffIfMongoClientBeanIsNotPresent() {
runner.run((context) -> assertThat(context).doesNotHaveBean(MongoReactiveDataAutoConfiguration.class));
}

@Test
void databaseHasDefault() {
this.contextRunner.run((context) -> {
ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class);
assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class);
assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("test");
});
}

@Test
void databasePropertyIsUsed() {
this.contextRunner.withPropertyValues("spring.data.mongodb.database=mydb").run((context) -> {
ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class);
assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class);
assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb");
});
}

@Test
void databaseInUriPropertyIsUsed() {
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/mydb")
.run((context) -> {
ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class);
assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class);
assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb");
});
}

@Test
void databasePropertyOverridesUriProperty() {
this.contextRunner
.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/notused",
"spring.data.mongodb.database=mydb")
.run((context) -> {
ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class);
assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class);
assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb");
});
}

@Test
void databasePropertyIsUsedWhenNoDatabaseInUri() {
this.contextRunner
.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/",
"spring.data.mongodb.database=mydb")
.run((context) -> {
ReactiveMongoDatabaseFactory factory = context.getBean(ReactiveMongoDatabaseFactory.class);
assertThat(factory).isInstanceOf(SimpleReactiveMongoDatabaseFactory.class);
assertThat(factory.getMongoDatabase().block().getName()).isEqualTo("mydb");
});
}

@Test
void contextFailsWhenDatabaseNotSet() {
this.contextRunner.withPropertyValues("spring.data.mongodb.uri=mongodb://mongo.example.com/")
.run((context) -> assertThat(context).getFailure().hasMessageContaining("Database name must not be empty"));
}

private String grisFsTemplateDatabaseName(AssertableApplicationContext context) {
assertThat(context).hasSingleBean(ReactiveGridFsTemplate.class);
ReactiveGridFsTemplate template = context.getBean(ReactiveGridFsTemplate.class);
Expand Down

0 comments on commit 593fa1d

Please sign in to comment.