1

I am using play-framework with Reactive mongo drivers. For handling reactive mongo BSONObjectId in our routes file, i am creating following binders:

object StringToBSONObjectIdBinder {

 /* This is for Path Parameter*/

 implicit object pathBindableBSONObjectID extends play.api.mvc.PathBindable.Parsing[BSONObjectID](
  BSONObjectID(_), _.stringify,
  (key: String, e: Exception) =>
   "Cannot parse parameter %s as BSONObjectID: %s".format(key, e.getMessage))

 /* This is for query String*/

 implicit object queryStringBindableBSONObjectID extends play.api.mvc.QueryStringBindable.Parsing[BSONObjectID](
  BSONObjectID(_), _.stringify,
  (key: String, e: Exception) =>
   "Cannot parse parameter %s as BSONObjectID: %s".format(key, e.getMessage))
}

In routing i am easily route my id as a path param like following example:

GET      /company/:companyId/users-detail       controllers.CompanyController.userDetail(companyId: BSONObjectID)

My BSONObjectId easily mapped with my request handler path param. But when i am using following route after above route as below:

GET /company/detail        controllers.CompanyController.companyDetail

I am getting following BadRequest:

    For request 'GET /company/detail?t=1466673779753' [Cannot parse parameter companyId as BSONObjectID: wrong ObjectId: 'teams']

But when i switch the routes as below:

GET /company/detail        controllers.CompanyController.companyDetail
GET      /company/:companyId/users-detail       controllers.CompanyController.userDetail(companyId: BSONObjectID)

Services run successfully. I am still no getting what actual problem is. This is play-framework issue, or something wrong with my code?

cchantep
  • 9,118
  • 3
  • 30
  • 41
Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126

1 Answers1

2

First you re-implement a QueryBindable, whereas the BSON one is already provided by the Play plugin: see sample

Then you pass the value "teams", which is not a valid representation for BSONObjectID.

cchantep
  • 9,118
  • 3
  • 30
  • 41