-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15451 from belu/feat/paramconverter-for-localdate
Add support for LocalDate as Path-/QueryParam
- Loading branch information
Showing
6 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...arkus/resteasy/reactive/server/test/simple/LocalDateCustomParamConverterProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...ent/src/test/java/io/quarkus/resteasy/reactive/server/test/simple/LocalDateParamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...rg/jboss/resteasy/reactive/server/core/parameters/converters/LocalDateParamConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |