Skip to content

Commit 8a394e4

Browse files
ronsigalasoldano
authored andcommitted
[RESTEASY-2765] Fix validation of empty array
1 parent 4c656d1 commit 8a394e4

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

resteasy-jaxrs/src/main/java/org/jboss/resteasy/api/validation/ResteasyViolationException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ protected static String convertArrayToString(Object o)
412412
if (o instanceof Object[])
413413
{
414414
Object[] array = Object[].class.cast(o);
415+
if (array.length == 0)
416+
{
417+
return "[]";
418+
}
415419
StringBuffer sb = new StringBuffer("[").append(convertArrayToString(array[0]));
416420
for (int i = 1; i < array.length; i++)
417421
{
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.jboss.resteasy.test.validation;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import javax.ws.rs.client.Entity;
6+
import javax.ws.rs.core.MediaType;
7+
import javax.ws.rs.core.Response;
8+
9+
import org.jboss.arquillian.container.test.api.Deployment;
10+
import org.jboss.arquillian.container.test.api.RunAsClient;
11+
import org.jboss.arquillian.junit.Arquillian;
12+
import org.jboss.resteasy.api.validation.ResteasyViolationException;
13+
import org.jboss.resteasy.api.validation.Validation;
14+
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
15+
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
16+
import org.jboss.resteasy.test.validation.resource.EmptyArrayValidationFoo;
17+
import org.jboss.resteasy.test.validation.resource.EmptyArrayValidationResource;
18+
import org.jboss.resteasy.util.HttpResponseCodes;
19+
import org.jboss.resteasy.utils.PortProviderUtil;
20+
import org.jboss.resteasy.utils.TestUtil;
21+
import org.jboss.shrinkwrap.api.Archive;
22+
import org.jboss.shrinkwrap.api.spec.WebArchive;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
/**
28+
* @tpSubChapter Resteasy-client
29+
* @tpChapter Client tests
30+
* @tpSince RESTEasy 3.14.0
31+
* @tpTestCaseDetails Regression test for RESTEASY-2765
32+
*/
33+
@RunWith(Arquillian.class)
34+
@RunAsClient
35+
public class EmptyArrayValidationTest {
36+
37+
@Deployment
38+
public static Archive<?> deploy() {
39+
WebArchive war = TestUtil.prepareArchive(EmptyArrayValidationTest.class.getSimpleName());
40+
war.addClass(EmptyArrayValidationFoo.class);
41+
return TestUtil.finishContainerPrepare(war, null, EmptyArrayValidationResource.class);
42+
}
43+
44+
private String generateURL(String path) {
45+
return PortProviderUtil.generateURL(path, EmptyArrayValidationTest.class.getSimpleName());
46+
}
47+
48+
/**
49+
* @tpTestDetails Verify validation of empty array doesn't throw ArrayIndexOutOfBoundsException.
50+
* @tpSince RESTEasy 3.14.0
51+
*/
52+
@Test
53+
public void testEmptyArray() throws Exception {
54+
ResteasyClient client = new ResteasyClientBuilder().build();
55+
EmptyArrayValidationFoo foo = new EmptyArrayValidationFoo(new Object[] {});
56+
Response response = client.target(generateURL("/emptyarray")).request().post(Entity.entity(foo, MediaType.APPLICATION_JSON), Response.class);
57+
Object header = response.getHeaderString(Validation.VALIDATION_HEADER);
58+
Assert.assertTrue("Header has wrong format", header instanceof String);
59+
Assert.assertTrue("Header has wrong format", Boolean.valueOf(String.class.cast(header)));
60+
String answer = response.readEntity(String.class);
61+
assertEquals(HttpResponseCodes.SC_BAD_REQUEST, response.getStatus());
62+
ResteasyViolationException e = new ResteasyViolationException(String.class.cast(answer));
63+
TestUtil.countViolations(e, 1, 0, 0, 0, 1, 0);
64+
}
65+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.jboss.resteasy.test.validation.resource;
2+
3+
import javax.validation.constraints.NotEmpty;
4+
5+
public class EmptyArrayValidationFoo {
6+
7+
@NotEmpty
8+
Object[] array;
9+
10+
public EmptyArrayValidationFoo(final Object[] array) {
11+
this.array = array;
12+
}
13+
14+
public EmptyArrayValidationFoo() {
15+
}
16+
17+
public Object[] getArray() {
18+
return array;
19+
}
20+
21+
public void setArray(final Object[] array) {
22+
this.array = array;
23+
}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.jboss.resteasy.test.validation.resource;
2+
3+
import javax.validation.Valid;
4+
import javax.ws.rs.Consumes;
5+
import javax.ws.rs.POST;
6+
import javax.ws.rs.Path;
7+
import javax.ws.rs.core.MediaType;
8+
9+
@Path("")
10+
public class EmptyArrayValidationResource {
11+
12+
@POST
13+
@Path("emptyarray")
14+
@Consumes(MediaType.APPLICATION_JSON)
15+
public void test(@Valid EmptyArrayValidationFoo foo) {
16+
}
17+
}

0 commit comments

Comments
 (0)