Skip to content

Commit d6f6e37

Browse files
committed
Add Records+ReadOnly test cases for issue FasterXML#4119.
1 parent 7e59a33 commit d6f6e37

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package com.fasterxml.jackson.databind.records;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.fasterxml.jackson.annotation.JsonProperty.Access;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
public class RecordWithReadOnlyTest extends DatabindTestUtil {
12+
13+
record RecordWithReadOnly(int id, @JsonProperty(access = Access.READ_ONLY) String name) {
14+
}
15+
16+
record RecordWithReadOnlyNamedProperty(int id,
17+
@JsonProperty(value = "name", access = Access.READ_ONLY) String name) {
18+
}
19+
20+
record RecordWithReadOnlyAccessor(int id, String name) {
21+
22+
@JsonProperty(access = Access.READ_ONLY)
23+
@Override
24+
public String name() {
25+
return name;
26+
}
27+
}
28+
29+
record RecordWithReadOnlyComponentOverriddenAccessor(int id, @JsonProperty(access = Access.READ_ONLY) String name) {
30+
31+
// @JsonProperty on overridden method is not automatically inherited by overriding method
32+
@Override
33+
public String name() {
34+
return name;
35+
}
36+
}
37+
38+
record RecordWithReadOnlyPrimitiveType(@JsonProperty(access = Access.READ_ONLY) int id, String name) {
39+
}
40+
41+
record RecordWithReadOnlyAll(@JsonProperty(access = Access.READ_ONLY) int id,
42+
@JsonProperty(access = Access.READ_ONLY) String name) {
43+
}
44+
45+
record RecordWithReadOnlyAllAndNoArgConstructor(@JsonProperty(access = Access.READ_ONLY) int id,
46+
@JsonProperty(access = Access.READ_ONLY) String name) {
47+
48+
public RecordWithReadOnlyAllAndNoArgConstructor() {
49+
this(-1, "no-arg");
50+
}
51+
}
52+
53+
private final ObjectMapper MAPPER = newJsonMapper();
54+
55+
/*
56+
/**********************************************************************
57+
/* Test methods, JsonProperty.access=READ_ONLY
58+
/**********************************************************************
59+
*/
60+
61+
@Test
62+
public void testSerializeReadOnlyProperty() throws Exception {
63+
String json = MAPPER.writeValueAsString(new RecordWithReadOnly(123, "Bob"));
64+
assertEquals(a2q("{'id':123,'name':'Bob'}"), json);
65+
}
66+
67+
@Test
68+
public void testDeserializeReadOnlyProperty() throws Exception {
69+
RecordWithReadOnly value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnly.class);
70+
assertEquals(new RecordWithReadOnly(123, null), value);
71+
}
72+
73+
/*
74+
/**********************************************************************
75+
/* Test methods, JsonProperty.access=READ_ONLY + JsonProperty.value=...
76+
/**********************************************************************
77+
*/
78+
79+
@Test
80+
public void testSerializeReadOnlyNamedProperty() throws Exception {
81+
String json = MAPPER.writeValueAsString(new RecordWithReadOnlyNamedProperty(123, "Bob"));
82+
assertEquals(a2q("{'id':123,'name':'Bob'}"), json);
83+
}
84+
85+
/**
86+
* Currently documents a bug where a property was NOT ignored during deserialization if given an explicit name.
87+
* Also reproducible in 2.14.x.
88+
*/
89+
@Test
90+
public void testDeserializeReadOnlyNamedProperty() throws Exception {
91+
RecordWithReadOnlyNamedProperty value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyNamedProperty.class);
92+
assertEquals(new RecordWithReadOnlyNamedProperty(123, "Bob"), value); // BUG: should be `null` instead of "Bob"
93+
}
94+
95+
/*
96+
/**********************************************************************
97+
/* Test methods, JsonProperty.access=READ_ONLY accessor
98+
/**********************************************************************
99+
*/
100+
101+
@Test
102+
public void testSerializeReadOnlyAccessor() throws Exception {
103+
String json = MAPPER.writeValueAsString(new RecordWithReadOnlyAccessor(123, "Bob"));
104+
assertEquals(a2q("{'id':123,'name':'Bob'}"), json);
105+
}
106+
107+
@Test
108+
public void testDeserializeReadOnlyAccessor() throws Exception {
109+
RecordWithReadOnlyAccessor expected = new RecordWithReadOnlyAccessor(123, null);
110+
111+
assertEquals(expected, MAPPER.readValue(a2q("{'id':123}"), RecordWithReadOnlyAccessor.class));
112+
assertEquals(expected, MAPPER.readValue(a2q("{'id':123,'name':null}"), RecordWithReadOnlyAccessor.class));
113+
assertEquals(expected, MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyAccessor.class));
114+
}
115+
116+
/*
117+
/**********************************************************************
118+
/* Test methods, JsonProperty.access=READ_ONLY component, but accessor method was overridden without re-annotating with JsonProperty.access=READ_ONLY
119+
/**********************************************************************
120+
*/
121+
122+
@Test
123+
public void testSerializeReadOnlyComponentOverrideAccessor() throws Exception {
124+
String json = MAPPER.writeValueAsString(new RecordWithReadOnlyComponentOverriddenAccessor(123, "Bob"));
125+
assertEquals(a2q("{'id':123,'name':'Bob'}"), json);
126+
}
127+
128+
@Test
129+
public void testDeserializeReadOnlyComponentOverrideAccessor() throws Exception {
130+
RecordWithReadOnlyComponentOverriddenAccessor expected = new RecordWithReadOnlyComponentOverriddenAccessor(123, null);
131+
132+
assertEquals(expected, MAPPER.readValue(a2q("{'id':123}"), RecordWithReadOnlyComponentOverriddenAccessor.class));
133+
assertEquals(expected, MAPPER.readValue(a2q("{'id':123,'name':null}"), RecordWithReadOnlyComponentOverriddenAccessor.class));
134+
assertEquals(expected, MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyComponentOverriddenAccessor.class));
135+
}
136+
137+
/*
138+
/**********************************************************************
139+
/* Test methods, JsonProperty.access=READ_ONLY parameter of primitive type
140+
/**********************************************************************
141+
*/
142+
143+
@Test
144+
public void testSerializeReadOnlyPrimitiveTypeProperty() throws Exception {
145+
String json = MAPPER.writeValueAsString(new RecordWithReadOnlyPrimitiveType(123, "Bob"));
146+
assertEquals(a2q("{'id':123,'name':'Bob'}"), json);
147+
}
148+
149+
@Test
150+
public void testDeserializeReadOnlyPrimitiveTypeProperty() throws Exception {
151+
RecordWithReadOnlyPrimitiveType value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyPrimitiveType.class);
152+
assertEquals(new RecordWithReadOnlyPrimitiveType(0, "Bob"), value);
153+
}
154+
155+
/*
156+
/**********************************************************************
157+
/* Test methods, JsonProperty.access=READ_ONLY all parameters
158+
/**********************************************************************
159+
*/
160+
161+
@Test
162+
public void testSerializeReadOnlyAllProperties() throws Exception {
163+
String json = MAPPER.writeValueAsString(new RecordWithReadOnlyAll(123, "Bob"));
164+
assertEquals(a2q("{'id':123,'name':'Bob'}"), json);
165+
}
166+
167+
@Test
168+
public void testDeserializeReadOnlyAllProperties() throws Exception {
169+
RecordWithReadOnlyAll value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyAll.class);
170+
assertEquals(new RecordWithReadOnlyAll(0, null), value);
171+
}
172+
173+
@Test
174+
public void testSerializeReadOnlyAllProperties_WithNoArgConstructor() throws Exception {
175+
String json = MAPPER.writeValueAsString(new RecordWithReadOnlyAllAndNoArgConstructor(123, "Bob"));
176+
assertEquals(a2q("{'id':123,'name':'Bob'}"), json);
177+
}
178+
179+
@Test
180+
public void testDeserializeReadOnlyAllProperties_WithNoArgConstructor() throws Exception {
181+
RecordWithReadOnlyAllAndNoArgConstructor value = MAPPER.readValue(a2q("{'id':123,'name':'Bob'}"), RecordWithReadOnlyAllAndNoArgConstructor.class);
182+
assertEquals(new RecordWithReadOnlyAllAndNoArgConstructor(0, null), value);
183+
}
184+
}

0 commit comments

Comments
 (0)