Skip to content

Commit

Permalink
Make RESTEasy Reactive work with Optional temporal types
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand authored and igorregis committed Oct 16, 2022
1 parent 502dae6 commit 9a87671
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ 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(INSTANT, LOCAL_DATE, LOCAL_TIME, LOCAL_DATE_TIME,
protected 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);

Expand Down Expand Up @@ -1281,8 +1281,8 @@ && isContextType(paramType.asClassType())) {
currentClassInfo, actualEndpointInfo, index);
}

handleOptionalParam(existingConverters, errorLocation, hasRuntimeConverters, builder, elementType,
genericElementType);
handleOptionalParam(existingConverters, anns, errorLocation, hasRuntimeConverters, builder, elementType,
genericElementType, currentMethodInfo);
}
builder.setOptional(true);
} else if (convertible) {
Expand Down Expand Up @@ -1380,8 +1380,11 @@ protected void handleSortedSetParam(Map<String, String> existingConverters, Stri
boolean hasRuntimeConverters, PARAM builder, String elementType) {
}

protected void handleOptionalParam(Map<String, String> existingConverters, String errorLocation,
boolean hasRuntimeConverters, PARAM builder, String elementType, String genericElementType) {
protected void handleOptionalParam(Map<String, String> existingConverters,
Map<DotName, AnnotationInstance> parameterAnnotations,
String errorLocation,
boolean hasRuntimeConverters, PARAM builder, String elementType, String genericElementType,
MethodInfo currentMethodInfo) {
}

protected void handleSetParam(Map<String, String> existingConverters, String errorLocation, boolean hasRuntimeConverters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,11 @@ protected void handleSortedSetParam(Map<String, String> existingConverters, Stri
builder.setConverter(new SortedSetConverter.SortedSetSupplier(converter));
}

protected void handleOptionalParam(Map<String, String> existingConverters, String errorLocation,
boolean hasRuntimeConverters, ServerIndexedParameter builder, String elementType, String genericElementType) {
protected void handleOptionalParam(Map<String, String> existingConverters,
Map<DotName, AnnotationInstance> parameterAnnotations,
String errorLocation,
boolean hasRuntimeConverters, ServerIndexedParameter builder, String elementType, String genericElementType,
MethodInfo currentMethodInfo) {
ParameterConverterSupplier converter = null;

if (genericElementType != null) {
Expand All @@ -350,6 +353,9 @@ protected void handleOptionalParam(Map<String, String> existingConverters, Strin
converter = new SortedSetConverter.SortedSetSupplier(genericTypeConverter);
builder.setSingle(false);
}
} else if (SUPPORT_TEMPORAL_PARAMS.contains(DotName.createSimple(elementType))) {
converter = determineTemporalConverter(DotName.createSimple(elementType), parameterAnnotations,
currentMethodInfo);
}

if (converter == null) {
Expand Down Expand Up @@ -393,6 +399,11 @@ protected String handleTrailingSlash(String path) {
protected void handleTemporalParam(ServerIndexedParameter builder, DotName paramType,
Map<DotName, AnnotationInstance> parameterAnnotations,
MethodInfo currentMethodInfo) {
builder.setConverter(determineTemporalConverter(paramType, parameterAnnotations, currentMethodInfo));
}

private ParameterConverterSupplier determineTemporalConverter(DotName paramType,
Map<DotName, AnnotationInstance> parameterAnnotations, MethodInfo currentMethodInfo) {
String format = null;
String dateTimeFormatterProviderClassName = null;

Expand All @@ -416,8 +427,7 @@ protected void handleTemporalParam(ServerIndexedParameter builder, DotName param
"'java.time.Instant' types must not be annotated with '@DateFormat'",
currentMethodInfo));
}
builder.setConverter(new InstantParamConverter.Supplier());
return;
return new InstantParamConverter.Supplier();
}

if ((format != null) && (dateTimeFormatterProviderClassName != null)) {
Expand All @@ -430,23 +440,17 @@ protected void handleTemporalParam(ServerIndexedParameter builder, DotName param
}

if (LOCAL_DATE.equals(paramType)) {
builder.setConverter(new LocalDateParamConverter.Supplier(format, dateTimeFormatterProviderClassName));
return;
return new LocalDateParamConverter.Supplier(format, dateTimeFormatterProviderClassName);
} else if (LOCAL_DATE_TIME.equals(paramType)) {
builder.setConverter(new LocalDateTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName));
return;
return new LocalDateTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName);
} else if (LOCAL_TIME.equals(paramType)) {
builder.setConverter(new LocalTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName));
return;
return new LocalTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName);
} else if (OFFSET_DATE_TIME.equals(paramType)) {
builder.setConverter(new OffsetDateTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName));
return;
return new OffsetDateTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName);
} else if (OFFSET_TIME.equals(paramType)) {
builder.setConverter(new OffsetTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName));
return;
return new OffsetTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName);
} else if (ZONED_DATE_TIME.equals(paramType)) {
builder.setConverter(new ZonedDateTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName));
return;
return new ZonedDateTimeParamConverter.Supplier(format, dateTimeFormatterProviderClassName);
}

throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.restassured.RestAssured;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
Expand Down Expand Up @@ -30,6 +31,15 @@ public void localDateTimeAsQueryParam() {
.then().statusCode(200).body(Matchers.equalTo("hello#1984"));
}

@Test
public void localDateTimeAsOptionalQueryParam() {
RestAssured.get("/hello/optional?date=1984-08-08T01:02:03")
.then().statusCode(200).body(Matchers.equalTo("hello#1984"));

RestAssured.get("/hello/optional")
.then().statusCode(200).body(Matchers.equalTo("hello#2022"));
}

@Test
public void localDateTimeAsPathParam() {
RestAssured.get("/hello/1995-09-21 01:02:03")
Expand All @@ -50,6 +60,12 @@ public String helloQuery(@RestQuery LocalDateTime date) {
return "hello#" + date.getYear();
}

@Path("optional")
@GET
public String helloOptionalQuery(@RestQuery Optional<LocalDateTime> date) {
return "hello#" + date.orElse(LocalDateTime.of(2022, 1, 1, 0, 0)).getYear();
}

@GET
@Path("{date}")
public String helloPath(@RestPath @DateFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime date) {
Expand Down

0 comments on commit 9a87671

Please sign in to comment.