You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Lukasz Lenart (Jira)" <ji...@apache.org> on 2022/09/15 06:34:00 UTC

[jira] [Closed] (WW-4216) Error in XWorkBasicConverter.convertValue()

     [ https://issues.apache.org/jira/browse/WW-4216?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Lukasz Lenart closed WW-4216.
-----------------------------
    Resolution: Not A Problem

> Error in XWorkBasicConverter.convertValue()
> -------------------------------------------
>
>                 Key: WW-4216
>                 URL: https://issues.apache.org/jira/browse/WW-4216
>             Project: Struts 2
>          Issue Type: Bug
>    Affects Versions: 2.3.15.1
>            Reporter: Harigopal Patel
>            Priority: Major
>             Fix For: 6.1.0
>
>
> if multiple name parameter in action then create string array
> ex :- abc.action?id=4&id=4&name=xyz
> Object value = string array [0=4,1=4]
> Class toType = int
> 2.0.7 branch :- no check Primitive data type
> {code:java}
> public Object convertValue(Map context, Object o, Member member, String s, Object value, Class toType) {
>         Object result = null;
>         if (value == null || toType.isAssignableFrom(value.getClass())) {
>             return value;
>         }
>         if (toType == String.class) {
>             Class inputType = value.getClass();
>             if (Number.class.isAssignableFrom(inputType)) {
>                 result = doConvertFromNumberToString(context, value, inputType);
>                 if (result != null) {
>                     return result;
>                 }
>             }
>             result = doConvertToString(context, value);
>         } else if (toType == boolean.class) {
>             result = doConvertToBoolean(value);
>         } else if (toType == Boolean.class) {
>             result = doConvertToBoolean(value);
>         } else if (toType.isArray()) {
>             result = doConvertToArray(context, o, member, s, value, toType);
>         } else if (Date.class.isAssignableFrom(toType)) {
>             result = doConvertToDate(context, value, toType);
>         } else if (Collection.class.isAssignableFrom(toType)) {
>             result = doConvertToCollection(context, o, member, s, value, toType);
>         } else if (toType == Character.class) {
>             result = doConvertToCharacter(value);
>         } else if (toType == char.class) {
>             result = doConvertToCharacter(value);
>         } else if (Number.class.isAssignableFrom(toType)) {
>             result = doConvertToNumber(context, value, toType);
>         } else if (toType == Class.class) {
>             result = doConvertToClass(value);
>         }
>         if (result == null) {
>             if (value instanceof Object[]) {
>                 Object[] array = (Object[]) value;
>                 if (array.length >= 1) {
>                     value = array[0]; /************ call this code value = 4 ****************/
>                 }
>                 result = convertValue(context, o, member, s, value, toType);
>             } else if (!"".equals(value)) { // we've already tried the types we know
>                 result = super.convertValue(context, value, toType);
>             }
>             if (result == null && value != null && !"".equals(value)) {
>                 throw new XWorkException("Cannot create type " + toType + " from value " + value);
>             }
>         }
>         return result;
>     }
> {code}
> 2.1.2 branch :- check Primitive data type
> {code:java}
> public Object convertValue(Map<String, Object> context, Object o, Member member, String propertyName, Object value, Class toType) {
>         Object result = null;
>         if (value == null || toType.isAssignableFrom(value.getClass())) {
>             return value;
>         }
>         if (toType == String.class) {
>             Class inputType = value.getClass();
>             if (Number.class.isAssignableFrom(inputType)) {
>                 result = doConvertFromNumberToString(context, value, inputType);
>                 if (result != null) {
>                     return result;
>                 }
>             }
>             result = doConvertToString(context, value);
>         } else if (toType == boolean.class) {
>             result = doConvertToBoolean(value);
>         } else if (toType == Boolean.class) {
>             result = doConvertToBoolean(value);
>         } else if (toType.isArray()) {
>             result = doConvertToArray(context, o, member, propertyName, value, toType);
>         } else if (Date.class.isAssignableFrom(toType)) {
>             result = doConvertToDate(context, value, toType);
>         } else if (Calendar.class.isAssignableFrom(toType)) {
>             result = doConvertToCalendar(context, value);
>         } else if (Collection.class.isAssignableFrom(toType)) {
>             result = doConvertToCollection(context, o, member, propertyName, value, toType);
>         } else if (toType == Character.class) {
>             result = doConvertToCharacter(value);
>         } else if (toType == char.class) {
>             result = doConvertToCharacter(value);
>         } else if (Number.class.isAssignableFrom(toType) || toType.isPrimitive()) {
> /************ call this code because toType is a int data type but throws error because value is a string array[0=4,1=4] ****************/
>             result = doConvertToNumber(context, value, toType);
>         } else if (toType == Class.class) {
>             result = doConvertToClass(value);
>         }
>         if (result == null) {
>             if (value instanceof Object[]) {
>                 Object[] array = (Object[]) value;
>                 if (array.length >= 1) {
>                     value = array[0];
>                 } else {
>                     value = null;
>                 }
>                 result = convertValue(context, o, member, propertyName, value, toType);
>             } else if (!"".equals(value)) { // we've already tried the types we know
>                 result = super.convertValue(context, value, toType);
>             }
>             if (result == null && value != null && !"".equals(value)) {
>                 throw new XWorkException("Cannot create type " + toType + " from value " + value);
>             }
>         }
>         return result;
>     } 
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)