Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4216

Error in XWorkBasicConverter.convertValue()

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.3.15.1
    • Fix Version/s: 2.5.x
    • Component/s: None
    • Labels:
      None

      Description

      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

      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;
          }
      

      2.1.2 branch :- check Primitive data type

      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;
          } 
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              patel.harigopal@gmail.com Harigopal Patel
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: