Warnings following upgrade of play2-reactivemongo from version 0.18.4 to 0.19.5

719 Views Asked by At

We are developing an application based on Play Framework (Scala) and storing data into a Mongo database using ReactiveMongo.

After upgrading play2-reactivemongo from version 0.18.4 to 0.19.5, we obtain a lot of deprecation warnings and I can understand how to fix them.

Here is the deprecation warning I am getting:

package object json in package json is deprecated (since 0.19.2): Will be replaced `reactivemongo.play.json.compat._` from `reactivemongo-play-json-compat`

For example, I have a Nonce object:

package models

import java.security.SecureRandom
import java.util.Base64

import org.joda.time.DateTime
import play.api.libs.json._
import reactivemongo.bson.{ BSONDateTime, BSONObjectID }
case class Nonce(_id: Option[BSONObjectID], value: String, expiresAt: BSONDateTime, ttl: Option[Int])

object Nonce {

  import reactivemongo.play.json._

  // Generate a Nonce object with a value matching the base64url encoding without the trailing '=' as defined section two of [RFC7515]
  def apply(entropy: Int, ttl: Int): Nonce = {
    val pattern = "([^=]*).*".r
    val random = new SecureRandom()
    val input = new Array[Byte](entropy)
    random.nextBytes(input)
    val pattern(nonce) = Base64.getUrlEncoder.encodeToString(input)
    Nonce(None, nonce, BSONDateTime(DateTime.now.plusSeconds(ttl).getMillis), Some(ttl))
  }

  implicit val nonceFormat: OFormat[Nonce] = Json.format[Nonce]

}

And a service class to store the Nonce object:

package models

import javax.inject.{ Inject, Singleton }
import play.api.Configuration
import play.modules.reactivemongo.ReactiveMongoApi
import reactivemongo.api.WriteConcern
import reactivemongo.api.commands.WriteResult
import reactivemongo.api.indexes.{ Index, IndexType }
import reactivemongo.bson.BSONDocument
import reactivemongo.play.json._
import reactivemongo.play.json.collection.JSONCollection

import scala.concurrent.{ ExecutionContext, Future }
@Singleton
class NonceService @Inject() (implicit ec: ExecutionContext, configuration: Configuration, reactiveMongoApi: ReactiveMongoApi) {

  // Specifying a TTL on the nonces collection for the element expiresAt
  private val ttlIndex = Index(Seq("expiresAt" -> IndexType.Ascending), name = Some("expiresAt_"), options = BSONDocument("expireAfterSeconds" -> 0))

  val noncesCollection = reactiveMongoApi.database.map(_.collection[JSONCollection]("nonces"))

  def add(nonce: Nonce): Future[WriteResult] = {
    noncesCollection.flatMap { col =>
      col.insert(ordered = false).one(nonce) andThen { case _ => col.indexesManager.ensure(ttlIndex) }
    }
  }

  def get(value: String): Future[Option[Nonce]] = {
    val query = BSONDocument("value" -> value)
    noncesCollection.flatMap(_.findAndRemove(query, None, None, WriteConcern.Journaled, None, None, Seq.empty).map(_.result[Nonce]))
  }

}

The compiler indicates the following warnings:

[warn] ... Nonce.scala:26:57: package object json in package json is deprecated (since 0.19.2): Will be replaced `reactivemongo.play.json.compat._` from `reactivemongo-play-json-compat`
[warn]   implicit val nonceFormat: OFormat[Nonce] = Json.format[Nonce]
[warn] ...NonceService.scala:31:45: package object json in package json is deprecated (since 0.19.2): Will be replaced `reactivemongo.play.json.compat._` from `reactivemongo-play-json-compat`
[warn]     noncesCollection.flatMap(_.findAndRemove(query, None, None, WriteConcern.Journaled, None, None, Seq.empty).map(_.result[Nonce]))
[warn]                                             ^
1

There are 1 best solutions below

1
On

'reactivemongo-play-json-compat' is a library. You need to add it to you build.sbt. for play 2.5 it would be

"org.reactivemongo" %% "reactivemongo-play-json-compat" % "0.20.3-play25" 

Then remove the json import from your file

import reactivemongo.play.json._

and replace with

import reactivemongo.play.json.compat._

That worked for me