How do you properly bind a list of objects into a Grails command?

James McMahon picture James McMahon · Sep 5, 2013 · Viewed 8.6k times · Source

I'm trying to figure out how to deserialize and validate nested objects in JSON request into a Grails 2.1.1 command object.

Currently I have a command object in my controller that has a few basic properties and then list of domain objects,

protected static class CustomCommand {
    String name
    String description
    List<DomainObject> objs
}

And a JSON body to my POST request,

{
    name: 'test name',
    description: 'test description',
    objs: [
        {
            name: 'test sub object',
            description: 'test description'
        }
    ]
}

I'm seeing the command object created with an empty array. Any idea how I can get the sub objects my in JSON body to deserialize into the command object and then validate them?

Previously I've worked around this by manually creating an object from the parameter map and validating that directly, but that feels like a workaround that isn't taking advantage of everything Grails offers.

Answer

micha picture micha · Sep 5, 2013

We had a similiar issue with binding post data to a list inside a command. Our workaround that worked was to define a default value for collection elements:

class MyCommand {    
    List<MyClass> items= [].withLazyDefault {
        new MyClass()
    }
}

After that the post data was correctly bound to the list. I think the reason is that Groovy ignores the generic type parameter of the list and does not know which object to instantiate at runtime.

I'am not sure if this works in your case but it might be worth a try

Update:

I used this a few minutes ago:

public static class MyCommand {
    String foo
    List<Bar> bars

    public String toString() {
        return "foo: " + foo + ", bars: " + bars
    }
}

public static class Bar {
    String baz
}

controller method:

def test() {
     println new MyCommand(request.JSON)
}

I posted some json using jquery:

$.ajax({
    type: "POST",
    url: '...',
    data: JSON.stringify({ 
        'foo': '12345', 
        bars: [
            {baz: '1'}, 
            {baz: '2'}
        ]
    }),
    contentType : 'application/json',
});

The output printed by the controller:

foo: 12345, bars: [[baz:1], [baz:2]]

So it seems to work :o