mandubian/play-siena — Issue #5: Cannot use Siena model objects with Query fields on Groovy templates
Author: dmoebius
Created: 2011-10-14 21:38:54+00:00
State: open

Consider the following model class:

```java
@Table("units")
public class Unit extends Model {
@Id public Long id;

@Filter("unit")
public Query

entries;
}
```

If you try to put an instance of this class into the params map and use it in a Groovy/HTML template:

```java
public static void edit(Long unitId) {
Unit unit = Unit.getByKey(unitId);
notFoundIfNull(unit, "unit " + unitId);
render(unit);
}
```

#{extends 'main.html' /}
#{set title:'Edit Unit ${unit.name}' /}

Edit Unit ${unit.name}

then Play fails to serialize ("unbind") the class because it runs into an infinite recursion when trying to serialize the Query field. Eventually it throws a StackOverflowException.

The reason is because Play serializes the Unit instance into a map of json keys like this:

unit.entries.obj = ...
unit.entries.obj.entries = ...
unit.entries.obj.entries.obj = ...
unit.entries.obj.entries.obj.entries = ...
unit.entries.obj.entries.obj.entries.obj = ...

... and so on forever.

A solution for the play-siena plugin might be to override the unBind() method in SienaPlugin to ignore Query fields altogether:

```java
@Override
public Map unBind(Object src, String name) {
if (siena.Query.class.isAssignableFrom(src.getClass())) {
return new HashMap();
}
return null;
}
```

Please evaluate.