Skip to content

Commit 13f59e0

Browse files
authored
default mapper cannot handle basic immutable class (#3898)
1 parent 59ea818 commit 13f59e0

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package com.fasterxml.jackson.databind.deser.creators;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
8+
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
9+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNotNull;
13+
14+
import org.junit.jupiter.api.Test;
15+
16+
// [databind#3898]
17+
public class SingleImmutableFieldCreatorTest
18+
extends DatabindTestUtil
19+
{
20+
static class ImmutableId {
21+
private final int id;
22+
23+
public ImmutableId(int id) { this.id = id; }
24+
25+
public int getId() {
26+
return id;
27+
}
28+
}
29+
30+
static class ImmutableIdWithEmptyConstuctor {
31+
private final int id;
32+
33+
public ImmutableIdWithEmptyConstuctor() { this(-1); }
34+
35+
public ImmutableIdWithEmptyConstuctor(int id) { this.id = id; }
36+
37+
public int getId() {
38+
return id;
39+
}
40+
}
41+
42+
static class ImmutableIdWithJsonCreatorAnnotation {
43+
private final int id;
44+
45+
@JsonCreator
46+
public ImmutableIdWithJsonCreatorAnnotation(int id) { this.id = id; }
47+
48+
public int getId() {
49+
return id;
50+
}
51+
}
52+
53+
static class ImmutableIdWithJsonPropertyFieldAnnotation {
54+
@JsonProperty("id") private final int id;
55+
56+
public ImmutableIdWithJsonPropertyFieldAnnotation(int id) { this.id = id; }
57+
58+
public int getId() {
59+
return id;
60+
}
61+
}
62+
63+
static class ImmutableIdWithJsonPropertyConstructorAnnotation {
64+
private final int id;
65+
66+
public ImmutableIdWithJsonPropertyConstructorAnnotation(@JsonProperty("id") int id) { this.id = id; }
67+
68+
public int getId() {
69+
return id;
70+
}
71+
}
72+
73+
static class MyParamIntrospector extends JacksonAnnotationIntrospector
74+
{
75+
private static final long serialVersionUID = 1L;
76+
77+
@Override
78+
public String findImplicitPropertyName(AnnotatedMember param) {
79+
return "id";
80+
}
81+
}
82+
83+
/*
84+
/**********************************************************
85+
/* Unit tests
86+
/**********************************************************
87+
*/
88+
89+
@Test
90+
public void testSetterlessProperty() throws Exception
91+
{
92+
ImmutableId input = new ImmutableId(13);
93+
ObjectMapper m = new ObjectMapper();
94+
m.setAnnotationIntrospector(new MyParamIntrospector());
95+
String json = m.writerWithDefaultPrettyPrinter().writeValueAsString(input);
96+
97+
ImmutableId output = m.readValue(json, ImmutableId.class);
98+
assertNotNull(output);
99+
100+
assertEquals(input.id, output.id);
101+
}
102+
103+
// in the past, this was a workaround for the first test
104+
@Test
105+
public void testSetterlessPropertyWithEmptyConstructor() throws Exception
106+
{
107+
ImmutableIdWithEmptyConstuctor input = new ImmutableIdWithEmptyConstuctor(13);
108+
ObjectMapper m = new ObjectMapper();
109+
String json = m.writerWithDefaultPrettyPrinter().writeValueAsString(input);
110+
111+
ImmutableIdWithEmptyConstuctor output = m.readValue(json, ImmutableIdWithEmptyConstuctor.class);
112+
assertNotNull(output);
113+
114+
assertEquals(input.id, output.id);
115+
}
116+
117+
@Test
118+
public void testSetterlessPropertyWithJsonCreator() throws Exception
119+
{
120+
ImmutableIdWithJsonCreatorAnnotation input = new ImmutableIdWithJsonCreatorAnnotation(13);
121+
ObjectMapper m = new ObjectMapper();
122+
m.setAnnotationIntrospector(new MyParamIntrospector());
123+
String json = m.writerWithDefaultPrettyPrinter().writeValueAsString(input);
124+
125+
ImmutableIdWithJsonCreatorAnnotation output =
126+
m.readValue(json, ImmutableIdWithJsonCreatorAnnotation.class);
127+
assertNotNull(output);
128+
129+
assertEquals(input.id, output.id);
130+
}
131+
132+
// in the past, this was a workaround for the first test
133+
@Test
134+
public void testSetterlessPropertyWithJsonPropertyField() throws Exception
135+
{
136+
ImmutableIdWithJsonPropertyConstructorAnnotation input = new ImmutableIdWithJsonPropertyConstructorAnnotation(13);
137+
ObjectMapper m = new ObjectMapper();
138+
String json = m.writerWithDefaultPrettyPrinter().writeValueAsString(input);
139+
140+
ImmutableIdWithJsonPropertyConstructorAnnotation output =
141+
m.readValue(json, ImmutableIdWithJsonPropertyConstructorAnnotation.class);
142+
assertNotNull(output);
143+
144+
assertEquals(input.id, output.id);
145+
}
146+
147+
@Test
148+
public void testSetterlessPropertyWithJsonPropertyConstructor() throws Exception
149+
{
150+
ImmutableIdWithJsonPropertyFieldAnnotation input = new ImmutableIdWithJsonPropertyFieldAnnotation(13);
151+
ObjectMapper m = new ObjectMapper();
152+
m.setAnnotationIntrospector(new MyParamIntrospector());
153+
String json = m.writerWithDefaultPrettyPrinter().writeValueAsString(input);
154+
155+
ImmutableIdWithJsonPropertyFieldAnnotation output =
156+
m.readValue(json, ImmutableIdWithJsonPropertyFieldAnnotation.class);
157+
assertNotNull(output);
158+
159+
assertEquals(input.id, output.id);
160+
}
161+
}

0 commit comments

Comments
 (0)