Skip to content

Commit 3eee565

Browse files
committed
unify BodyParser and ParamConverter as one API fix jooby-project#94
Replace BodyParser with Parser fix jooby-project#95 replace ParamConverter with Parser fix jooby-project#96 replace req.body(type) with req.body().to(type) fix jooby-project#97 replace req.params(type) with req.params().to(type) fix jooby-project#98 @Body annotation is required on MVC handler fix jooby-project#99
1 parent 6051c09 commit 3eee565

88 files changed

Lines changed: 2043 additions & 1695 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

coverage-report/src/test/java/org/jooby/AcceptHeaderFeature.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package org.jooby;
22

3-
import org.jooby.MediaType;
3+
import java.util.List;
4+
45
import org.jooby.test.ServerFeature;
56
import org.junit.Test;
67

78
public class AcceptHeaderFeature extends ServerFeature {
89

910
{
10-
get("/", (req, rsp) -> rsp.send(req.accept()));
11+
get("/", req -> req.accept());
1112

12-
get("/accept",
13-
(req, rsp) -> rsp.send(req.accepts(req.param("type").toList(MediaType.class))
14-
.map(MediaType::toString).orElse("nope")));
13+
get("/accept", req -> {
14+
List<MediaType> types = req.param("type").toList(MediaType.class);
15+
return req.accepts(types)
16+
.map(MediaType::toString).orElse("nope");
17+
});
1518
}
1619

1720
@Test

coverage-report/src/test/java/org/jooby/BodyConverters.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,10 @@
33
import java.util.List;
44

55
import com.google.common.collect.ImmutableList;
6-
import com.google.common.io.CharStreams;
7-
import com.google.inject.TypeLiteral;
86

97
public class BodyConverters {
108

11-
public static final BodyParser fromJson = new BodyParser() {
12-
13-
@Override
14-
public boolean canParse(final TypeLiteral<?> type) {
15-
return true;
16-
}
17-
18-
@Override
19-
public <T> T parse(final TypeLiteral<T> type, final BodyParser.Context ctx) throws Exception {
20-
return ctx.text(r -> CharStreams.toString(r));
21-
}
22-
23-
@Override
24-
public List<MediaType> types() {
25-
return ImmutableList.of(MediaType.json);
26-
}
27-
28-
};
9+
public static final Parser fromJson = (type, ctx) -> ctx.body(body -> body.text());
2910

3011
public static final BodyFormatter toJson = new BodyFormatter() {
3112

coverage-report/src/test/java/org/jooby/BodyParamFeature.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public Bean body(final Bean param) throws IOException {
3939

4040
{
4141

42-
param((type, values, ctx) -> {
43-
if (values != null) {
44-
return new Bean(values[0]);
42+
parser((type, ctx) -> {
43+
if (type.getRawType() == Bean.class) {
44+
return ctx.param(values -> new Bean(values.get(0)));
4545
}
46-
return ctx.convert(type, values);
46+
return ctx.next();
4747
});
4848

4949
use(new BodyFormatter() {
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package org.jooby;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.time.LocalDate;
5+
import java.time.format.DateTimeFormatter;
6+
import java.util.Date;
7+
import java.util.Locale;
8+
import java.util.Optional;
9+
10+
import org.jooby.mvc.Body;
11+
import org.jooby.mvc.POST;
12+
import org.jooby.mvc.Path;
13+
import org.jooby.test.ServerFeature;
14+
import org.junit.Test;
15+
16+
public class BuiltinBodyParserFeature extends ServerFeature {
17+
18+
public enum VOWELS {
19+
A,
20+
B;
21+
}
22+
23+
@Path("/r")
24+
public static class Resource {
25+
26+
@POST
27+
@Path("/optional/int")
28+
public Object intBody(final @Body Optional<Integer> p) {
29+
return p.toString();
30+
}
31+
32+
@POST
33+
@Path("/locale")
34+
public Object localeBody(final @Body Locale l) {
35+
return l;
36+
}
37+
38+
@POST
39+
@Path("/enum")
40+
public Object enumBody(final @Body VOWELS v) {
41+
return v;
42+
}
43+
44+
@POST
45+
@Path("/date")
46+
public Object dateBody(final @Body Date d) {
47+
return new SimpleDateFormat("dd-MM-yy").format(d);
48+
}
49+
50+
@POST
51+
@Path("/ldate")
52+
public Object dateBody(final @Body LocalDate d) {
53+
return d.format(DateTimeFormatter.ofPattern("dd-MM-yy"));
54+
}
55+
56+
}
57+
58+
{
59+
60+
use(Resource.class);
61+
62+
post("/optional/int", req -> {
63+
return req.body().toOptional(int.class);
64+
});
65+
66+
post("/locale", req -> {
67+
return req.body().to(Locale.class);
68+
});
69+
70+
post("/enum", req -> {
71+
return req.body().to(VOWELS.class);
72+
});
73+
74+
post("/date", req -> {
75+
return new SimpleDateFormat("dd-MM-yy").format(req.body().to(Date.class));
76+
});
77+
78+
post("/ldate", req -> {
79+
return req.body().to(LocalDate.class).format(DateTimeFormatter.ofPattern("dd-MM-yy"));
80+
});
81+
}
82+
83+
@Test
84+
public void optionalBody() throws Exception {
85+
request()
86+
.post("/optional/int")
87+
.body("7", "text/palin")
88+
.expect("Optional[7]");
89+
90+
request()
91+
.post("/optional/int")
92+
.expect("Optional.empty");
93+
94+
request()
95+
.post("/r/optional/int")
96+
.body("7", "text/palin")
97+
.expect("Optional[7]");
98+
99+
request()
100+
.post("/r/optional/int")
101+
.expect("Optional.empty");
102+
}
103+
104+
@Test
105+
public void localeBody() throws Exception {
106+
request()
107+
.post("/locale")
108+
.body("es_AR", "text/palin")
109+
.expect("es_AR");
110+
111+
request()
112+
.post("/r/locale")
113+
.body("es_AR", "text/palin")
114+
.expect("es_AR");
115+
116+
}
117+
118+
@Test
119+
public void enumBody() throws Exception {
120+
request()
121+
.post("/enum")
122+
.body("A", "text/palin")
123+
.expect("A");
124+
125+
request()
126+
.post("/r/enum")
127+
.body("A", "text/palin")
128+
.expect("A");
129+
130+
}
131+
132+
@Test
133+
public void dateBody() throws Exception {
134+
request()
135+
.post("/date")
136+
.body("10-05-15", "text/palin")
137+
.expect("10-05-15");
138+
139+
request()
140+
.post("/r/date")
141+
.body("10-05-15", "text/palin")
142+
.expect("10-05-15");
143+
}
144+
145+
@Test
146+
public void localDateBody() throws Exception {
147+
request()
148+
.post("/ldate")
149+
.body("10-05-15", "text/palin")
150+
.expect("10-05-15");
151+
152+
request()
153+
.post("/r/ldate")
154+
.body("10-05-15", "text/palin")
155+
.expect("10-05-15");
156+
}
157+
158+
}

coverage-report/src/test/java/org/jooby/ConfigPropertiesFeature.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import javax.inject.Inject;
88
import javax.inject.Named;
99

10-
import org.jooby.Env;
1110
import org.jooby.mvc.GET;
1211
import org.jooby.mvc.Path;
1312
import org.jooby.test.ServerFeature;
@@ -17,6 +16,20 @@
1716

1817
public class ConfigPropertiesFeature extends ServerFeature {
1918

19+
public static class StrConstructor {
20+
21+
private String value;
22+
23+
public StrConstructor(final String value) {
24+
this.value = value;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return value;
30+
}
31+
}
32+
2033
public static class ValueOf {
2134

2235
private String value;
@@ -63,6 +76,7 @@ public static class Resource {
6376
private Letter letter;
6477
private UUID uuid;
6578
private ValueOf valueOf;
79+
private StrConstructor strConstructor;
6680

6781
@Inject
6882
public Resource(final Charset charset, final Env mode,
@@ -71,7 +85,8 @@ public Resource(final Charset charset, final Env mode,
7185
@Named("list") final List<String> list,
7286
@Named("letter") final Letter letter,
7387
@Named("uuid") final UUID uuid,
74-
@Named("valueOf") final ValueOf valueOf) {
88+
@Named("valueOf") final ValueOf valueOf,
89+
@Named("strconst") final StrConstructor strConstructor) {
7590
this.charset = charset;
7691
this.mode = mode;
7792
this.intprop = intprop;
@@ -80,12 +95,14 @@ public Resource(final Charset charset, final Env mode,
8095
this.letter = letter;
8196
this.uuid = uuid;
8297
this.valueOf = valueOf;
98+
this.strConstructor = strConstructor;
8399
}
84100

85101
@GET
86102
@Path("/properties")
87103
public Object properties() {
88-
return charset + " " + intprop + " " + stringprop + " " + uuid + " " + valueOf;
104+
return charset + " " + intprop + " " + stringprop + " " + uuid + " " + valueOf + " "
105+
+ strConstructor;
89106
}
90107

91108
@GET
@@ -116,7 +133,7 @@ public Object letter() {
116133
public void properties() throws Exception {
117134
request()
118135
.get("/r/properties")
119-
.expect("UTF-8 14 The man who sold the world a8843f4a-2c71-42ef-82aa-83fa8246c0d4 valueOf");
136+
.expect("UTF-8 14 The man who sold the world a8843f4a-2c71-42ef-82aa-83fa8246c0d4 valueOf strconst");
120137
}
121138

122139
@Test

coverage-report/src/test/java/org/jooby/ErrDefaultStatusCodeFeature.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import java.util.NoSuchElementException;
44

5-
import org.jooby.Err;
6-
import org.jooby.Status;
75
import org.jooby.test.ServerFeature;
86
import org.junit.Test;
97

@@ -31,7 +29,7 @@ public class ErrDefaultStatusCodeFeature extends ServerFeature {
3129
}).produces("json");
3230

3331
post("/UnsupportedMediaType", (req, rsp) -> {
34-
rsp.send(req.body(String.class));
32+
rsp.send(req.body().to(String.class));
3533
}).consumes("json");
3634

3735
}

coverage-report/src/test/java/org/jooby/ParamBeanFeature.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Optional;
66

7+
import org.jooby.mvc.Body;
78
import org.jooby.mvc.Path;
89
import org.jooby.test.ServerFeature;
910
import org.junit.Test;
@@ -71,7 +72,7 @@ public String getibean(final org.jooby.Request req, final IBean bean) throws Exc
7172

7273
@org.jooby.mvc.POST
7374
@Path("/ibean")
74-
public String postibean(final org.jooby.Request req, final IBean bean) throws Exception {
75+
public String postibean(final org.jooby.Request req, final @Body IBean bean) throws Exception {
7576
assertEquals(req.param("name").value(), bean.name());
7677
assertEquals(req.param("valid").booleanValue(), bean.isValid());
7778
assertEquals(req.param("age").intValue(), bean.getAge());
@@ -122,49 +123,49 @@ public String invalidbean(final InvalidBean bean) throws Exception {
122123

123124
{
124125
get("/ibean", req -> {
125-
IBean bean = req.params(IBean.class);
126+
IBean bean = req.params().to(IBean.class);
126127
assertEquals(req.param("name").value(), bean.name());
127128
assertEquals(req.param("valid").booleanValue(), bean.isValid());
128129
assertEquals(req.param("age").intValue(), bean.getAge());
129130
return "OK";
130131
});
131132

132133
post("/ibean", req -> {
133-
IBean bean = req.body(IBean.class);
134+
IBean bean = req.body().to(IBean.class);
134135
assertEquals(req.param("name").value(), bean.name());
135136
assertEquals(req.param("valid").booleanValue(), bean.isValid());
136137
assertEquals(req.param("age").intValue(), bean.getAge());
137138
return "OK";
138139
});
139140

140141
get("/beanwithargs", req -> {
141-
BeanWithArgs bean = req.params(BeanWithArgs.class);
142+
BeanWithArgs bean = req.params().to(BeanWithArgs.class);
142143
assertEquals(req.param("name").value(), bean.name);
143144
assertEquals(req.param("age").intValue(), (int) bean.age.get());
144145
return "OK";
145146
});
146147

147148
get("/invalidbean", req -> {
148-
req.params(InvalidBean.class);
149+
req.params().to(InvalidBean.class);
149150
return "OK";
150151
});
151152

152153
post("/beanwithargs", req -> {
153-
BeanWithArgs bean = req.body(BeanWithArgs.class);
154+
BeanWithArgs bean = req.body().to(BeanWithArgs.class);
154155
assertEquals(req.param("name").value(), bean.name);
155156
assertEquals(req.param("age").intValue(), (int) bean.age.get());
156157
return "OK";
157158
});
158159

159160
get("/beannoarg", req -> {
160-
BeanNoArg bean = req.params(BeanNoArg.class);
161+
BeanNoArg bean = req.params().to(BeanNoArg.class);
161162
assertEquals(req.param("name").value(), bean.getName());
162163
assertEquals(req.param("age").intValue(), (int) bean.getAge().get());
163164
return "OK";
164165
});
165166

166167
post("/beannoarg", req -> {
167-
BeanNoArg bean = req.body(BeanNoArg.class);
168+
BeanNoArg bean = req.body().to(BeanNoArg.class);
168169
assertEquals(req.param("name").value(), bean.getName());
169170
assertEquals(req.param("age").intValue(), (int) bean.getAge().get());
170171
return "OK";

0 commit comments

Comments
 (0)