History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: WW-596
Type: Bug Bug
Status: Closed Closed
Resolution: Not A Problem
Priority: Minor Minor
Assignee: Patrick Lightbody
Reporter: KaChun Ng
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
WebWork

IllegalArgumentException when setting an indexed property

Created: 20/Jul/04 01:37 AM   Updated: 03/Jul/05 11:43 AM
Component/s: Expression Language
Affects Version/s: 2.0
Fix Version/s: 2.2


 Description  « Hide
I am using WW 2.0 and I need to let the user input a 2 dimensional array of
values (String[][] amount), while the size of the dimensions are determined at
runtime.
To do this I create textfields in my jsp using two iterators to produce
indexed names like: 'amount[0][0]', 'amount[0][1]', ..., 'amount[1][0]', ...

When the user submits his input, Ognl raises an IllegalArgumentException:

java.lang.IllegalArgumentException: array element type mismatch
        at java.lang.reflect.Array.set(Native Method)
        at ognl.ArrayPropertyAccessor.setProperty(ArrayPropertyAccessor.java:120)
        at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1460)
        at ognl.ASTProperty.setValueBody(ASTProperty.java:105)
        at ognl.SimpleNode.setValue(SimpleNode.java:215)
        at ognl.ASTChain.setValueBody(ASTChain.java:172)
        at ognl.SimpleNode.setValue(SimpleNode.java:215)
        at ognl.Ognl.setValue(Ognl.java:478)
        at com.opensymphony.xwork.util.OgnlValueStack.setValue(OgnlValueStack.java:83)
        at com.opensymphony.xwork.util.OgnlValueStack.setValue(OgnlValueStack.java:74)
        at
com.opensymphony.xwork.interceptor.ParametersInterceptor.before(ParametersInterceptor.java:44)

After carefully looking, I saw that the problem was that the submitted
parameter values were String arrays with length 1. This may be the problem that Ognl can't set the array String element with a String array.

I try in the debugger to set a single String value instead of an array String in ParametersInterceptor:

this line=> stack.setValue(entry.getKey().toString(), entry.getValue());

Now I see that the right value is set in the property amount.



 All   Comments   Change History      Sort Order:
KaChun Ng - [20/Jul/04 02:39 AM ]
I have post the following message to the ogn-developer mailing list:

-------------

Hi,

I am using Ognl in combination with WebWork 2.0 and encounter a problem when
setting an indexed named property, for example 'amount[0]', after a user
submit. WebWork 2.0 is using Ognl 2.6.3.

WebWork tries to set 'amount[0]' with a String array, because HTTP allows
multiple values using the same parameter name. It is using Ognl to do this
job, but then Ognl is throwing an IllegalArgumentException:

java.lang.IllegalArgumentException: array element type mismatch
        at java.lang.reflect.Array.set(Native Method)
        at ognl.ArrayPropertyAccessor.setProperty(ArrayPropertyAccessor.java:120)
        at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1460)
        at ognl.ASTProperty.setValueBody(ASTProperty.java:105)
        at ognl.SimpleNode.setValue(SimpleNode.java:215)
        at ognl.ASTChain.setValueBody(ASTChain.java:172)
        at ognl.SimpleNode.setValue(SimpleNode.java:215)
        at ognl.Ognl.setValue(Ognl.java:478)
        at
com.opensymphony.xwork.util.OgnlValueStack.setValue(OgnlValueStack.java:83)
        at
com.opensymphony.xwork.util.OgnlValueStack.setValue(OgnlValueStack.java:74)
        at
com.opensymphony.xwork.interceptor.ParametersInterceptor.before(ParametersInterceptor.java:44)


If the property name is not indexed, the custom TypeConvertor inside WebWork
is invoked and they handle the String array conversion properly by returning
the first value in the array if the array length is 1. But setting an indexed
named property never triggers the TypeConvertor.

After debugging through the sources of both WebWork and Ognl, I see that the
ArrayPropertyAccessor will just do an Array.set call to set the array element
and that is the source of the problem. A non-indexed property name will use
the ObjectPropertyAccessor which eventually will use the TypeConvertor to do
the conversion, but for an array this never happens.

I looked in the sources of bith 3.0.0 and 2.6.5 Ognl versions and this problem
still exist in both versions. Array.set is called if the index is a number.

A solution is to catch the IllegalArgumentException and invoke the setProperty
method in its super class (ObjectPropertyAccessor in 2.6.3) if this happens.
Or check the type of the value first before setting the value and then pass
the call to the setProperty in the super class if the type does not match.

KaChun Ng - [26/Jul/04 10:57 AM ]
I got the following response form Drew of Ognl and hopefully the next release of Ognl is coming soon:

This is indeed a problem. From your description it is a bug in the way
that ValueConverters are called. I will add this usage as a test case
for both 2.6.6 and 3.0.0.

Thank you for the thoroughness of your investigation. It helps quite a
bit to understand both the technical problem and the use case.

- Drew

KaChun Ng - [08/Aug/04 05:53 PM ]
Problem solved in Ognl release 2.6.6

Patrick Lightbody - [03/Jul/05 11:43 AM ]
Glad it got fixed in the latest version of OGNL.