Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-61438] IllegalArgumentException for ChoiceParameter on bindJSON saving job configuration #182

Merged
merged 3 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions core/src/main/java/org/kohsuke/stapler/RequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
102 changes: 80 additions & 22 deletions core/src/test/java/org/kohsuke/stapler/RequestImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,29 +29,106 @@
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.junit.Assert;
import org.junit.Test;
import org.jvnet.hudson.test.For;
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 <a href="mailto:[email protected]">[email protected]</a>
* @author Nikolas Falco
*/
public class RequestImplTest {

@For(RequestImpl.class)
public static class SetterObject {
private List<String> choices;

@DataBoundConstructor
public SetterObject() {
choices = new ArrayList<String>();
}

@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<String>) choices;
}
}

public List<String> getChoices() {
return choices;
}

}

@Issue("JENKINS-61438")
@Test
nfalco79 marked this conversation as resolved.
Show resolved Hide resolved
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.<AncestorImpl>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);
jglick marked this conversation as resolved.
Show resolved Hide resolved
Assert.assertEquals(o.getChoices(), Arrays.asList("1", "2", "3"));
}

@Test
public void test_multipart_formdata() throws IOException, ServletException {
final Stapler stapler = new Stapler();
final byte[] buf = generateMultipartData();
final MockRequest mockRequest = mockRequest(generateMultipartData());

RequestImpl request = new RequestImpl(stapler, mockRequest, Collections.<AncestorImpl>emptyList(), null);

// Check that we can get the Form Fields. See https://github.com/stapler/stapler/issues/52
Assert.assertEquals("text1_val", request.getParameter("text1"));
Assert.assertEquals("text2_val", request.getParameter("text2"));

// Check that we can get the file
FileItem fileItem = request.getFileItem("pomFile");
Assert.assertNotNull(fileItem);

// Check getParameterValues
Assert.assertEquals("text1_val", request.getParameterValues("text1")[0]);

// Check getParameterNames
Assert.assertTrue(Collections.list(request.getParameterNames()).contains("p1"));
Assert.assertTrue(Collections.list(request.getParameterNames()).contains("text1"));

// Check getParameterMap
Assert.assertTrue(request.getParameterMap().containsKey("text1"));
}

private MockRequest mockRequest(final byte[] buf) {
jglick marked this conversation as resolved.
Show resolved Hide resolved
final ByteArrayInputStream is = new ByteArrayInputStream(buf);
final MockRequest mockRequest = new MockRequest() {
return new MockRequest() {
@Override
public String getContentType() {
return "multipart/form-data; boundary=mpboundary";
Expand Down Expand Up @@ -100,26 +178,6 @@ public void setReadListener(ReadListener readListener) {
};
}
};

RequestImpl request = new RequestImpl(stapler, mockRequest, Collections.<AncestorImpl>emptyList(), null);

// Check that we can get the Form Fields. See https://github.com/stapler/stapler/issues/52
Assert.assertEquals("text1_val", request.getParameter("text1"));
Assert.assertEquals("text2_val", request.getParameter("text2"));

// Check that we can get the file
FileItem fileItem = request.getFileItem("pomFile");
Assert.assertNotNull(fileItem);

// Check getParameterValues
Assert.assertEquals("text1_val", request.getParameterValues("text1")[0]);

// Check getParameterNames
Assert.assertTrue(Collections.list(request.getParameterNames()).contains("p1"));
Assert.assertTrue(Collections.list(request.getParameterNames()).contains("text1"));

// Check getParameterMap
Assert.assertTrue(request.getParameterMap().containsKey("text1"));
}

private byte[] generateMultipartData() throws IOException {
Expand Down