forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for java.time.Year as JAX-RS parameter
Closes: quarkusio#34324
- Loading branch information
Showing
5 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
...ava/org/jboss/resteasy/reactive/server/core/parameters/converters/YearParamConverter.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,55 @@ | ||
package org.jboss.resteasy.reactive.server.core.parameters.converters; | ||
|
||
import static java.time.temporal.ChronoField.YEAR; | ||
|
||
import java.time.Year; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.time.format.SignStyle; | ||
|
||
public class YearParamConverter extends TemporalParamConverter<Year> { | ||
|
||
// lifted from the JDK as PARSER is private... | ||
private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder() | ||
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) | ||
.toFormatter(); | ||
|
||
// this can be called by generated code | ||
public YearParamConverter() { | ||
super(PARSER); | ||
} | ||
|
||
public YearParamConverter(DateTimeFormatter formatter) { | ||
super(formatter); | ||
} | ||
|
||
@Override | ||
protected Year convert(String value) { | ||
return Year.parse(value); | ||
} | ||
|
||
@Override | ||
protected Year convert(String value, DateTimeFormatter formatter) { | ||
return Year.parse(value, formatter); | ||
} | ||
|
||
public static class Supplier extends TemporalSupplier<YearParamConverter> { | ||
|
||
public Supplier() { | ||
} | ||
|
||
public Supplier(String pattern, String dateTimeFormatterProviderClassName) { | ||
super(pattern, dateTimeFormatterProviderClassName); | ||
} | ||
|
||
@Override | ||
protected YearParamConverter createConverter(DateTimeFormatter dateTimeFormatter) { | ||
return new YearParamConverter(dateTimeFormatter); | ||
} | ||
|
||
@Override | ||
public String getClassName() { | ||
return YearParamConverter.class.getName(); | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...rtx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/YearParamTest.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,104 @@ | ||
package org.jboss.resteasy.reactive.server.vertx.test.simple; | ||
|
||
import java.time.Year; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import jakarta.ws.rs.CookieParam; | ||
import jakarta.ws.rs.FormParam; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.HeaderParam; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.PathParam; | ||
import jakarta.ws.rs.QueryParam; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.resteasy.reactive.DateFormat; | ||
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.restassured.RestAssured; | ||
|
||
public class YearParamTest { | ||
|
||
@RegisterExtension | ||
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(HelloResource.class, CustomDateTimeFormatterProvider.class)); | ||
|
||
@Test | ||
public void localDateAsQueryParam() { | ||
RestAssured.get("/hello?date=01984") | ||
.then().statusCode(200).body(Matchers.equalTo("hello#1984")); | ||
} | ||
|
||
@Test | ||
public void localDateAsPathParam() { | ||
RestAssured.get("/hello/-21") | ||
.then().statusCode(200).body(Matchers.equalTo("hello@-21")); | ||
} | ||
|
||
@Test | ||
public void localDateAsFormParam() { | ||
RestAssured.given().formParam("date", "995").post("/hello") | ||
.then().statusCode(200).body(Matchers.equalTo("hello:995")); | ||
} | ||
|
||
@Test | ||
public void localDateAsHeader() { | ||
RestAssured.with().header("date", "1984") | ||
.get("/hello/header") | ||
.then().statusCode(200).body(Matchers.equalTo("hello=1984")); | ||
} | ||
|
||
@Test | ||
public void localDateAsCookie() { | ||
RestAssured.with().cookie("date", "1984") | ||
.get("/hello/cookie") | ||
.then().statusCode(200).body(Matchers.equalTo("hello/1984")); | ||
} | ||
|
||
@Path("hello") | ||
public static class HelloResource { | ||
|
||
@GET | ||
public String helloQuery(@QueryParam("date") @DateFormat(pattern = "yyyyy") Year date) { | ||
return "hello#" + date; | ||
} | ||
|
||
@GET | ||
@Path("{date}") | ||
public String helloPath(@PathParam("date") Year date) { | ||
return "hello@" + date; | ||
} | ||
|
||
@POST | ||
public String helloForm( | ||
@FormParam("date") @DateFormat(dateTimeFormatterProvider = CustomDateTimeFormatterProvider.class) Year date) { | ||
return "hello:" + date; | ||
} | ||
|
||
@GET | ||
@Path("cookie") | ||
public String helloCookie( | ||
@CookieParam("date") Year date) { | ||
return "hello/" + date; | ||
} | ||
|
||
@GET | ||
@Path("header") | ||
public String helloHeader( | ||
@HeaderParam("date") Year date) { | ||
return "hello=" + date; | ||
} | ||
} | ||
|
||
public static class CustomDateTimeFormatterProvider implements DateFormat.DateTimeFormatterProvider { | ||
@Override | ||
public DateTimeFormatter get() { | ||
return DateTimeFormatter.ofPattern("yyy"); | ||
} | ||
} | ||
|
||
} |