Skip to content

Commit 93edd99

Browse files
committed
jackson/json: add parameter names module fix #616
1 parent 0685cac commit 93edd99

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.json.Jackson;
4+
import org.jooby.test.ServerFeature;
5+
import org.junit.Test;
6+
7+
public class Issue616 extends ServerFeature {
8+
9+
public static class Person {
10+
11+
// mandatory fields
12+
private final String name;
13+
private final String surname;
14+
15+
// optional fields
16+
private String nickname;
17+
18+
public Person(final String name, final String surname) {
19+
this.name = name;
20+
this.surname = surname;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public String getSurname() {
28+
return surname;
29+
}
30+
31+
public String getNickname() {
32+
return nickname;
33+
}
34+
35+
public void setNickname(final String nickname) {
36+
this.nickname = nickname;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return name + " " + surname + "(" + nickname + ")";
42+
}
43+
}
44+
45+
{
46+
use(new Jackson());
47+
post("/616", req -> {
48+
return req.body(Person.class).toString();
49+
});
50+
}
51+
52+
@Test
53+
public void jacksonWithParamNamesModule() throws Exception {
54+
request()
55+
.post("/616")
56+
.body("{\"name\":\"N\",\"surname\":\"S\"}", "application/json")
57+
.expect("\"N S(null)\"");
58+
59+
request()
60+
.post("/616")
61+
.body("{\"name\":\"N\",\"surname\":\"S\",\"nickname\":\".\"}", "application/json")
62+
.expect("\"N S(.)\"");
63+
}
64+
}

jooby-jackson/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<artifactId>jackson-databind</artifactId>
5555
</dependency>
5656

57+
<dependency>
58+
<groupId>com.fasterxml.jackson.module</groupId>
59+
<artifactId>jackson-module-parameter-names</artifactId>
60+
</dependency>
61+
5762
<!-- Test dependencies -->
5863
<dependency>
5964
<groupId>org.jooby</groupId>

jooby-jackson/src/main/java/org/jooby/json/Jackson.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.fasterxml.jackson.databind.ObjectMapper;
4242
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
4343
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
44+
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
4445
import com.google.inject.Binder;
4546
import com.google.inject.Key;
4647
import com.google.inject.multibindings.Multibinder;
@@ -218,13 +219,13 @@ public void configure(final Env env, final Config config, final Binder binder) {
218219
m.setDateFormat(new SimpleDateFormat(config.getString("application.dateFormat"), locale));
219220
m.setLocale(locale);
220221
m.setTimeZone(TimeZone.getTimeZone(config.getString("application.tz")));
222+
// default modules:
223+
m.registerModule(new Jdk8Module());
224+
m.registerModule(new JavaTimeModule());
225+
m.registerModule(new ParameterNamesModule());
221226
return m;
222227
});
223228

224-
// some java 8 modules
225-
mapper.registerModule(new Jdk8Module());
226-
mapper.registerModule(new JavaTimeModule());
227-
228229
if (configurer != null) {
229230
configurer.accept(mapper);
230231
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,12 @@
821821
<version>${jackson.version}</version>
822822
</dependency>
823823

824+
<dependency>
825+
<groupId>com.fasterxml.jackson.module</groupId>
826+
<artifactId>jackson-module-parameter-names</artifactId>
827+
<version>${jackson.version}</version>
828+
</dependency>
829+
824830
<dependency>
825831
<groupId>com.fasterxml.jackson.datatype</groupId>
826832
<artifactId>jackson-datatype-guava</artifactId>

0 commit comments

Comments
 (0)