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

TemporalParamConverter returns null if it has to convert empty string #42468

Merged
merged 7 commits into from
Nov 15, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void sendEmptyParameter() {
.when().get("/param-converter")
.then()
.statusCode(200)
.body(Matchers.is("Hello! You provided an empty number."));
.body(Matchers.is("Hello, world! No number was provided."));
}

@ApplicationScoped
Expand All @@ -70,12 +70,8 @@ public static class ParamConverterEndpoint {

@GET
public Response greet(@QueryParam("number") Optional<Integer> numberOpt) {
if (numberOpt != null) {
if (numberOpt.isPresent()) {
return Response.ok(String.format("Hello, %s!", numberOpt.get())).build();
} else {
return Response.ok("Hello! You provided an empty number.").build();
}
if (numberOpt.isPresent()) {
return Response.ok(String.format("Hello, %s!", numberOpt.get())).build();
} else {
return Response.ok("Hello, world! No number was provided.").build();
}
Expand Down Expand Up @@ -108,10 +104,6 @@ public static class OptionalIntegerParamConverter implements ParamConverter<Opti
@Override
public Optional<Integer> fromString(String value) {
if (value == null) {
return null;
}

if (value.trim().isEmpty()) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,19 +821,35 @@ public boolean isProducesChecked() {
@Override
public Object getHeader(String name, boolean single) {
if (httpHeaders == null) {
if (single)
return serverRequest().getRequestHeader(name);
if (single) {
String header = serverRequest().getRequestHeader(name);
if (header == null || header.isEmpty()) {
return null;
} else {
return header;
}
}
// empty collections must not be turned to null
return serverRequest().getAllRequestHeaders(name);
return serverRequest().getAllRequestHeaders(name).stream()
.filter(h -> !h.isEmpty())
.toList();
} else {
if (single)
return httpHeaders.getMutableHeaders().getFirst(name);
if (single) {
String header = httpHeaders.getMutableHeaders().getFirst(name);
if (header == null || header.isEmpty()) {
return null;
} else {
return header;
}
}
// empty collections must not be turned to null
List<String> list = httpHeaders.getMutableHeaders().get(name);
if (list == null) {
return Collections.emptyList();
} else {
return list;
return list.stream()
.filter(h -> !h.isEmpty())
.toList();
}
}
}
Expand All @@ -846,14 +862,19 @@ public Object getQueryParameter(String name, boolean single, boolean encoded) {
public Object getQueryParameter(String name, boolean single, boolean encoded, String separator) {
if (single) {
String val = serverRequest().getQueryParam(name);
if (val != null && val.isEmpty()) {
return null;
}
if (encoded && val != null) {
val = Encode.encodeQueryParam(val);
}
return val;
}

// empty collections must not be turned to null
List<String> strings = serverRequest().getAllQueryParams(name);
List<String> strings = serverRequest().getAllQueryParams(name).stream()
.filter(p -> !p.isEmpty())
.toList();
if (encoded) {
List<String> newStrings = new ArrayList<>();
for (String i : strings) {
Expand Down Expand Up @@ -909,7 +930,7 @@ public Object getMatrixParameter(String name, boolean single, boolean encoded) {
@Override
public String getCookieParameter(String name) {
Cookie cookie = getHttpHeaders().getCookies().get(name);
return cookie != null ? cookie.getValue() : null;
return cookie != null && !cookie.getValue().isEmpty() ? cookie.getValue() : null;
}

@Override
Expand All @@ -919,7 +940,7 @@ public Object getFormParameter(String name, boolean single, boolean encoded) {
}
if (single) {
FormValue val = formData.getFirst(name);
if (val == null || val.isFileItem()) {
if (val == null || val.isFileItem() || val.getValue().isEmpty()) {
return null;
}
if (encoded) {
Expand All @@ -931,15 +952,18 @@ public Object getFormParameter(String name, boolean single, boolean encoded) {
List<String> strings = new ArrayList<>();
if (val != null) {
for (FormValue i : val) {
if (i.getValue().isEmpty()) {
continue;
}
if (encoded) {
strings.add(Encode.encodeQueryParam(i.getValue()));
} else {
strings.add(i.getValue());
}
}
}
return strings;

return strings;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -37,12 +38,18 @@ public class LocalDateTimeParamTest {
public void localDateTimeAsQueryParam() {
RestAssured.get("/hello?date=1984-08-08T01:02:03")
.then().statusCode(200).body(Matchers.equalTo("hello#1984"));

RestAssured.get("/hello?date=")
.then().statusCode(200).body(Matchers.equalTo("hello#null"));
}

@Test
public void localDateTimeCollectionAsQueryParam() {
RestAssured.get("/hello?date=1984-08-08T01:02:03,1992-04-25T01:02:03")
RestAssured.get("/hello/list?date=1984-08-08T01:02:03,1992-04-25T01:02:03")
.then().statusCode(200).body(Matchers.equalTo("hello#1984,1992"));

RestAssured.get("/hello/list?date=&date=1984-08-08T01:02:03")
.then().statusCode(200).body(Matchers.equalTo("hello#1984"));
}

@Test
Expand All @@ -64,11 +71,14 @@ public void localDateTimeAsPathParam() {
public void localDateTimeAsFormParam() {
RestAssured.given().formParam("date", "1995/09/22 01:02").post("/hello")
.then().statusCode(200).body(Matchers.equalTo("hello:22"));

RestAssured.given().formParam("date", "").post("/hello")
.then().statusCode(200).body(Matchers.equalTo("hello:null"));
}

@Test
public void localDateTimeCollectionAsFormParam() {
RestAssured.given().formParam("date", "1995/09/22 01:02", "1992/04/25 01:02").post("/hello")
RestAssured.given().formParam("date", "1995/09/22 01:02", "1992/04/25 01:02").post("/hello/list")
.then().statusCode(200).body(Matchers.equalTo("hello:22,25"));
}

Expand All @@ -77,24 +87,47 @@ 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"));

RestAssured.with().header("date", "")
.get("/hello/header")
.then().statusCode(200).body(Matchers.equalTo("hello=null"));
}

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

RestAssured.with().header("date", "")
.get("/hello/header/list")
.then().statusCode(200).body(Matchers.equalTo("hello=[]"));
}

@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"));

RestAssured.with().cookie("date", "")
.get("/hello/cookie")
.then().statusCode(200).body(Matchers.equalTo("hello/null"));
}

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

@GET
public String helloQuery(@RestQuery LocalDateTime date) {
if (date == null) {
return "hello#null";
}
return "hello#" + date.getYear();
}

@GET
@Path("list")
public String helloQuerySet(@RestQuery @Separator(",") Set<LocalDateTime> date) {
String joinedYears = date.stream()
.map(LocalDateTime::getYear)
Expand All @@ -119,10 +152,14 @@ public String helloPath(@RestPath @DateFormat(pattern = "yyyy-MM-dd HH:mm:ss") L
@POST
public String helloForm(
@FormParam("date") @DateFormat(dateTimeFormatterProvider = CustomDateTimeFormatterProvider.class) LocalDateTime date) {
if (date == null) {
return "hello:null";
}
return "hello:" + date.getDayOfMonth();
}

@POST
@Path("list")
public String helloFormSet(
@FormParam("date") @DateFormat(dateTimeFormatterProvider = CustomDateTimeFormatterProvider.class) Set<LocalDateTime> dates) {
String joinedDays = dates.stream()
Expand All @@ -144,6 +181,12 @@ public String helloCookie(@RestCookie @DateFormat(pattern = "yyyy-MM-dd HH:mm:ss
public String helloHeader(@RestHeader @DateFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime date) {
return "hello=" + date;
}

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

public static class CustomDateTimeFormatterProvider implements DateFormat.DateTimeFormatterProvider {
Expand Down
2 changes: 1 addition & 1 deletion tcks/resteasy-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<properties>

<!-- to avoid sudden surprises, checkout is pinned to a specific commit -->
<resteasy-reactive-testsuite.repo.ref>09267f0600c682aed769b8b4e52ea0ca5ab4b639</resteasy-reactive-testsuite.repo.ref>
<resteasy-reactive-testsuite.repo.ref>2b263617d93e3f161bf092bb6425405e9d3c01d0</resteasy-reactive-testsuite.repo.ref>

<exec.skip>${skipTests}</exec.skip>
<resteasy-reactive-testsuite.clone.skip>${exec.skip}</resteasy-reactive-testsuite.clone.skip>
Expand Down
Loading