Hussain Fakhruddin
Hussain Fakhruddin

Reputation: 3272

Grails JSON convertor is not serialising embedded child objects

I upgraded my project from grails 2.3.4 to 2.3.8 and conversion of embedded JSON objects stopped working

I'm using MongoDB as the only persistence.

Following is my code structure.


// Post class
class Post {
    String id
    String title
    String content
    List comments

    static embedded = ['comments']
    static hasMany = [comments: Comment]

}

//Comments class
class Comment {
    String name
    String email
    String website
    String content
}

//code in controller
class PostController {
    def show() {
        def postInstance = Post.collection.findOne(title: id)
        postInstance = postInstance as Post
        log.info "Post comments with id {postInstance.comments.name}"

        respond postInstance
    }
}

//log details
INFO Post comments with id [Hussain1, Hussain2]

//JSON Response
{
    "class": "com.Post",
    "id": "5364be6703647a4cd37dd293",
    "comments": [
        {
            "class": "com.Comment",
            "id": null
        },
        {
            "class": "com.Comment",
            "id": null
        }
    ],
    "content": "Content",
    "title": "This-is-a-title"
}

Any idea why the child objects in JSON objects are not coming properly similar thing was working in Grails 2.3.4

Upvotes: 1

Views: 265

Answers (1)

Graeme Rocher
Graeme Rocher

Reputation: 7985

You’re rendering the BSONObject from MongoDB rather than the domain instance, is that intentional? If so check the data you actually have stored in your Post document collection. Otherwise file a jira at http://jira.grails.org/browse/GPMONGODB and attach an example that reproduces the problem.

Upvotes: 1

Related Questions