Skip to content

Commit

Permalink
Merge pull request #15451 from belu/feat/paramconverter-for-localdate
Browse files Browse the repository at this point in the history
Add support for LocalDate as Path-/QueryParam
  • Loading branch information
geoand authored Mar 12, 2021
2 parents a4597d9 + 592ca7e commit ee1fafc
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.quarkus.resteasy.reactive.server.test.simple;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;

import org.hamcrest.Matchers;
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;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class LocalDateCustomParamConverterProviderTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(HelloResource.class, CustomLocalDateParamConverterProvider.class,
CustomLocalDateParamConverter.class));

@Test
public void localDateAsQueryParam() {
RestAssured.get("/hello?date=1981-W38-6")
.then().body(Matchers.equalTo("hello#1981-09-19"));
}

@Test
public void localDateAsPathParam() {
RestAssured.get("/hello/1995-W38-4")
.then().body(Matchers.equalTo("hello@1995-09-21"));
}

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

@GET
public String helloQuery(@QueryParam("date") LocalDate date) {
return "hello#" + date;
}

@GET
@Path("{date}")
public String helloPath(@PathParam("date") LocalDate date) {
return "hello@" + date;
}
}

public static class CustomLocalDateParamConverter implements ParamConverter<LocalDate> {

@Override
public LocalDate fromString(String value) {
return LocalDate.parse(value, DateTimeFormatter.ISO_WEEK_DATE);
}

@Override
public String toString(LocalDate value) {
return value.format(DateTimeFormatter.ISO_WEEK_DATE);
}
}

@Provider
public static class CustomLocalDateParamConverterProvider implements ParamConverterProvider {

@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if (rawType == LocalDate.class) {
return (ParamConverter<T>) new CustomLocalDateParamConverter();
}
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.resteasy.reactive.server.test.simple;

import java.time.LocalDate;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;

import org.hamcrest.Matchers;
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;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class LocalDateParamTest {

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

@Test
public void localDateAsQueryParam() {
RestAssured.get("/hello?date=1984-08-08")
.then().body(Matchers.equalTo("hello#1984-08-08"));
}

@Test
public void localDateAsPathParam() {
RestAssured.get("/hello/1995-09-21")
.then().body(Matchers.equalTo("hello@1995-09-21"));
}

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

@GET
public String helloQuery(@QueryParam("date") LocalDate date) {
return "hello#" + date;
}

@GET
@Path("{date}")
public String helloPath(@PathParam("date") LocalDate date) {
return "hello@" + date;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.HEADER_PARAM;
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;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.LONG;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.MATRIX_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.MULTI;
Expand Down Expand Up @@ -901,6 +902,10 @@ && isContextType(paramType.asClassType())) {
elementType = paramType.name().toString();
handlePathSegmentParam(builder);
typeHandled = true;
} else if ((paramType.name().equals(LOCAL_DATE)) && (type == ParameterType.PATH || type == ParameterType.QUERY)) {
elementType = paramType.name().toString();
handleLocalDateParam(builder);
typeHandled = true;
}

if (!typeHandled) {
Expand Down Expand Up @@ -930,6 +935,9 @@ protected boolean handleCustomParameter(Map<DotName, AnnotationInstance> anns, P
protected void handlePathSegmentParam(PARAM builder) {
}

protected void handleLocalDateParam(PARAM builder) {
}

protected void handleOtherParam(Map<String, String> existingConverters, String errorLocation, boolean hasRuntimeConverters,
PARAM builder, String elementType) {
}
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.LocalDate;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -164,6 +165,7 @@ public final class ResteasyReactiveDotNames {
public static final DotName MAP = DotName.createSimple(Map.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 LOCAL_DATE = DotName.createSimple(LocalDate.class.getName());

public static final DotName UNI = DotName.createSimple(Uni.class.getName());
public static final DotName MULTI = DotName.createSimple(Multi.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames;
import org.jboss.resteasy.reactive.server.core.parameters.ParameterExtractor;
import org.jboss.resteasy.reactive.server.core.parameters.converters.ListConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.LocalDateParamConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.OptionalConverter;
import org.jboss.resteasy.reactive.server.core.parameters.converters.ParameterConverterSupplier;
import org.jboss.resteasy.reactive.server.core.parameters.converters.PathSegmentParamConverter;
Expand Down Expand Up @@ -282,6 +283,10 @@ protected void handlePathSegmentParam(ServerIndexedParameter builder) {
builder.setConverter(new PathSegmentParamConverter.Supplier());
}

protected void handleLocalDateParam(ServerIndexedParameter builder) {
builder.setConverter(new LocalDateParamConverter.Supplier());
}

protected ParameterConverterSupplier extractConverter(String elementType, IndexView indexView,
Map<String, String> existingConverters, String errorLocation, boolean hasRuntimeConverters) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jboss.resteasy.reactive.server.core.parameters.converters;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import org.jboss.resteasy.reactive.server.model.ParamConverterProviders;

public class LocalDateParamConverter implements ParameterConverter {

@Override
public Object convert(Object parameter) {
return LocalDate.parse(String.valueOf(parameter), DateTimeFormatter.ISO_LOCAL_DATE);
}

@Override
public void init(ParamConverterProviders deployment, Class<?> rawType, Type genericType,
Annotation[] annotations) {
// no init required
}

public static class Supplier implements ParameterConverterSupplier {

@Override
public String getClassName() {
return LocalDateParamConverter.class.getName();
}

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

0 comments on commit ee1fafc

Please sign in to comment.