Skip to content

Commit 69f329d

Browse files
committed
Fixed MongoClientSettings builder CodecRegistry
Ensures that the Scala default codec registry is used, unless the user explictly uses a custom one SCALA-220
1 parent 89e353c commit 69f329d

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

bson/src/main/scala/org/mongodb/scala/bson/codecs/package.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package org.mongodb.scala.bson
1818

1919
import org.bson.codecs.configuration.CodecRegistries._
2020
import org.bson.codecs.configuration.CodecRegistry
21-
import org.bson.codecs.{DocumentCodecProvider => ADocumentCodecProvider}
2221
import com.mongodb.async.client.MongoClients
2322

2423
package object codecs {

driver/src/main/scala/org/mongodb/scala/MongoClient.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ import java.io.Closeable
2020

2121
import scala.reflect.ClassTag
2222

23-
import org.bson.codecs.configuration.CodecRegistries.{ fromProviders, fromRegistries }
2423
import org.bson.codecs.configuration.CodecRegistry
2524
import com.mongodb.ConnectionString
2625
import com.mongodb.async.client.{ MongoClient => JMongoClient, MongoClients }
2726

2827
import org.mongodb.scala.bson.DefaultHelper.DefaultsTo
29-
import org.mongodb.scala.bson.codecs.DocumentCodecProvider
3028
import org.mongodb.scala.connection.{ ClusterSettings, ConnectionPoolSettings, ServerSettings, SocketSettings, SslSettings }
3129
import org.mongodb.scala.internal.ObservableHelper.observe
3230

@@ -70,10 +68,7 @@ object MongoClient {
7068
*/
7169
def apply(clientSettings: MongoClientSettings): MongoClient = MongoClient(MongoClients.create(clientSettings))
7270

73-
val DEFAULT_CODEC_REGISTRY: CodecRegistry = fromRegistries(
74-
MongoClients.getDefaultCodecRegistry,
75-
fromProviders(DocumentCodecProvider())
76-
)
71+
val DEFAULT_CODEC_REGISTRY: CodecRegistry = org.mongodb.scala.bson.codecs.DEFAULT_CODEC_REGISTRY
7772
}
7873

7974
/**

driver/src/main/scala/org/mongodb/scala/MongoClientSettings.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.mongodb.scala
1818

19-
import com.mongodb.async.client.{ MongoClientSettings => JMongoClientSettings }
19+
import com.mongodb.async.client.{ MongoClientSettings => JMongoClientSettings, MongoClients => JMongoClients }
2020

2121
/**
2222
* A MongoClientSettings companion object
@@ -29,13 +29,20 @@ object MongoClientSettings {
2929
* Creates a the default builder
3030
* @return a MongoClientSettings builder
3131
*/
32-
def builder(): JMongoClientSettings.Builder = JMongoClientSettings.builder()
32+
def builder(): JMongoClientSettings.Builder = JMongoClientSettings.builder().codecRegistry(MongoClient.DEFAULT_CODEC_REGISTRY)
3333

3434
/**
3535
* Creates a builder from an existing `MongoClientSettings`.
3636
*
3737
* @param settings the settings to create the builder from
3838
* @return a MongoClientSettings builder
3939
*/
40-
def builder(settings: MongoClientSettings): JMongoClientSettings.Builder = JMongoClientSettings.builder(settings)
40+
def builder(settings: MongoClientSettings): JMongoClientSettings.Builder = {
41+
val builder = JMongoClientSettings.builder(settings)
42+
if (settings.getCodecRegistry == JMongoClients.getDefaultCodecRegistry) {
43+
builder.codecRegistry(MongoClient.DEFAULT_CODEC_REGISTRY)
44+
}
45+
builder
46+
}
47+
4148
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.mongodb.scala
18+
19+
import org.bson.codecs.configuration.CodecRegistries._
20+
21+
import org.mongodb.scala.bson.codecs.{ DEFAULT_CODEC_REGISTRY, DocumentCodecProvider }
22+
import org.scalamock.scalatest.proxy.MockFactory
23+
import org.scalatest.{ FlatSpec, Matchers }
24+
25+
class MongoClientSettingsSpec extends FlatSpec with Matchers with MockFactory {
26+
27+
"MongoClientSettings" should "default with the Scala Codec Registry" in {
28+
MongoClientSettings.builder().build().getCodecRegistry should equal(DEFAULT_CODEC_REGISTRY)
29+
}
30+
31+
it should "keep the default Scala Codec Registry in no codec registry is set" in {
32+
val settings = MongoClientSettings.builder().readPreference(ReadPreference.nearest()).build()
33+
MongoClientSettings.builder(settings).build().getCodecRegistry should equal(DEFAULT_CODEC_REGISTRY)
34+
}
35+
36+
it should "use a none default Codec Registry if set" in {
37+
val codecRegistry = fromProviders(DocumentCodecProvider())
38+
val settings = MongoClientSettings.builder().codecRegistry(codecRegistry).build()
39+
MongoClientSettings.builder(settings).build().getCodecRegistry should equal(codecRegistry)
40+
}
41+
42+
}

driver/src/test/scala/org/mongodb/scala/MongoClientSpec.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
package org.mongodb.scala
18+
1819
import scala.collection.JavaConverters._
1920

2021
import org.bson.BsonDocument
@@ -46,7 +47,7 @@ class MongoClientSpec extends FlatSpec with Matchers with MockFactory {
4647
mongoClient.settings.getClusterSettings.getHosts().asScala.head shouldBe serverAddress
4748
}
4849

49-
it should "accept MongoClientOptions" in {
50+
it should "accept MongoClientSettings" in {
5051
val serverAddress = new ServerAddress("localhost", 27020)
5152
val clusterSettings = ClusterSettings.builder().hosts(List(serverAddress).asJava).build()
5253
val mongoClient = MongoClient(MongoClientSettings.builder().clusterSettings(clusterSettings).build())

driver/src/test/scala/org/mongodb/scala/MongoCollectionSpec.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
package org.mongodb.scala
18+
1819
import scala.collection.JavaConverters._
1920

2021
import org.bson.BsonDocument

0 commit comments

Comments
 (0)