Skip to content

Commit

Permalink
Merge pull request #32199 from luca-bassoricci/issue_32194
Browse files Browse the repository at this point in the history
Add support of Temporal types for cookie and header params
  • Loading branch information
geoand committed Mar 29, 2023
2 parents 5d24150 + 335e492 commit ebe57ad
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,8 @@ && isParameterContainerType(paramType.asClassType())) {
handlePathSegmentParam(builder);
typeHandled = true;
} else if (SUPPORT_TEMPORAL_PARAMS.contains(paramType.name())
&& (type == ParameterType.PATH || type == ParameterType.QUERY || type == ParameterType.FORM)) {
&& (type == ParameterType.PATH || type == ParameterType.QUERY || type == ParameterType.FORM
|| type == ParameterType.COOKIE || type == ParameterType.HEADER)) {
elementType = paramType.name().toString();
handleTemporalParam(builder, paramType.name(), anns, currentMethodInfo);
typeHandled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import java.time.Instant;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;

import org.hamcrest.Matchers;
import org.jboss.resteasy.reactive.RestCookie;
import org.jboss.resteasy.reactive.RestForm;
import org.jboss.resteasy.reactive.RestHeader;
import org.jboss.resteasy.reactive.RestPath;
import org.jboss.resteasy.reactive.RestQuery;
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest;
import org.jboss.shrinkwrap.api.ShrinkWrap;
Expand All @@ -24,18 +29,67 @@ public class InstantParamTest {
.addClasses(HelloResource.class));

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

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

@Test
public void instantAsFormParam() {
RestAssured.given().formParam("instant", "1984-08-08T01:02:03Z").post("/hello")
.then().statusCode(200).body(Matchers.equalTo("hello:1984-08-09T01:02:03Z"));
}

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

@Test
public void instantAsCookie() {
RestAssured.with().cookie("instant", "1984-08-08T01:02:03Z")
.get("/hello/cookie")
.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();
}

@GET
@Path("{instant}")
public String helloPath(@RestPath Instant instant) {
return "hello@" + instant.plus(Duration.ofDays(1)).toString();
}

@POST
public String helloForm(@RestForm Instant instant) {
return "hello:" + instant.plus(Duration.ofDays(1)).toString();
}

@GET
@Path("cookie")
public String helloCookie(@RestCookie Instant instant) {
return "hello/" + instant.plus(Duration.ofDays(1)).toString();
}

@GET
@Path("header")
public String helloHeader(@RestHeader Instant instant) {
return "hello=" + instant.plus(Duration.ofDays(1)).toString();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.time.LocalDate;
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;
Expand Down Expand Up @@ -43,6 +45,20 @@ public void localDateAsFormParam() {
.then().statusCode(200).body(Matchers.equalTo("hello:1995-09-22"));
}

@Test
public void localDateAsHeader() {
RestAssured.with().header("date", "08-08-1984")
.get("/hello/header")
.then().statusCode(200).body(Matchers.equalTo("hello=1984-08-08"));
}

@Test
public void localDateAsCookie() {
RestAssured.with().cookie("date", "08-08-1984")
.get("/hello/cookie")
.then().statusCode(200).body(Matchers.equalTo("hello/1984-08-08"));
}

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

Expand All @@ -62,6 +78,20 @@ public String helloForm(
@FormParam("date") @DateFormat(dateTimeFormatterProvider = CustomDateTimeFormatterProvider.class) LocalDate date) {
return "hello:" + date;
}

@GET
@Path("cookie")
public String helloCookie(
@CookieParam("date") @DateFormat(pattern = "dd-MM-yyyy") LocalDate date) {
return "hello/" + date;
}

@GET
@Path("header")
public String helloHeader(
@HeaderParam("date") @DateFormat(pattern = "dd-MM-yyyy") LocalDate date) {
return "hello=" + date;
}
}

public static class CustomDateTimeFormatterProvider implements DateFormat.DateTimeFormatterProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import org.hamcrest.Matchers;
import org.jboss.resteasy.reactive.DateFormat;
import org.jboss.resteasy.reactive.RestCookie;
import org.jboss.resteasy.reactive.RestHeader;
import org.jboss.resteasy.reactive.RestPath;
import org.jboss.resteasy.reactive.RestQuery;
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest;
Expand Down Expand Up @@ -55,6 +57,20 @@ public void localDateTimeAsFormParam() {
.then().statusCode(200).body(Matchers.equalTo("hello:22"));
}

@Test
public void localDateTimeAsHeader() {
RestAssured.with().header("date", "1984-08-08 01:02:03")
.get("/hello/header")
.then().statusCode(200).body(Matchers.equalTo("hello=1984-08-08T01:02:03"));
}

@Test
public void localDateTimeAsCookie() {
RestAssured.with().cookie("date", "1984-08-08 01:02:03")
.get("/hello/cookie")
.then().statusCode(200).body(Matchers.equalTo("hello/1984-08-08T01:02:03"));
}

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

Expand All @@ -80,6 +96,18 @@ public String helloForm(
@FormParam("date") @DateFormat(dateTimeFormatterProvider = CustomDateTimeFormatterProvider.class) LocalDateTime date) {
return "hello:" + date.getDayOfMonth();
}

@Path("cookie")
@GET
public String helloCookie(@RestCookie @DateFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime date) {
return "hello/" + date;
}

@Path("header")
@GET
public String helloHeader(@RestHeader @DateFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime date) {
return "hello=" + date;
}
}

public static class CustomDateTimeFormatterProvider implements DateFormat.DateTimeFormatterProvider {
Expand Down

0 comments on commit ebe57ad

Please sign in to comment.