Skip to content

Commit a6ba15a

Browse files
committed
JAVA-2271: Ensure that ConnectionString.getCredentialList returns an immutable list.
1 parent 771ebe6 commit a6ba15a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

driver-core/src/main/com/mongodb/ConnectionString.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,12 +827,12 @@ public String getConnectionString() {
827827

828828

829829
/**
830-
* Gets the credentials.
830+
* Gets the credentials in an immutable list. The list will be empty if no credentials were specified in the connection string.
831831
*
832-
* @return the credentials
832+
* @return the credentials in an immutable list
833833
*/
834834
public List<MongoCredential> getCredentialList() {
835-
return credentials != null ? asList(credentials) : new ArrayList<MongoCredential>();
835+
return credentials != null ? singletonList(credentials) : Collections.<MongoCredential>emptyList();
836836
}
837837

838838
/**

driver-core/src/test/unit/com/mongodb/ConnectionStringSpecification.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,18 @@ class ConnectionStringSpecification extends Specification {
252252
.withMechanismProperty('SERVICE_REALM', 'AWESOME'))
253253
}
254254

255+
@Unroll
256+
def 'should create immutable credential list'() {
257+
when:
258+
uri.credentialList.add(createGSSAPICredential('user'))
259+
260+
then:
261+
thrown(UnsupportedOperationException)
262+
263+
where:
264+
uri << [new ConnectionString('mongodb://jeff:123@localhost'), new ConnectionString('mongodb://localhost')]
265+
}
266+
255267
def 'should support thrown an IllegalArgumentException when given invalid authMechanismProperties'() {
256268
when:
257269
new ConnectionString('mongodb://jeff@localhost/?' +

0 commit comments

Comments
 (0)