Unable to save embedded objects in mongodb using gorm(typeMismatch error)

209 views Asked by At

I am trying to create and save a domain object using gorm and mongodb plugin, but I always get type mismatch error on the embedded property("emailTemplates").

My domain is defined as :

import org.apache.commons.mail.Email
import org.bson.types.ObjectId

class EmailTemplate {

    ObjectId id
    String subject
    String message
    String locale
    String from
    List<String> to
    List<String> cc
    List<String> bcc

    static mapWith = "mongo"

    static constraints = {
        subject(blank: false, maxSize: 78)
        message(blank: false)
        locale(blank: false)
    }

    static mapping = {
        locale defaultValue: "en_US"
    }
}

class Template {

    ObjectId id
    String namespace
    String name
    String description
    String defaultLocale
    Boolean enabled
    List emailTemplates

    static mapWith = "mongo"
    static hasMany = [emailTemplates: EmailTemplate]
    static embedded = ["emailTemplates"]
    static constraints = {
        name(unique: ['namespace'], blank: false, maxSize: 250)
        description(blank: true)
        enabled(blank: false)
        namespace(blank: false)
        defaultLocale(blank: false)
    }
    static mapping = {
        id attr: "_id"
        defaultLocale defaultValue: "en_US"
        enabled defaultValue: true
    }

}

And my POST payload json is:

{
  "namespace": "some namespave",
  "emailTemplates": [
    {
      "locale": "en_US",
      "from": "[email protected]",
      "message": "test",
      "subject": "test"
    }
  ],
  "name": "a name",
  "defaultLocale": "en_US",
  "enabled": true,
  "description": "test"
}

Right now I do have a workaround where I override the "save" method on the controller(which extends RestfulController) and manually instantiate the domain objects, but I would rather have the default "RestfulController" to work.

I am using mongodb plugin version 6.1.0, mongodb-driver:3.4.2 and my grails version is 3.2.8

0

There are 0 answers