diff --git a/core/src/main/java/org/kohsuke/stapler/RequestImpl.java b/core/src/main/java/org/kohsuke/stapler/RequestImpl.java index 00a51beb24..6cef11c790 100644 --- a/core/src/main/java/org/kohsuke/stapler/RequestImpl.java +++ b/core/src/main/java/org/kohsuke/stapler/RequestImpl.java @@ -734,16 +734,25 @@ public Object convertJSON(Object o) { if (l==null) {// single value conversion Converter converter = Stapler.lookupConverter(type); - if (converter==null) + if (converter == null) { + if (type == Object.class) { + return o; + } throw new IllegalArgumentException("Unable to convert to "+type); + } return converter.convert(type,o); } else {// single value in a collection Converter converter = Stapler.lookupConverter(l.itemType); - if (converter==null) - throw new IllegalArgumentException("Unable to convert to "+l.itemType); - - l.add(converter.convert(l.itemType, o)); + if (converter == null) { + if (l.itemType == Object.class) { + l.add(o); + } else { + throw new IllegalArgumentException("Unable to convert to "+l.itemType); + } + } else { + l.add(converter.convert(l.itemType, o)); + } return l.toCollection(); } } diff --git a/core/src/test/java/org/kohsuke/stapler/RequestImplTest.java b/core/src/test/java/org/kohsuke/stapler/RequestImplTest.java index de9f985e42..b7e8242e4d 100644 --- a/core/src/test/java/org/kohsuke/stapler/RequestImplTest.java +++ b/core/src/test/java/org/kohsuke/stapler/RequestImplTest.java @@ -2,6 +2,7 @@ * The MIT License * * Copyright (c) 2015, CloudBees, Inc. + * Copyright (c) 2020, Nikolas Falco * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -28,23 +29,75 @@ import org.apache.http.entity.mime.MultipartEntityBuilder; import org.junit.Assert; import org.junit.Test; +import org.jvnet.hudson.test.Issue; +import org.mockito.Mockito; + +import net.sf.json.JSONObject; import javax.servlet.ReadListener; +import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; +import java.util.List; /** * @author tom.fennelly@gmail.com + * @author Nikolas Falco */ public class RequestImplTest { + public static class SetterObject { + private List choices; + + @DataBoundConstructor + public SetterObject() { + choices = new ArrayList(); + } + + @SuppressWarnings("unchecked") + @DataBoundSetter + public void setChoices(Object choices) { + if (choices instanceof String) { + for (String choice : ((String) choices).split("\n")) { + this.choices.add(choice); + } + } else { + this.choices = (List) choices; + } + } + + public List getChoices() { + return choices; + } + + } + + @Issue("JENKINS-61438") + @Test + public void verify_JSON_bind_work_with_setter_that_accept_object_type() throws Exception { + final Stapler stapler = new Stapler(); + stapler.setWebApp(new WebApp(Mockito.mock(ServletContext.class))); + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + RequestImpl req = new RequestImpl(stapler, request, Collections.emptyList(), null); + + JSONObject json = new JSONObject(); + json.put("$class", SetterObject.class.getName()); + json.put("choices", "1\n2\n3"); + + SetterObject o = req.bindJSON(SetterObject.class, json); + Assert.assertEquals(o.getChoices(), Arrays.asList("1", "2", "3")); + } + @Test public void test_multipart_formdata() throws IOException, ServletException { final Stapler stapler = new Stapler();