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

List based parameters no longer work when there is only one value.

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.5.5
    • Fix Version/s: 2.5.8
    • Component/s: None
    • Labels:
      None

      Description

      If an action has a List parameter, but is only called with one value, the parameter is never set.

      Consider the basic action below.

      public class TestAction
      {
      	private List<SomeObject> object;
      
      	public String execute()
      	{
      		for (SomeObject user : object)
      		{
      			System.out.println("as list" + user.field);
      		}
      		return "success";
      	}
      
      	public static class SomeObject{
      		private String field;
      
      		public void setField(String field)
      		{
      			this.field = field;
      		}
      	}
      
      	public List<SomeObject> getObject()
      	{
      		return object;
      	}
      
      	public void setObject(List<SomeObject> object)
      	{
      		this.object = object;
      	}
      }
      

      Performing a GET on "/test.action?object.field=a"
      Expected outcome: "object" list should be populated with 1 object. with a field value of "a"
      Actual Outcome: object list is empty.

      Performing a GET on "/test.action?object.field=a&object.field=b" works as expected (list has 2 entries).

      The following changes to ParametersInterceptor break this behavior. because XWorkListPropertyAccessor Expects an Array of strings for value (which was previously sent when HttpParameters was a map instead of an Object).

      if (value instanceof Parameter.File) {
           newStack.setParameter(name, value.getObject());
      } else  if (value.isMultiple()) {
          newStack.setParameter(name, value.getMultipleValues());
      } else {
           newStack.setParameter(name, value.getValue());
      }
      

        Issue Links

          Activity

          Hide
          aleksandr-m Aleksandr Mashchenko added a comment -

          Lukasz Lenart Do we need this if in ParametersInterceptor at all? Current tests pass with just newStack.setParameter(name, value.getObject());, including the one I've created for this issue.

          Show
          aleksandr-m Aleksandr Mashchenko added a comment - Lukasz Lenart Do we need this if in ParametersInterceptor at all? Current tests pass with just newStack.setParameter(name, value.getObject()); , including the one I've created for this issue.
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          Probably not but I would like to add a new PropertyAccessor which will support HttpParameters, let me work on that.

          Show
          lukaszlenart Lukasz Lenart added a comment - Probably not but I would like to add a new PropertyAccessor which will support HttpParameters , let me work on that.
          Hide
          aleksandr-m Aleksandr Mashchenko added a comment -

          Test for this issue.

          Added private List<TestBean> beanList; with getter and setter to com.opensymphony.xwork2.SimpleAction.

          ParametersInterceptorTest:

              public void testBeanListSingleValue() throws Exception {
                  Map<String, Object> params = new HashMap<>();
                  params.put("beanList.name", new String[] { "Superman" });
          
                  HashMap<String, Object> extraContext = new HashMap<>();
                  extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
          
                  ActionProxy proxy = actionProxyFactory.createActionProxy("",
                                                      MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, null, extraContext);
                  proxy.execute();
                  SimpleAction action = (SimpleAction) proxy.getAction();
                  assertNotNull(action);
                  assertNotNull(action.getBeanList());
                  assertFalse(action.getBeanList().isEmpty());
              }
          
          Show
          aleksandr-m Aleksandr Mashchenko added a comment - Test for this issue. Added private List<TestBean> beanList; with getter and setter to com.opensymphony.xwork2.SimpleAction . ParametersInterceptorTest : public void testBeanListSingleValue() throws Exception { Map< String , Object > params = new HashMap<>(); params.put( "beanList.name" , new String [] { "Superman" }); HashMap< String , Object > extraContext = new HashMap<>(); extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build()); ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, null , extraContext); proxy.execute(); SimpleAction action = (SimpleAction) proxy.getAction(); assertNotNull(action); assertNotNull(action.getBeanList()); assertFalse(action.getBeanList().isEmpty()); }
          Hide
          lukaszlenart Lukasz Lenart added a comment -

          Aleksandr Mashchenko thanks! I have used your test case to confirm my fix, everything works

          Show
          lukaszlenart Lukasz Lenart added a comment - Aleksandr Mashchenko thanks! I have used your test case to confirm my fix, everything works
          Hide
          jira-bot ASF subversion and git services added a comment -

          Commit b5240560ab5e63e66d5108d15fc78a6622019547 in struts's branch refs/heads/master from Lukasz Lenart
          [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=b524056 ]

          WW-4702 WW-4707 Fixes issue with with list based parameters

          Show
          jira-bot ASF subversion and git services added a comment - Commit b5240560ab5e63e66d5108d15fc78a6622019547 in struts's branch refs/heads/master from Lukasz Lenart [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=b524056 ] WW-4702 WW-4707 Fixes issue with with list based parameters
          Hide
          hudson Hudson added a comment -

          SUCCESS: Integrated in Jenkins build Struts-JDK7-master #546 (See https://builds.apache.org/job/Struts-JDK7-master/546/)
          WW-4702 WW-4707 Fixes issue with with list based parameters (lukaszlenart: rev b5240560ab5e63e66d5108d15fc78a6622019547)

          • (add) core/src/main/java/com/opensymphony/xwork2/ognl/accessor/HttpParametersPropertyAccessor.java
          • (edit) core/src/main/resources/struts-default.xml
          • (edit) core/src/test/java/com/opensymphony/xwork2/SimpleAction.java
          • (edit) core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java
          • (edit) core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java
          • (add) core/src/main/java/com/opensymphony/xwork2/ognl/accessor/ParameterPropertyAccessor.java
          • (edit) core/src/main/java/com/opensymphony/xwork2/config/providers/XWorkConfigurationProvider.java
          Show
          hudson Hudson added a comment - SUCCESS: Integrated in Jenkins build Struts-JDK7-master #546 (See https://builds.apache.org/job/Struts-JDK7-master/546/ ) WW-4702 WW-4707 Fixes issue with with list based parameters (lukaszlenart: rev b5240560ab5e63e66d5108d15fc78a6622019547) (add) core/src/main/java/com/opensymphony/xwork2/ognl/accessor/HttpParametersPropertyAccessor.java (edit) core/src/main/resources/struts-default.xml (edit) core/src/test/java/com/opensymphony/xwork2/SimpleAction.java (edit) core/src/main/java/com/opensymphony/xwork2/interceptor/ParametersInterceptor.java (edit) core/src/test/java/com/opensymphony/xwork2/interceptor/ParametersInterceptorTest.java (add) core/src/main/java/com/opensymphony/xwork2/ognl/accessor/ParameterPropertyAccessor.java (edit) core/src/main/java/com/opensymphony/xwork2/config/providers/XWorkConfigurationProvider.java

            People

            • Assignee:
              lukaszlenart Lukasz Lenart
              Reporter:
              agreenfield Adam Greenfield
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development