Skip to content

Scala: Ensure implicit conversion keeps type fidelity #627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions driver-scala/src/main/scala/org/mongodb/scala/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package org.mongodb

import _root_.scala.language.implicitConversions
import _root_.scala.reflect.ClassTag
import org.bson.BsonDocumentReader
import org.bson.codecs.{ DecoderContext, DocumentCodec }
import org.mongodb.scala.bson.BsonDocument
import org.mongodb.scala.internal.WriteConcernImplicits

import _root_.scala.language.implicitConversions
import _root_.scala.reflect.ClassTag

/**
* The MongoDB Scala Driver package
*
Expand Down Expand Up @@ -392,9 +395,12 @@ package object scala extends ClientSessionImplicits with ObservableImplicits wit

implicit def bsonDocumentToDocument(doc: BsonDocument): Document = new Document(doc)

implicit def bsonDocumentToUntypedDocument(doc: BsonDocument): org.bson.Document =
org.bson.Document.parse(doc.toJson())
implicit def documentToUntypedDocument(doc: Document): org.bson.Document =
bsonDocumentToUntypedDocument(doc.underlying)

implicit def documentToUntypedDocument(doc: Document): org.bson.Document = org.bson.Document.parse(doc.toJson())
private lazy val DOCUMENT_CODEC = new DocumentCodec()
implicit def bsonDocumentToUntypedDocument(doc: BsonDocument): org.bson.Document = {
DOCUMENT_CODEC.decode(new BsonDocumentReader(doc), DecoderContext.builder().build())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
package org.mongodb.scala

import java.util.concurrent.TimeUnit

import _root_.scala.concurrent.duration.Duration

import com.mongodb.{ MongoCredential => JMongoCredential }

import org.bson.BsonDocumentWrapper
import org.bson.codecs.DocumentCodec
import org.mongodb.scala
import org.mongodb.scala.bson.BsonString
import org.mongodb.scala.MongoClient.DEFAULT_CODEC_REGISTRY
import org.mongodb.scala.bson._
import org.mongodb.scala.model._
import org.scalatest.{ FlatSpec, Matchers }

class ScalaPackageSpec extends BaseSpec {

Expand Down Expand Up @@ -66,9 +65,9 @@ class ScalaPackageSpec extends BaseSpec {

it should "be able to create Documents" in {
val doc = Document("a" -> BsonString("1"))
val doc2 = org.mongodb.scala.bson.collection.Document("a" -> BsonString("1"))
val doc2 = collection.Document("a" -> BsonString("1"))

doc shouldBe a[org.mongodb.scala.bson.collection.immutable.Document]
doc shouldBe a[collection.immutable.Document]
doc should equal(doc2)
}

Expand Down Expand Up @@ -141,4 +140,33 @@ class ScalaPackageSpec extends BaseSpec {
val javaCredential5 = JMongoCredential.createGSSAPICredential("userName")
scalaCredential5 should equal(javaCredential5)
}

it should "implicitly convert to org.bson.document with type fidelity" in {

val bsonDocument = Document(
"null" -> BsonNull(),
"int32" -> BsonInt32(32),
"int64" -> BsonInt64(Long.MaxValue),
"decimal128" -> BsonDecimal128(128.1),
"boolean" -> BsonBoolean(true),
"date" -> BsonDateTime(123456789),
"double" -> BsonDouble(1.1),
"string" -> BsonString("String"),
"minKey" -> BsonMinKey(),
"maxKey" -> BsonMaxKey(),
"javaScript" -> BsonJavaScript("function () {}"),
"objectId" -> BsonObjectId(),
"codeWithScope" -> BsonJavaScriptWithScope("function () {}", Document()),
"regex" -> BsonRegularExpression("/(.*)/"),
"symbol" -> BsonSymbol(Symbol("sym")),
"timestamp" -> BsonTimestamp(),
"undefined" -> BsonUndefined(),
"binary" -> BsonBinary(Array[Byte](128.toByte)),
"array" -> BsonArray(List("a", "b", "c")),
"document" -> Document("a" -> 1, "b" -> List(1, 2, 3))
)

val document: org.bson.Document = bsonDocument
BsonDocumentWrapper.asBsonDocument(document, DEFAULT_CODEC_REGISTRY) should equal(bsonDocument.underlying)
}
}