diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java b/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java index e4249b8139..144189b9f1 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java @@ -21,12 +21,16 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.security.AccessController; import java.text.ParseException; import java.util.Date; import java.util.List; import java.util.Optional; +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.OptionalLong; import javax.inject.Inject; import javax.inject.Singleton; @@ -252,16 +256,16 @@ public String toString(final T value) throws IllegalArgumentException { /** * Provider of {@link ParamConverter param converter} that produce the Optional instance - * by invoking {@link AggregatedProvider}. + * by invoking {@link ParamConverterProvider}. */ @Singleton - public static class OptionalProvider implements ParamConverterProvider { + public static class OptionalCustomProvider implements ParamConverterProvider { // Delegates to this provider when the type of Optional is extracted. private final InjectionManager manager; @Inject - public OptionalProvider(InjectionManager manager) { + public OptionalCustomProvider(InjectionManager manager) { this.manager = manager; } @@ -305,6 +309,91 @@ public String toString(T value) throws IllegalArgumentException { } + /** + * Provider of {@link ParamConverter param converter} that produce the OptionalInt, OptionalDouble + * or OptionalLong instance. + */ + @Singleton + public static class OptionalProvider implements ParamConverterProvider { + + @Override + public ParamConverter getConverter(Class rawType, Type genericType, Annotation[] annotations) { + final Optionals optionals = Optionals.getOptional(rawType); + return (optionals == null) ? null : new ParamConverter() { + + @Override + public T fromString(String value) { + if (value == null) { + return (T) optionals.empty(); + } else { + return (T) optionals.of(value); + } + } + + @Override + public String toString(T value) throws IllegalArgumentException { + /* + * Unfortunately 'orElse' cannot be stored in an Optional. As only one value can + * be stored, it makes no sense that 'value' is Optional. It can just be the value. + * We don't fail here but we don't process it. + */ + return null; + } + }; + } + + private static enum Optionals { + + OPTIONAL_INT(OptionalInt.class) { + @Override + Object empty() { + return OptionalInt.empty(); + } + @Override + Object of(Object value) { + return OptionalInt.of(Integer.parseInt((String) value)); + } + }, OPTIONAL_DOUBLE(OptionalDouble.class) { + @Override + Object empty() { + return OptionalDouble.empty(); + } + @Override + Object of(Object value) { + return OptionalDouble.of(Double.parseDouble((String) value)); + } + }, OPTIONAL_LONG(OptionalLong.class) { + @Override + Object empty() { + return OptionalLong.empty(); + } + @Override + Object of(Object value) { + return OptionalLong.of(Long.parseLong((String) value)); + } + }; + + private final Class clazz; + + private Optionals(Class clazz) { + this.clazz = clazz; + } + + private static Optionals getOptional(Class clazz) { + for (Optionals optionals : Optionals.values()) { + if (optionals.clazz == clazz) { + return optionals; + } + } + return null; + } + + abstract Object empty(); + + abstract Object of(Object value); + } + } + /** * Aggregated {@link ParamConverterProvider param converter provider}. */ @@ -326,7 +415,8 @@ public AggregatedProvider(InjectionManager manager) { new CharacterProvider(), new TypeFromString(), new StringConstructor(), - new OptionalProvider(manager) + new OptionalCustomProvider(manager), + new OptionalProvider() }; } diff --git a/tests/e2e-server/src/test/java/org/glassfish/jersey/tests/e2e/server/OptionalParamConverterNoProviderTest.java b/tests/e2e-server/src/test/java/org/glassfish/jersey/tests/e2e/server/OptionalParamConverterNoProviderTest.java new file mode 100644 index 0000000000..162692920a --- /dev/null +++ b/tests/e2e-server/src/test/java/org/glassfish/jersey/tests/e2e/server/OptionalParamConverterNoProviderTest.java @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.tests.e2e.server; + +import static org.junit.Assert.assertEquals; + +import java.util.List; +import java.util.Optional; +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.OptionalLong; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Response; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Test; + +public class OptionalParamConverterNoProviderTest extends JerseyTest { + + private static final String PARAM_NAME = "paramName"; + + @Path("/OptionalResource") + public static class OptionalResource { + + @GET + @Path("/fromString") + public Response fromString(@QueryParam(PARAM_NAME) Optional data) { + return Response.ok(data.orElse("")).build(); + } + + @GET + @Path("/fromInteger") + public Response fromInteger(@QueryParam(PARAM_NAME) Optional data) { + return Response.ok(data.orElse(0)).build(); + } + + @GET + @Path("/fromList") + public Response fromList(@QueryParam(PARAM_NAME) List> data) { + StringBuilder builder = new StringBuilder(""); + for (Optional val : data) { + builder.append(val.orElse(0)); + } + return Response.ok(builder.toString()).build(); + } + + @GET + @Path("/fromListOptionalInt") + public Response fromListOptionalInt(@QueryParam(PARAM_NAME) List data) { + StringBuilder builder = new StringBuilder(""); + for (OptionalInt val : data) { + builder.append(val.orElse(0)); + } + return Response.ok(builder.toString()).build(); + } + + @GET + @Path("/fromListOptionalDouble") + public Response fromListOptionalDouble(@QueryParam(PARAM_NAME) List data) { + StringBuilder builder = new StringBuilder(""); + for (OptionalDouble val : data) { + builder.append(val.orElse(0)); + } + return Response.ok(builder.toString()).build(); + } + + @GET + @Path("/fromListOptionalLong") + public Response fromListOptionalLong(@QueryParam(PARAM_NAME) List data) { + StringBuilder builder = new StringBuilder(""); + for (OptionalLong val : data) { + builder.append(val.orElse(0)); + } + return Response.ok(builder.toString()).build(); + } + + @GET + @Path("/fromInteger2") + public Response fromInteger2(@QueryParam(PARAM_NAME) OptionalInt data) { + return Response.ok(data.orElse(0)).build(); + } + + @GET + @Path("/fromLong") + public Response fromLong(@QueryParam(PARAM_NAME) OptionalLong data) { + return Response.ok(data.orElse(0)).build(); + } + + @GET + @Path("/fromDouble") + public Response fromDouble(@QueryParam(PARAM_NAME) OptionalDouble data) { + return Response.ok(data.orElse(0.1)).build(); + } + } + + @Override + protected Application configure() { + return new ResourceConfig(OptionalResource.class); + } + + @Test + public void fromOptionalStr() { + Response empty = target("/OptionalResource/fromString").request().get(); + Response notEmpty = target("/OptionalResource/fromString").queryParam(PARAM_NAME, "anyValue").request().get(); + assertEquals(200, empty.getStatus()); + assertEquals("", empty.readEntity(String.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals("anyValue", notEmpty.readEntity(String.class)); + } + + @Test + public void fromOptionalInt() { + Response empty = target("/OptionalResource/fromInteger").request().get(); + Response notEmpty = target("/OptionalResource/fromInteger").queryParam(PARAM_NAME, 1).request().get(); + assertEquals(200, empty.getStatus()); + assertEquals(Integer.valueOf(0), empty.readEntity(Integer.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals(Integer.valueOf(1), notEmpty.readEntity(Integer.class)); + } + + @Test + public void fromOptionalInt2() { + Response empty = target("/OptionalResource/fromInteger2").request().get(); + Response notEmpty = target("/OptionalResource/fromInteger2").queryParam(PARAM_NAME, 1).request().get(); + assertEquals(200, empty.getStatus()); + assertEquals(Integer.valueOf(0), empty.readEntity(Integer.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals(Integer.valueOf(1), notEmpty.readEntity(Integer.class)); + } + + @Test + public void fromOptionalList() { + Response empty = target("/OptionalResource/fromList").request().get(); + Response notEmpty = target("/OptionalResource/fromList").queryParam(PARAM_NAME, 1) + .queryParam(PARAM_NAME, 2).request().get(); + assertEquals(200, empty.getStatus()); + assertEquals("", empty.readEntity(String.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals("12", notEmpty.readEntity(String.class)); + } + + @Test + public void fromOptionalListOptionalInt() { + Response empty = target("/OptionalResource/fromListOptionalInt").request().get(); + Response notEmpty = target("/OptionalResource/fromListOptionalInt").queryParam(PARAM_NAME, 1) + .queryParam(PARAM_NAME, 2).request().get(); + assertEquals(200, empty.getStatus()); + assertEquals("", empty.readEntity(String.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals("12", notEmpty.readEntity(String.class)); + } + + @Test + public void fromOptionalListOptionalDouble() { + Response empty = target("/OptionalResource/fromListOptionalDouble").request().get(); + Response notEmpty = target("/OptionalResource/fromListOptionalDouble").queryParam(PARAM_NAME, 1) + .queryParam(PARAM_NAME, 2).request().get(); + assertEquals(200, empty.getStatus()); + assertEquals("", empty.readEntity(String.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals("1.02.0", notEmpty.readEntity(String.class)); + } + + @Test + public void fromOptionalListOptionalLong() { + Response empty = target("/OptionalResource/fromListOptionalLong").request().get(); + Response notEmpty = target("/OptionalResource/fromListOptionalLong").queryParam(PARAM_NAME, 1) + .queryParam(PARAM_NAME, 2).request().get(); + assertEquals(200, empty.getStatus()); + assertEquals("", empty.readEntity(String.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals("12", notEmpty.readEntity(String.class)); + } + + @Test + public void fromOptionalLong() { + Response empty = target("/OptionalResource/fromLong").request().get(); + Response notEmpty = target("/OptionalResource/fromLong").queryParam(PARAM_NAME, 1L).request().get(); + assertEquals(200, empty.getStatus()); + assertEquals(Long.valueOf(0L), empty.readEntity(Long.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals(Long.valueOf(1L), notEmpty.readEntity(Long.class)); + } + + @Test + public void fromOptionalDouble() { + Response empty = target("/OptionalResource/fromDouble").request().get(); + Response notEmpty = target("/OptionalResource/fromDouble").queryParam(PARAM_NAME, 1.1).request().get(); + assertEquals(200, empty.getStatus()); + assertEquals(Double.valueOf(0.1), empty.readEntity(Double.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals(Double.valueOf(1.1), notEmpty.readEntity(Double.class)); + } +}