Skip to content

Commit

Permalink
Add support for java.time.Instant params in RESTEasy Reactive
Browse files Browse the repository at this point in the history
Address: #21675 (comment)
(cherry picked from commit d0cda63)
  • Loading branch information
geoand authored and gsmet committed Apr 18, 2022
1 parent c327e5b commit 0214699
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.FORM_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.HEADER_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.HTTP_HEADERS;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.INSTANT;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.INTEGER;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.LIST;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.LOCAL_DATE;
Expand Down Expand Up @@ -143,7 +144,8 @@ public abstract class EndpointIndexer<T extends EndpointIndexer<T, PARAM, METHOD
DotName.createSimple("org.jboss.resteasy.reactive.server.SimpleResourceInfo"), //TODO: fixme
RESOURCE_INFO)));

private static final Set<DotName> SUPPORT_TEMPORAL_PARAMS = Set.of(LOCAL_DATE, LOCAL_TIME, LOCAL_DATE_TIME, OFFSET_TIME,
private static final Set<DotName> SUPPORT_TEMPORAL_PARAMS = Set.of(INSTANT, LOCAL_DATE, LOCAL_TIME, LOCAL_DATE_TIME,
OFFSET_TIME,
OFFSET_DATE_TIME, ZONED_DATE_TIME);

protected static final Logger log = Logger.getLogger(EndpointIndexer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
Expand Down Expand Up @@ -182,6 +183,7 @@ public final class ResteasyReactiveDotNames {
public static final DotName DUMMY_ELEMENT_TYPE = DotName.createSimple(DummyElementType.class.getName());
public static final DotName MULTI_VALUED_MAP = DotName.createSimple(MultivaluedMap.class.getName());
public static final DotName PATH_SEGMENT = DotName.createSimple(PathSegment.class.getName());
public static final DotName INSTANT = DotName.createSimple(Instant.class.getName());
public static final DotName LOCAL_DATE = DotName.createSimple(LocalDate.class.getName());
public static final DotName LOCAL_DATE_TIME = DotName.createSimple(LocalDateTime.class.getName());
public static final DotName LOCAL_TIME = DotName.createSimple(LocalTime.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.DATE_FORMAT;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.INSTANT;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.JAX_RS_ANNOTATIONS_FOR_FIELDS;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.JSONP_JSON_ARRAY;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.JSONP_JSON_NUMBER;
Expand Down Expand Up @@ -57,6 +58,7 @@
import org.jboss.resteasy.reactive.server.core.parameters.converters.ArrayConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.CharParamConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.CharacterParamConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.InstantParamConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.ListConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.LoadedParameterConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.LocalDateParamConverter;
Expand Down Expand Up @@ -403,6 +405,16 @@ protected void handleTemporalParam(ServerIndexedParameter builder, DotName param
}
}

if (INSTANT.equals(paramType)) {
if (dateFormatInstance != null) {
throw new RuntimeException(contextualizeErrorMessage(
"'java.time.Instant' types must not be annotated with '@DateFormat'",
currentMethodInfo));
}
builder.setConverter(new InstantParamConverter.Supplier());
return;
}

if ((format != null) && (dateTimeFormatterProviderClassName != null)) {
throw new RuntimeException(contextualizeErrorMessage(
"Using both 'format' and 'dateTimeFormatterProvider' is not allowed when using '@DateFormat'",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jboss.resteasy.reactive.server.core.parameters.converters;

import java.time.Instant;

public class InstantParamConverter implements ParameterConverter {

@Override
public Object convert(Object value) {
return Instant.parse(value.toString());
}

public static class Supplier implements ParameterConverterSupplier {

public Supplier() {
}

@Override
public ParameterConverter get() {
return new InstantParamConverter();
}

@Override
public String getClassName() {
return InstantParamConverter.class.getName();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jboss.resteasy.reactive.server.vertx.test.simple;

import io.restassured.RestAssured;
import java.time.Duration;
import java.time.Instant;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.hamcrest.Matchers;
import org.jboss.resteasy.reactive.RestQuery;
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class InstantParamTest {

@RegisterExtension
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(HelloResource.class));

@Test
public void test() {
RestAssured.get("/hello?instant=1984-08-08T01:02:03Z")
.then().statusCode(200).body(Matchers.equalTo("hello#1984-08-09T01:02:03Z"));
}

@Path("hello")
public static class HelloResource {

@GET
public String helloQuery(@RestQuery Instant instant) {
return "hello#" + instant.plus(Duration.ofDays(1)).toString();
}
}

}

0 comments on commit 0214699

Please sign in to comment.