Skip to content

Commit 15d0309

Browse files
authored
Fixing records deserialization issue when JsonIgnore is used with same json property name (#5089)
1 parent 31bfb13 commit 15d0309

File tree

7 files changed

+38
-45
lines changed

7 files changed

+38
-45
lines changed

release-notes/CREDITS-2.x

+5-2
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,10 @@ Zhen Lin Low (@zhenlin-pay2)
18861886
when collecting bean properties, breaking AsExternalTypeDeserializer
18871887
(2.18.3)
18881888

1889-
Fawzi Essam (@iifawz)
1889+
Fawzi Essam (@iifawzi)
1890+
* Contributed fix or #4628: `@JsonIgnore` and `@JsonProperty.access=READ_ONLY`
1891+
on Record property
1892+
(2.18.4)
18901893
* Contributed fix for #5049: Duplicate creator property "b" (index 0 vs 1)
18911894
on simple java record
1892-
(2.18.3)
1895+
(2.18.4)

release-notes/VERSION-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Project: jackson-databind
66

77
2.18.4 (not yet released)
88

9+
#4628: `@JsonIgnore` and `@JsonProperty.access=READ_ONLY` on Record property
10+
ignored for deserialization
11+
(reported by Sim Y-T)
12+
(fix contributed by Fawzi E)
913
#5049: Duplicate creator property "b" (index 0 vs 1) on simple java record
1014
(reported by @richard-melvin)
1115
(fix contributed by Fawzi E)

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java

+6
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,12 @@ protected void _renameProperties(Map<String, POJOPropertyBuilder> props)
13801380
Map.Entry<String, POJOPropertyBuilder> entry = it.next();
13811381
POJOPropertyBuilder prop = entry.getValue();
13821382

1383+
// 10-Apr-2025: [databind#4628] skip properties that are marked to be ignored
1384+
// TODO: we are using implicit name, is that ok?
1385+
if (_ignoredPropertyNames != null && _ignoredPropertyNames.contains(prop.getName())) {
1386+
continue;
1387+
}
1388+
13831389
Collection<PropertyName> l = prop.findExplicitNames();
13841390

13851391
// no explicit names? Implicit one is fine as is

src/test-jdk17/java/com/fasterxml/jackson/databind/records/RecordJsonSerDeser188Test.java

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public void serialize(String value, JsonGenerator jgen, SerializerProvider provi
5050

5151
static class PrefixStringDeserializer extends StdScalarDeserializer<String>
5252
{
53+
private static final long serialVersionUID = 1L;
54+
5355
protected PrefixStringDeserializer() {
5456
super(String.class);
5557
}

src/test-jdk17/java/com/fasterxml/jackson/databind/records/RecordWithJsonIgnoreTest.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ record RecordWithIgnore(int id, @JsonIgnore String name) {
1616
record RecordWithIgnoreJsonProperty(int id, @JsonIgnore @JsonProperty("name") String name) {
1717
}
1818

19+
record RecordWithIgnoreJsonPropertyDifferentName(int id, @JsonIgnore @JsonProperty("name2") String name) {
20+
}
21+
1922
record RecordWithIgnoreAccessor(int id, String name) {
20-
2123
@JsonIgnore
2224
@Override
2325
public String name() {
@@ -61,6 +63,20 @@ public void testSerializeJsonIgnoreAndJsonPropertyRecord() throws Exception {
6163
assertEquals("{\"id\":123}", json);
6264
}
6365

66+
@Test
67+
public void testDeserializeJsonIgnoreAndJsonPropertyRecord() throws Exception {
68+
RecordWithIgnoreJsonProperty value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}",
69+
RecordWithIgnoreJsonProperty.class);
70+
assertEquals(new RecordWithIgnoreJsonProperty(123, null), value);
71+
}
72+
73+
@Test
74+
public void testDeserializeJsonIgnoreRecordWithDifferentName() throws Exception {
75+
RecordWithIgnoreJsonPropertyDifferentName value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}",
76+
RecordWithIgnoreJsonPropertyDifferentName.class);
77+
assertEquals(new RecordWithIgnoreJsonPropertyDifferentName(123, null), value);
78+
}
79+
6480
/*
6581
/**********************************************************************
6682
/* Test methods, JsonIgnore accessor
@@ -69,10 +85,11 @@ public void testSerializeJsonIgnoreAndJsonPropertyRecord() throws Exception {
6985

7086
@Test
7187
public void testSerializeJsonIgnoreAccessorRecord() throws Exception {
72-
String json = MAPPER.writeValueAsString(new RecordWithIgnoreAccessor(123, "Bob"));
73-
assertEquals("{\"id\":123}", json);
88+
assertEquals("{\"id\":123}",
89+
MAPPER.writeValueAsString(new RecordWithIgnoreAccessor(123, "Bob")));
7490
}
7591

92+
// [databind#4628]
7693
@Test
7794
public void testDeserializeJsonIgnoreAccessorRecord() throws Exception {
7895
RecordWithIgnoreAccessor expected = new RecordWithIgnoreAccessor(123, null);

src/test-jdk17/java/com/fasterxml/jackson/databind/tofix/RecordWIthJsonIgnoreAndValue4628Test.java

-39
This file was deleted.

src/test/java/com/fasterxml/jackson/databind/exc/SubclassedThrowableDeserialization4827Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
public class SubclassedThrowableDeserialization4827Test
1616
extends DatabindTestUtil
1717
{
18-
18+
@SuppressWarnings("serial")
1919
public static class SubclassedExceptionJava extends Exception {
2020
@JsonCreator
2121
public SubclassedExceptionJava(

0 commit comments

Comments
 (0)