Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of Temporal types for cookie and header params #32199

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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