diff --git a/ext/bean-validation/src/main/java/org/glassfish/jersey/server/validation/internal/ValidationHelper.java b/ext/bean-validation/src/main/java/org/glassfish/jersey/server/validation/internal/ValidationHelper.java index 862ac423d1..5172583b22 100644 --- a/ext/bean-validation/src/main/java/org/glassfish/jersey/server/validation/internal/ValidationHelper.java +++ b/ext/bean-validation/src/main/java/org/glassfish/jersey/server/validation/internal/ValidationHelper.java @@ -99,7 +99,25 @@ private static String getViolationInvalidValue(final Object invalidValue) { } if (invalidValue.getClass().isArray()) { - return Arrays.toString((Object[]) invalidValue); + if (invalidValue instanceof Object[]) { + return Arrays.toString((Object[]) invalidValue); + } else if (invalidValue instanceof boolean[]) { + return Arrays.toString((boolean[]) invalidValue); + } else if (invalidValue instanceof byte[]) { + return Arrays.toString((byte[]) invalidValue); + } else if (invalidValue instanceof char[]) { + return Arrays.toString((char[]) invalidValue); + } else if (invalidValue instanceof double[]) { + return Arrays.toString((double[]) invalidValue); + } else if (invalidValue instanceof float[]) { + return Arrays.toString((float[]) invalidValue); + } else if (invalidValue instanceof int[]) { + return Arrays.toString((int[]) invalidValue); + } else if (invalidValue instanceof long[]) { + return Arrays.toString((long[]) invalidValue); + } else if (invalidValue instanceof short[]) { + return Arrays.toString((short[]) invalidValue); + } } return invalidValue.toString(); diff --git a/tests/integration/jersey-2689/pom.xml b/tests/integration/jersey-2689/pom.xml new file mode 100644 index 0000000000..73c687a3b3 --- /dev/null +++ b/tests/integration/jersey-2689/pom.xml @@ -0,0 +1,113 @@ + + + + 4.0.0 + + + org.glassfish.jersey.tests.integration + project + 2.14-SNAPSHOT + + + jersey-2689 + war + jersey-tests-integration-jersey-2689 + + Servlet integration test - JERSEY-2689 - Problem with validation errors on primitive type arrays + + + + org.glassfish.jersey.containers + jersey-container-servlet-core + + + org.glassfish.jersey.ext + jersey-bean-validation + + + + com.fasterxml.jackson.core + jackson-core + 2.4.0 + + + com.fasterxml.jackson.core + jackson-annotations + 2.4.0 + + + com.fasterxml.jackson.core + jackson-databind + 2.4.0 + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + 2.4.0 + + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-external + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.apache.maven.plugins + maven-failsafe-plugin + + + org.mortbay.jetty + jetty-maven-plugin + + + + + diff --git a/tests/integration/jersey-2689/src/main/java/org/glassfish/jersey/tests/integration/jersey2689/Jersey2689.java b/tests/integration/jersey-2689/src/main/java/org/glassfish/jersey/tests/integration/jersey2689/Jersey2689.java new file mode 100644 index 0000000000..72b1f95ccf --- /dev/null +++ b/tests/integration/jersey-2689/src/main/java/org/glassfish/jersey/tests/integration/jersey2689/Jersey2689.java @@ -0,0 +1,76 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.tests.integration.jersey2689; + +import java.util.HashSet; +import java.util.Set; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.server.ServerProperties; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; + +/** + * @author Oscar Guindzberg + * + */ +public class Jersey2689 extends ResourceConfig { + + public Jersey2689() { + // Set package to look for resources in + packages("org.glassfish.jersey.tests.integration.jersey2689"); + + this.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true); + + // create custom ObjectMapper + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + + // create JsonProvider to provide custom ObjectMapper + JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(); + provider.setMapper(mapper); + this.register(provider); + + } + + +} diff --git a/tests/integration/jersey-2689/src/main/java/org/glassfish/jersey/tests/integration/jersey2689/Resource.java b/tests/integration/jersey-2689/src/main/java/org/glassfish/jersey/tests/integration/jersey2689/Resource.java new file mode 100644 index 0000000000..4f29582e9a --- /dev/null +++ b/tests/integration/jersey-2689/src/main/java/org/glassfish/jersey/tests/integration/jersey2689/Resource.java @@ -0,0 +1,14 @@ +package org.glassfish.jersey.tests.integration.jersey2689; + +import javax.validation.Valid; +import javax.ws.rs.POST; +import javax.ws.rs.Path; + +@Path("/") +public class Resource { + @POST + @Path("/post-bean") + public void processBean(@Valid SampleBean bean) { + //do-nothing + } +} diff --git a/tests/integration/jersey-2689/src/main/java/org/glassfish/jersey/tests/integration/jersey2689/SampleBean.java b/tests/integration/jersey-2689/src/main/java/org/glassfish/jersey/tests/integration/jersey2689/SampleBean.java new file mode 100644 index 0000000000..f2462fb1b3 --- /dev/null +++ b/tests/integration/jersey-2689/src/main/java/org/glassfish/jersey/tests/integration/jersey2689/SampleBean.java @@ -0,0 +1,17 @@ +package org.glassfish.jersey.tests.integration.jersey2689; + +import org.hibernate.validator.constraints.NotEmpty; + +public class SampleBean { + + @NotEmpty + private byte[] array; + + public byte[] getArray() { + return array; + } + public void setArray(byte[] array) { + this.array = array; + } + +} diff --git a/tests/integration/jersey-2689/src/main/webapp/WEB-INF/web.xml b/tests/integration/jersey-2689/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..af50575a50 --- /dev/null +++ b/tests/integration/jersey-2689/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,63 @@ + + + + + + testServlet1 + org.glassfish.jersey.servlet.ServletContainer + + javax.ws.rs.Application + org.glassfish.jersey.tests.integration.jersey2689.Jersey2689 + + 1 + + + + testServlet1 + /* + + + diff --git a/tests/integration/jersey-2689/src/test/java/org/glassfish/jersey/tests/integration/jersey2689/Jersey2689ITCase.java b/tests/integration/jersey-2689/src/test/java/org/glassfish/jersey/tests/integration/jersey2689/Jersey2689ITCase.java new file mode 100644 index 0000000000..cdea644699 --- /dev/null +++ b/tests/integration/jersey-2689/src/test/java/org/glassfish/jersey/tests/integration/jersey2689/Jersey2689ITCase.java @@ -0,0 +1,126 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * http://glassfish.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.jersey.tests.integration.jersey2689; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.external.ExternalTestContainerFactory; +import org.glassfish.jersey.test.spi.TestContainerException; +import org.glassfish.jersey.test.spi.TestContainerFactory; +import org.junit.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; + +/** + * Tests for JERSEY-2689: Problem with validation errors on primitive type arrays. + *

There is a bug when a validation fails for a primitive data array. Eg a NotNull failed validation on a byte[] causes the code to throw a ClassCastException. The problem is caused by ValidationHelper.getViolationInvalidValue(Object invalidValue) It tries to cast any array to a Object[] A byte[] parameter would generate a ClassCastException.* + * @author Oscar Guindzberg (oscar.guindzberg at gmail.com) + */ +public class Jersey2689ITCase extends JerseyTest { + + + @Override + protected ResourceConfig configure() { + return new Jersey2689(); + } + + @Override + protected TestContainerFactory getTestContainerFactory() throws TestContainerException { + return new ExternalTestContainerFactory(); + } + + /** + * Checks if a thread gets stuck when an {@code IOException} is thrown from the {@code + * MessageBodyWriter#writeTo}. + */ + @Test + public void testByteArray() throws Exception { + // Executor. + final ExecutorService executor = Executors.newSingleThreadExecutor(); + + final Future responseFuture = executor.submit(new Callable() { + + @Override + public Response call() throws Exception { + SampleBean bean = new SampleBean(); + bean.setArray(new byte[]{}); + + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(); + provider.setMapper(mapper); + client().register(provider); + + return target().path("post-bean").request().post(Entity.entity(bean, MediaType.APPLICATION_JSON)); + } + + }); + + executor.shutdown(); + final boolean inTime = executor.awaitTermination(5000, TimeUnit.MILLISECONDS); + + // Asserts. + assertTrue(inTime); + + // Response. + final Response response = responseFuture.get(); + + //Make sure we get a 400 error and not a 500 error + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusInfo().getStatusCode()); + + } + + +}