Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit ccea9ad

Browse files
committed
param/body API: shortcut methods fix jooby-project#433
1 parent 81e44ec commit ccea9ad

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class Issue433 extends ServerFeature {
7+
8+
public static class Bean {
9+
String q;
10+
}
11+
12+
{
13+
get("/433", req -> {
14+
Bean bean = req.params(Bean.class);
15+
return bean.q;
16+
});
17+
18+
post("/433", req -> {
19+
Bean bean = req.body(Bean.class);
20+
return bean.q;
21+
});
22+
}
23+
24+
@Test
25+
public void shortCutMethods() throws Exception {
26+
request()
27+
.get("/433?q=q1")
28+
.expect("q1");
29+
30+
request()
31+
.post("/433")
32+
.form()
33+
.add("q", "q2")
34+
.expect("q2");
35+
}
36+
}

jooby/src/main/java/org/jooby/Request.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ public Mutant params() {
133133
return req.params();
134134
}
135135

136+
@Override
137+
public <T> T params(final Class<T> type) {
138+
return req.params(type);
139+
}
140+
136141
@Override
137142
public Mutant param(final String name) {
138143
return req.param(name);
@@ -173,6 +178,11 @@ public Mutant body() throws Exception {
173178
return req.body();
174179
}
175180

181+
@Override
182+
public <T> T body(final Class<T> type) throws Exception {
183+
return req.body(type);
184+
}
185+
176186
@Override
177187
public <T> T require(final Class<T> type) {
178188
return req.require(type);
@@ -550,6 +560,16 @@ default Optional<MediaType> accepts(final MediaType... types) {
550560
*/
551561
Mutant params();
552562

563+
/**
564+
* Short version of <code>params().to(type)</code>.
565+
*
566+
* @param type Object type.
567+
* @return Instance of object.
568+
*/
569+
default <T> T params(final Class<T> type) {
570+
return params().to(type);
571+
}
572+
553573
/**
554574
* Get a HTTP request parameter under the given name. A HTTP parameter can be provided in any of
555575
* these forms:
@@ -635,6 +655,17 @@ default List<Upload> files(final String name) {
635655
*/
636656
Mutant body() throws Exception;
637657

658+
/**
659+
* Short version of <code>body().to(type)</code>.
660+
*
661+
* @param type Object type.
662+
* @return Instance of object.
663+
* @throws Exception If body can't be converted or there is no HTTP body.
664+
*/
665+
default <T> T body(final Class<T> type) throws Exception {
666+
return body().to(type);
667+
}
668+
638669
/**
639670
* The charset defined in the request body. If the request doesn't specify a character
640671
* encoding, this method return the global charset: <code>application.charset</code>.

jooby/src/test/java/org/jooby/RequestForwardingTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,4 +739,37 @@ public void form() throws Exception {
739739
});
740740
}
741741

742+
@Test
743+
public void bodyWithType() throws Exception {
744+
RequestForwardingTest v = new RequestForwardingTest();
745+
new MockUnit(Request.class, Map.class)
746+
.expect(unit -> {
747+
Request req = unit.get(Request.class);
748+
749+
expect(req.body(RequestForwardingTest.class)).andReturn(v);
750+
})
751+
.run(unit -> {
752+
assertEquals(
753+
v,
754+
new Request.Forwarding(unit.get(Request.class)).body(
755+
RequestForwardingTest.class));
756+
});
757+
}
758+
759+
@Test
760+
public void paramsWithType() throws Exception {
761+
RequestForwardingTest v = new RequestForwardingTest();
762+
new MockUnit(Request.class, Map.class)
763+
.expect(unit -> {
764+
Request req = unit.get(Request.class);
765+
766+
expect(req.params(RequestForwardingTest.class)).andReturn(v);
767+
})
768+
.run(unit -> {
769+
assertEquals(
770+
v,
771+
new Request.Forwarding(unit.get(Request.class)).params(
772+
RequestForwardingTest.class));
773+
});
774+
}
742775
}

0 commit comments

Comments
 (0)