-1

I have an application that uses gridFS to store some images for things. Im using the gridFSBodyParser and controllers work. Now I need to seed some data from disk for a test for a particular edge case, for which I cannot use the controller because reasons. I have following code and relevant imports -

import java.nio.file.{Files, Paths}
import com.cookit.allergy.Allergy
import play.api.libs.iteratee.Enumerator
import play.modules.reactivemongo.MongoController.readFileReads
import play.modules.reactivemongo.ReactiveMongoApi
import reactivemongo.play.json._
def seedTestData(....) = {
  for {
    //create actual entity
    obj <- createEntity(....)
    //create avatar for entity
    gridFS <- mongo.asyncGridFS.flatMap(gridFS => {
      val url = this.getClass.getClassLoader.getResource("test.jpg")
      gridFS.save(
        Enumerator.enumerate(List(Files.readAllBytes(Paths.get(url.getPath)))),
        gridFS.fileToSave(Some(name), Some("image/jpeg"), None, buildMD(obj)))
    }).map(result => {
      logger.info(.....)
    }).recover({
      case e: Exception =>
        logger.error(....)
        e.printStackTrace()
        throw e
    })
  } yield {
    ....
  }
}

My problem is with id generation. It complains that -

play.api.libs.json.JsResultException: JsResultException(errors:List((,List(JsonValidationError(List(List((,List(JsonValidationError(List(List((,List(JsonValidationError(List(List((,List(JsonValidationError(List(Wrong ObjectId (length != 24): '4c75f45d255045a2aac01fdbbe031c11'),WrappedArray()))))),WrappedArray()))))),WrappedArray()))))),WrappedArray())))))

I suspect it has something to do with the JSON serialization, but I cannot figure out exactly where the problem is.

cchantep
  • 9,118
  • 3
  • 30
  • 41
kot
  • 65
  • 1
  • 7
  • The error message is quite clear: `Wrong ObjectId (length != 24): '4c75f45d255045a2aac01fdbbe031c11'` ... The given value is a 32 bytes hexa string whereas Object ID should by at most 24 bytes ... – cchantep Sep 15 '20 at 00:35
  • You didn't indicate the version (enumerated are no longer use with recent gridfs api). Also, json serialization is not useful for such test, and BTW is replace by conversion to bson serialization in 1.0. – cchantep Sep 15 '20 at 07:59
  • somehow I feel like there is a comment you just responded to, that I cannot see. Curious how that happened.... reactivemongo version is 0.20. – kot Sep 16 '20 at 02:36

1 Answers1

0

Basically I ended up passing a custom FileToSave[JSONSerializationPack.type, JsValue] implementation instead of gridFS.fileToSave(....) and overriding pack, filename, contentType, uploadDate and metadata fields.

kot
  • 65
  • 1
  • 7