Skip to content

Commit c61d236

Browse files
authored
Merge pull request #42468 from blazmrak/fix-40833
TemporalParamConverter returns null if it has to convert empty string
2 parents ccd4f3f + f119848 commit c61d236

File tree

4 files changed

+83
-24
lines changed

4 files changed

+83
-24
lines changed

extensions/resteasy-reactive/rest/deployment/src/test/java/io/quarkus/resteasy/reactive/server/test/converters/RuntimeParamConverterTest.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void sendEmptyParameter() {
6161
.when().get("/param-converter")
6262
.then()
6363
.statusCode(200)
64-
.body(Matchers.is("Hello! You provided an empty number."));
64+
.body(Matchers.is("Hello, world! No number was provided."));
6565
}
6666

6767
@ApplicationScoped
@@ -70,12 +70,8 @@ public static class ParamConverterEndpoint {
7070

7171
@GET
7272
public Response greet(@QueryParam("number") Optional<Integer> numberOpt) {
73-
if (numberOpt != null) {
74-
if (numberOpt.isPresent()) {
75-
return Response.ok(String.format("Hello, %s!", numberOpt.get())).build();
76-
} else {
77-
return Response.ok("Hello! You provided an empty number.").build();
78-
}
73+
if (numberOpt.isPresent()) {
74+
return Response.ok(String.format("Hello, %s!", numberOpt.get())).build();
7975
} else {
8076
return Response.ok("Hello, world! No number was provided.").build();
8177
}
@@ -108,10 +104,6 @@ public static class OptionalIntegerParamConverter implements ParamConverter<Opti
108104
@Override
109105
public Optional<Integer> fromString(String value) {
110106
if (value == null) {
111-
return null;
112-
}
113-
114-
if (value.trim().isEmpty()) {
115107
return Optional.empty();
116108
}
117109

independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ResteasyReactiveRequestContext.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -821,19 +821,35 @@ public boolean isProducesChecked() {
821821
@Override
822822
public Object getHeader(String name, boolean single) {
823823
if (httpHeaders == null) {
824-
if (single)
825-
return serverRequest().getRequestHeader(name);
824+
if (single) {
825+
String header = serverRequest().getRequestHeader(name);
826+
if (header == null || header.isEmpty()) {
827+
return null;
828+
} else {
829+
return header;
830+
}
831+
}
826832
// empty collections must not be turned to null
827-
return serverRequest().getAllRequestHeaders(name);
833+
return serverRequest().getAllRequestHeaders(name).stream()
834+
.filter(h -> !h.isEmpty())
835+
.toList();
828836
} else {
829-
if (single)
830-
return httpHeaders.getMutableHeaders().getFirst(name);
837+
if (single) {
838+
String header = httpHeaders.getMutableHeaders().getFirst(name);
839+
if (header == null || header.isEmpty()) {
840+
return null;
841+
} else {
842+
return header;
843+
}
844+
}
831845
// empty collections must not be turned to null
832846
List<String> list = httpHeaders.getMutableHeaders().get(name);
833847
if (list == null) {
834848
return Collections.emptyList();
835849
} else {
836-
return list;
850+
return list.stream()
851+
.filter(h -> !h.isEmpty())
852+
.toList();
837853
}
838854
}
839855
}
@@ -846,14 +862,19 @@ public Object getQueryParameter(String name, boolean single, boolean encoded) {
846862
public Object getQueryParameter(String name, boolean single, boolean encoded, String separator) {
847863
if (single) {
848864
String val = serverRequest().getQueryParam(name);
865+
if (val != null && val.isEmpty()) {
866+
return null;
867+
}
849868
if (encoded && val != null) {
850869
val = Encode.encodeQueryParam(val);
851870
}
852871
return val;
853872
}
854873

855874
// empty collections must not be turned to null
856-
List<String> strings = serverRequest().getAllQueryParams(name);
875+
List<String> strings = serverRequest().getAllQueryParams(name).stream()
876+
.filter(p -> !p.isEmpty())
877+
.toList();
857878
if (encoded) {
858879
List<String> newStrings = new ArrayList<>();
859880
for (String i : strings) {
@@ -909,7 +930,7 @@ public Object getMatrixParameter(String name, boolean single, boolean encoded) {
909930
@Override
910931
public String getCookieParameter(String name) {
911932
Cookie cookie = getHttpHeaders().getCookies().get(name);
912-
return cookie != null ? cookie.getValue() : null;
933+
return cookie != null && !cookie.getValue().isEmpty() ? cookie.getValue() : null;
913934
}
914935

915936
@Override
@@ -919,7 +940,7 @@ public Object getFormParameter(String name, boolean single, boolean encoded) {
919940
}
920941
if (single) {
921942
FormValue val = formData.getFirst(name);
922-
if (val == null || val.isFileItem()) {
943+
if (val == null || val.isFileItem() || val.getValue().isEmpty()) {
923944
return null;
924945
}
925946
if (encoded) {
@@ -931,15 +952,18 @@ public Object getFormParameter(String name, boolean single, boolean encoded) {
931952
List<String> strings = new ArrayList<>();
932953
if (val != null) {
933954
for (FormValue i : val) {
955+
if (i.getValue().isEmpty()) {
956+
continue;
957+
}
934958
if (encoded) {
935959
strings.add(Encode.encodeQueryParam(i.getValue()));
936960
} else {
937961
strings.add(i.getValue());
938962
}
939963
}
940964
}
941-
return strings;
942965

966+
return strings;
943967
}
944968

945969
@Override

independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/LocalDateTimeParamTest.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.time.LocalDateTime;
44
import java.time.format.DateTimeFormatter;
5+
import java.util.List;
56
import java.util.Optional;
67
import java.util.Set;
78
import java.util.stream.Collectors;
@@ -37,12 +38,18 @@ public class LocalDateTimeParamTest {
3738
public void localDateTimeAsQueryParam() {
3839
RestAssured.get("/hello?date=1984-08-08T01:02:03")
3940
.then().statusCode(200).body(Matchers.equalTo("hello#1984"));
41+
42+
RestAssured.get("/hello?date=")
43+
.then().statusCode(200).body(Matchers.equalTo("hello#null"));
4044
}
4145

4246
@Test
4347
public void localDateTimeCollectionAsQueryParam() {
44-
RestAssured.get("/hello?date=1984-08-08T01:02:03,1992-04-25T01:02:03")
48+
RestAssured.get("/hello/list?date=1984-08-08T01:02:03,1992-04-25T01:02:03")
4549
.then().statusCode(200).body(Matchers.equalTo("hello#1984,1992"));
50+
51+
RestAssured.get("/hello/list?date=&date=1984-08-08T01:02:03")
52+
.then().statusCode(200).body(Matchers.equalTo("hello#1984"));
4653
}
4754

4855
@Test
@@ -64,11 +71,14 @@ public void localDateTimeAsPathParam() {
6471
public void localDateTimeAsFormParam() {
6572
RestAssured.given().formParam("date", "1995/09/22 01:02").post("/hello")
6673
.then().statusCode(200).body(Matchers.equalTo("hello:22"));
74+
75+
RestAssured.given().formParam("date", "").post("/hello")
76+
.then().statusCode(200).body(Matchers.equalTo("hello:null"));
6777
}
6878

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

@@ -77,24 +87,47 @@ public void localDateTimeAsHeader() {
7787
RestAssured.with().header("date", "1984-08-08 01:02:03")
7888
.get("/hello/header")
7989
.then().statusCode(200).body(Matchers.equalTo("hello=1984-08-08T01:02:03"));
90+
91+
RestAssured.with().header("date", "")
92+
.get("/hello/header")
93+
.then().statusCode(200).body(Matchers.equalTo("hello=null"));
94+
}
95+
96+
@Test
97+
public void localDateTimeAsHeaderList() {
98+
RestAssured.with().header("date", "", "1984-08-08 01:02:03", "")
99+
.get("/hello/header/list")
100+
.then().statusCode(200).body(Matchers.equalTo("hello=[1984-08-08T01:02:03]"));
101+
102+
RestAssured.with().header("date", "")
103+
.get("/hello/header/list")
104+
.then().statusCode(200).body(Matchers.equalTo("hello=[]"));
80105
}
81106

82107
@Test
83108
public void localDateTimeAsCookie() {
84109
RestAssured.with().cookie("date", "1984-08-08 01:02:03")
85110
.get("/hello/cookie")
86111
.then().statusCode(200).body(Matchers.equalTo("hello/1984-08-08T01:02:03"));
112+
113+
RestAssured.with().cookie("date", "")
114+
.get("/hello/cookie")
115+
.then().statusCode(200).body(Matchers.equalTo("hello/null"));
87116
}
88117

89118
@Path("hello")
90119
public static class HelloResource {
91120

92121
@GET
93122
public String helloQuery(@RestQuery LocalDateTime date) {
123+
if (date == null) {
124+
return "hello#null";
125+
}
94126
return "hello#" + date.getYear();
95127
}
96128

97129
@GET
130+
@Path("list")
98131
public String helloQuerySet(@RestQuery @Separator(",") Set<LocalDateTime> date) {
99132
String joinedYears = date.stream()
100133
.map(LocalDateTime::getYear)
@@ -119,10 +152,14 @@ public String helloPath(@RestPath @DateFormat(pattern = "yyyy-MM-dd HH:mm:ss") L
119152
@POST
120153
public String helloForm(
121154
@FormParam("date") @DateFormat(dateTimeFormatterProvider = CustomDateTimeFormatterProvider.class) LocalDateTime date) {
155+
if (date == null) {
156+
return "hello:null";
157+
}
122158
return "hello:" + date.getDayOfMonth();
123159
}
124160

125161
@POST
162+
@Path("list")
126163
public String helloFormSet(
127164
@FormParam("date") @DateFormat(dateTimeFormatterProvider = CustomDateTimeFormatterProvider.class) Set<LocalDateTime> dates) {
128165
String joinedDays = dates.stream()
@@ -144,6 +181,12 @@ public String helloCookie(@RestCookie @DateFormat(pattern = "yyyy-MM-dd HH:mm:ss
144181
public String helloHeader(@RestHeader @DateFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime date) {
145182
return "hello=" + date;
146183
}
184+
185+
@Path("header/list")
186+
@GET
187+
public String helloHeaderList(@RestHeader @DateFormat(pattern = "yyyy-MM-dd HH:mm:ss") List<LocalDateTime> date) {
188+
return "hello=" + date;
189+
}
147190
}
148191

149192
public static class CustomDateTimeFormatterProvider implements DateFormat.DateTimeFormatterProvider {

tcks/resteasy-reactive/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<properties>
1818

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

2222
<exec.skip>${skipTests}</exec.skip>
2323
<resteasy-reactive-testsuite.clone.skip>${exec.skip}</resteasy-reactive-testsuite.clone.skip>

0 commit comments

Comments
 (0)