Skip to content

Commit 55342ac

Browse files
committed
JAVA-682: Changed MongoURI.getOptions() to return a cached instance, so as not to break clients that are mutating that instance before passing the MongoURI to a Mongo constructor
1 parent b2182e6 commit 55342ac

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/main/com/mongodb/MongoURI.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public class MongoURI {
126126
public static final String MONGODB_PREFIX = "mongodb://";
127127

128128
private final MongoClientURI mongoClientURI;
129+
private final MongoOptions mongoOptions;
129130

130131
/**
131132
* Creates a MongoURI from a string.
@@ -134,10 +135,12 @@ public class MongoURI {
134135
*/
135136
public MongoURI( String uri ) {
136137
this.mongoClientURI = new MongoClientURI(uri, new MongoClientOptions.Builder().legacyDefaults());
138+
mongoOptions = new MongoOptions(mongoClientURI.getOptions());
137139
}
138140

139141
public MongoURI(final MongoClientURI mongoClientURI) {
140142
this.mongoClientURI = mongoClientURI;
143+
mongoOptions = new MongoOptions(mongoClientURI.getOptions());
141144
}
142145

143146
// ---------------------------------
@@ -183,11 +186,12 @@ public String getCollection(){
183186
}
184187

185188
/**
186-
* Gets the options
187-
* @return
189+
* Gets the options. This method will return the same instance of {@code MongoOptions} for every call, so it's
190+
* possible to mutate the returned instance to change the defaults.
191+
* @return the mongo options
188192
*/
189193
public MongoOptions getOptions(){
190-
return new MongoOptions(mongoClientURI.getOptions());
194+
return mongoOptions;
191195
}
192196

193197
/**

src/test/com/mongodb/MongoURITest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,10 @@ public void testOptionDefaults() {
6666
assertEquals(options.slaveOk, false);
6767
assertEquals(options.isCursorFinalizerEnabled(), true);
6868
}
69+
70+
@Test
71+
public void testOptionSameInstance() {
72+
MongoURI mongoURI = new MongoURI( "mongodb://localhost");
73+
assertSame(mongoURI.getOptions(), mongoURI.getOptions());
74+
}
6975
}

0 commit comments

Comments
 (0)