Search code examples
inheritancegrailspolymorphismjson-view

Grails 3 : Class inheritance not working with json-views


I'm trying to render json-views from a result of a polymorphic query. With object marshallers, it works perfectly, but not with grails3 json-views. I tried all sort of things, it seems like a bug in json-views plugin.

Here is an example to clarify the problem:

class Content {
 String text
}

class BlogEntry extends Content {
    URL url
}

class Book extends Content {
    String ISBN
}

And in contentController :

class ContentController extends RestfulController<Content> {

    static responseFormats = ['json', 'xml']

    ContentController() {
        super(Content)
    }

    def index(Integer max) {
        println '*CONTENT:INDEX*'
        def contents= Content.findAll()
        respond contents 
    }
}

json views:

// /content/index.gson
model {
    Iterable<Content> contentList
}

json tmpl.content(contentList ?: [])

// /content/_content.gson
model {
    Content content
}

json g.render(content)

I get an empty result from the controller's index method. i'm using grails 3.2.0.RC2 with views-json:1.1.0.RC3

With custom object-marshallers, i can get json result by subclass type (BlogEntry, Book) which is the result expected.


Solution

  • Resolved in grails-views v1.1.3.

    https://github.com/grails/grails-views/releases/tag/v1.1.3