Skip to content

Commit 9ea19b1

Browse files
committed
Netty: ctx.form() throws NullPointerException fix #1338
1 parent 5eab7e4 commit 9ea19b1

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ private FileUpload register(FileUpload upload) {
533533
}
534534

535535
private void decodeForm(HttpRequest req, Formdata form) {
536+
if (decoder == null) {
537+
// empty/bad form
538+
return;
539+
}
536540
try {
537541
while (decoder.hasNext()) {
538542
HttpData next = (HttpData) decoder.next();

modules/jooby-utow/src/main/java/io/jooby/internal/utow/UtowContext.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,17 @@ void destroy(Exception cause) {
398398
}
399399

400400
private void formData(Formdata form, FormData data) {
401-
Iterator<String> it = data.iterator();
402-
while (it.hasNext()) {
403-
String path = it.next();
404-
Deque<FormData.FormValue> values = data.get(path);
405-
for (FormData.FormValue value : values) {
406-
if (value.isFileItem()) {
407-
form.put(path, new UtowFileUpload(path, value));
408-
} else {
409-
form.put(path, value.getValue());
401+
if (data != null) {
402+
Iterator<String> it = data.iterator();
403+
while (it.hasNext()) {
404+
String path = it.next();
405+
Deque<FormData.FormValue> values = data.get(path);
406+
for (FormData.FormValue value : values) {
407+
if (value.isFileItem()) {
408+
form.put(path, new UtowFileUpload(path, value));
409+
} else {
410+
form.put(path, value.getValue());
411+
}
410412
}
411413
}
412414
}

tests/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@
175175
</execution>
176176
</executions>
177177
</plugin>
178+
<!-- sure-fire -->
179+
<plugin>
180+
<groupId>org.apache.maven.plugins</groupId>
181+
<artifactId>maven-surefire-plugin</artifactId>
182+
<configuration>
183+
<skip>false</skip>
184+
<includes>
185+
<include>**/*Test.java</include>
186+
<include>**/*Feature.java</include>
187+
<include>**/*Issue*.java</include>
188+
</includes>
189+
</configuration>
190+
</plugin>
178191
</plugins>
179192
</build>
180193

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.jooby;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static io.restassured.RestAssured.given;
7+
import static org.hamcrest.Matchers.equalTo;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class Issue1338 {
11+
@Test
12+
@DisplayName("Form should not fail when empty")
13+
public void issue1338() {
14+
new JoobyRunner(app -> {
15+
app.post("/1338/form", ctx -> ctx.form().toMap());
16+
17+
app.post("/1338/multipart", ctx -> ctx.form().toMap());
18+
19+
}).ready(client -> {
20+
client.post("/1338/form", rsp -> {
21+
assertEquals("{}", rsp.body().string());
22+
});
23+
given()
24+
.port(client.getPort())
25+
.contentType("multipart/form-data;")
26+
.when()
27+
.post("/1338/multipart")
28+
.then()
29+
.assertThat()
30+
.body(equalTo("{}"));
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)