[
https://issues.apache.org/jira/browse/BEANUTILS-409?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13590068#comment-13590068
]
Niall Pemberton commented on BEANUTILS-409:
-------------------------------------------
The reason behind this type of behavior is because of the origin of BeanUtils
in the Struts 1 project - the populate() method was developed to convert a Map
of request parameters into a bean. Request parameters can have multiple values
- but Struts only used the first (multiple values were handled through a
parameter name convention):
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterValues(java.lang.String)
The describe() method suffers from this behaviour because it utilizes the same
methods (ultimately the BeanUtilsBean.convert(Object) method.
Work to improve the capabilities of the Converters and enhance BeanUtils to use
these new facilities came later (relatively!) in the 1.8.x series - but we
tried to retain backward compatibility for Struts 1 users.
There is a mechanism to change this behaviour in place in BeanUtils 1.8.3 -
sorry its not very elegant.
First you have to register BeanUtilsBean2 as the BeanUtils bean:
{code}
BeanUtilsBean.setInstance(new BeanUtilsBean2());
{code}
This causes the ConvertUtilsBean2 to be used and all the conversion is
delegated to the registered converters. However the default behavior for the
registered array converters when converting to a String is the old behavior of
just taking the first value. This can be changed using the
setOnlyFirstToString(false) - but you have to do that for each array converter.
For example, for String arrays you would do:
{code}
ArrayConverter stringArrayConverter = new ArrayConverter(String[].class, new
StringConverter(), 0);
stringArrayConverter.setOnlyFirstToString(false);
ConvertUtils.register(stringArrayConverter, String[].class);
{code}
This is cumbersome to do for every array converter so it would probably be a
good idea to expose a method which can do this for all the default array
converters that are registered.
> BeanUtils - 'describe' method returning Incorrect array value
> -------------------------------------------------------------
>
> Key: BEANUTILS-409
> URL: https://issues.apache.org/jira/browse/BEANUTILS-409
> Project: Commons BeanUtils
> Issue Type: Bug
> Affects Versions: 1.8.3
> Environment: commons-beanutils 1.8.3, jdk 1.6.0_20
> Reporter: benny
> Assignee: Benedikt Ritter
> Priority: Critical
> Labels: describe
> Fix For: 1.8.4
>
> Attachments: BEANUTILS-409-Test.patch
>
>
> I want to convert a bean class to a map (key=the name of the member,value=the
> value of the member).
> I'm using the method BeanUtils.describe(beanClass);
> (I'm using commons-beanutils 1.8.3, jdk 1.6.0_20, on commons-beanutils 1.5 it
> works)
> The problem is that the return value is incorrect, (the map contain only the
> first item from the array),
> the code:
> public class Demo {
> private ArrayList<String> myList = new ArrayList<String>();
> public Demo() {
> myList.add("first_value");
> myList.add("second_value");
> }
>
> public ArrayList<String> getMyList() {
> return myList;
> }
>
> public void setMyList(ArrayList<String> myList) {
> this.myList = myList;
> }
>
> public static void main(String[] args) {
> Demo myBean = new Demo();
> try {
> Map describe = BeanUtils.describe(myBean);
> Iterator it = describe.entrySet().iterator();
> while (it.hasNext()) {
> Map.Entry pairs = (Map.Entry) it.next();
> System.out.println(String.format("key=%s,value=%s",
> (String) pairs.getKey(), (String) pairs.getValue()));
>
> }
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> }
> •The expected output:
>
> key=myList,value=[first_value,second_value]
> key=class,value=class $Demo
> •But the real output is:
>
> key=myList,value=[first_value]
> key=class,value=class $Demo
> As you can see the array contains two values but the output(and the map)
> contains only one,why??
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira