sitebricks icon indicating copy to clipboard operation
sitebricks copied to clipboard

List conversion does not honor specified type

Open juraj-blahunka opened this issue 13 years ago • 5 comments

Conversion succeeds, but objects stored in ids list are String instances "1", "2", "3" not 1L, 2L, 3L.

private Form {
    List<Long> ids = Lists.newArrayList();
    .. setters, getters
}

<form>
    <input type="text" name="ids" value="1" />
    <input type="text" name="ids" value="2" />
    <input type="text" name="ids" value="3" />
</form>

juraj-blahunka avatar Jul 26 '11 09:07 juraj-blahunka

There is a type cast exception when use ArrayList Long

wuhaixing avatar Jul 27 '11 03:07 wuhaixing

When I replace List<Long> with ArrayList<Long>, no exception gets raised

I'm using sitebricks 0.8.5

I'm getting ClassCastException only when I explicitly iterate over the posted collection (String cannot be Long), however when there is no usage of the ids collection inside the Form instance, only in the the template, no exception is thrown.

for (Long ex : ids) {
    System.out.println(ex);
}

juraj-blahunka avatar Jul 27 '11 07:07 juraj-blahunka

I think following code from mvel org.mvel2.PropertyAccessor is the exception source:

if (curr instanceof Map) {
            //noinspection unchecked
            ((Map) curr).put(eval(ex, this.ctx, this.variableFactory), value);
          }
          else if (curr instanceof List) {
            //noinspection unchecked
            ((List) curr).set(eval(ex, this.ctx, this.variableFactory, Integer.class), value);
          }
          else if (hasPropertyHandler(curr.getClass())) {
            getPropertyHandler(curr.getClass()).setProperty(ex, ctx, variableFactory, value);
          }
          else if (curr.getClass().isArray()) {
            Array.set(curr, eval(ex, this.ctx, this.variableFactory, Integer.class), convert(value, getBaseComponentType(curr.getClass())));
          }

But I'm not sure yet

wuhaixing avatar Jul 27 '11 08:07 wuhaixing

I just add a test to PropertyAccessTests in MVEL


  public class Datas {
         private List<Long> ids = new ArrayList<Long>();
         public void setIds(List<Long> ids) {
             this.ids = ids;
         }
         public List<Long> getIds() {
             return this.ids;
         }

  }
  public void testBindingConvert() {

      Datas datas = new Datas();
      List values = new ArrayList();
      values.add("1");
      values.add("2");
      values.add("3");
      MVEL.setProperty(datas, "ids", values);

      assertEquals(new Long(1),datas.getIds().get(0));

  }

and got this:

testBindingConvert(org.mvel2.tests.core.PropertyAccessTests): expected:<1> but was:<1>

wuhaixing avatar Jul 27 '11 14:07 wuhaixing

Have you tried registering a converter? Is this issue still happening?

dhanji avatar Apr 20 '12 23:04 dhanji