Skip to content

Commit 9203dea

Browse files
authored
Revert "Ignore Records' immutable fields in BeanDeserializerFactory instead o…" (#4631)
This reverts commit f26f9e3.
1 parent f26f9e3 commit 9203dea

File tree

7 files changed

+18
-79
lines changed

7 files changed

+18
-79
lines changed

release-notes/CREDITS-2.x

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Co-Authors (with only partial listings below):
1010

1111
* Joo Hyuk Kim (JooHyukKim@github)
1212
* PJ Fanning (pjfanning@github)
13-
* Sim Yih Tsern (yihtsern@github)
1413

1514
----------------------------------------------------------------------------
1615

@@ -1625,9 +1624,9 @@ Sim Yih Tsern (yihtsern@github)
16251624
* Contributed fix for #3897: 2.15.0 breaks deserialization when POJO/Record only has a
16261625
single field and is marked `Access.WRITE_ONLY`
16271626
(2.15.1)
1628-
* Contributed fix fix #3968: Records with additional constructors failed to deserialize
1627+
* Contributed fux fix #3968: Records with additional constructors failed to deserialize
16291628
(2.15.3)
1630-
... and many more
1629+
16311630
16321631
Ajay Siwach (Siwach16@github)
16331632
* Contributed #3637: Add enum features into `@JsonFormat.Feature`

release-notes/VERSION-2.x

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ Project: jackson-databind
5757
(reported by Eduard G)
5858
#4617: Record property serialization order not preserved
5959
(reported by @GeorgiPetkov)
60-
#4626: `@JsonIgnore` on Record property ignored for deserialization, if there
61-
is getter override
62-
(contributed by @yihtserns)
63-
#4630: `@JsonIncludeProperties`, `@JsonIgnoreProperties` ignored when serializing Records,
64-
if there is getter override
65-
(contributed by @yihtserns)
6660

6761
2.17.2 (05-Jul-2024)
6862

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerFactory.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -977,14 +977,8 @@ protected SettableBeanProperty constructSettableProperty(DeserializationContext
977977
beanDesc.getClassAnnotations(), (AnnotatedMethod) mutator);
978978
} else {
979979
// 08-Sep-2016, tatu: wonder if we should verify it is `AnnotatedField` to be safe?
980-
AnnotatedField field = (AnnotatedField) mutator;
981-
// [databind#3736] Pointless to create a SettableBeanProperty for an immutable field
982-
// Records' fields can't mutated via reflection (JDK-8247517)
983-
// (also see [databind#4626]
984-
if (beanDesc.isRecordType()) {
985-
return null;
986-
}
987-
prop = new FieldProperty(propDef, type, typeDeser, beanDesc.getClassAnnotations(), field);
980+
prop = new FieldProperty(propDef, type, typeDeser,
981+
beanDesc.getClassAnnotations(), (AnnotatedField) mutator);
988982
}
989983
JsonDeserializer<?> deser = findDeserializerFromAnnotation(ctxt, mutator);
990984
if (deser == null) {

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,13 @@ protected void collectAll()
434434
// First: gather basic accessors
435435
LinkedHashMap<String, POJOPropertyBuilder> props = new LinkedHashMap<String, POJOPropertyBuilder>();
436436

437-
_addFields(props); // note: populates _fieldRenameMappings
437+
// 15-Jan-2023, tatu: [databind#3736] Let's avoid detecting fields of Records
438+
// altogether (unless we find a good reason to detect them)
439+
// 17-Apr-2023: Need Records' fields for serialization for cases
440+
// like [databind#3628], [databind#3895] and [databind#3992]
441+
if (!isRecordType() || _forSerialization) {
442+
_addFields(props); // note: populates _fieldRenameMappings
443+
}
438444
_addMethods(props);
439445
// 25-Jan-2016, tatu: Avoid introspecting (constructor-)creators for non-static
440446
// inner classes, see [databind#1502]
@@ -1295,7 +1301,10 @@ protected void _removeUnwantedProperties(Map<String, POJOPropertyBuilder> props)
12951301
*/
12961302
protected void _removeUnwantedAccessor(Map<String, POJOPropertyBuilder> props)
12971303
{
1298-
final boolean inferMutators = _config.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS);
1304+
// 15-Jan-2023, tatu: Avoid pulling in mutators for Records; Fields mostly
1305+
// since there should not be setters.
1306+
final boolean inferMutators = !isRecordType()
1307+
&& _config.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS);
12991308
Iterator<POJOPropertyBuilder> it = props.values().iterator();
13001309

13011310
while (it.hasNext()) {

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,4 @@ public void testHelloRecord() throws Exception {
5050
HelloRecord result = MAPPER.readValue(json, HelloRecord.class);
5151
assertNotNull(result);
5252
}
53-
54-
// [databind#4626]
55-
@Test
56-
public void testDeserialize() throws Exception {
57-
HelloRecord expected = new HelloRecord("hello", null);
58-
59-
assertEquals(expected, MAPPER.readValue(a2q("{'text':'hello'}"), HelloRecord.class));
60-
assertEquals(expected, MAPPER.readValue(a2q("{'text':'hello','hidden':null}"), HelloRecord.class));
61-
assertEquals(expected, MAPPER.readValue(a2q("{'text':'hello','hidden':{'all': []}}"), HelloRecord.class));
62-
}
6353
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ public void testSerializeJsonIgnoreAccessorRecord() throws Exception {
8181

8282
@Test
8383
public void testDeserializeJsonIgnoreAccessorRecord() throws Exception {
84-
RecordWithIgnoreAccessor expected = new RecordWithIgnoreAccessor(123, null);
85-
86-
assertEquals(expected, MAPPER.readValue("{\"id\":123}", RecordWithIgnoreAccessor.class));
87-
assertEquals(expected, MAPPER.readValue("{\"id\":123,\"name\":null}", RecordWithIgnoreAccessor.class));
88-
assertEquals(expected, MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithIgnoreAccessor.class));
84+
RecordWithIgnoreAccessor value = MAPPER.readValue("{\"id\":123,\"name\":\"Bob\"}",
85+
RecordWithIgnoreAccessor.class);
86+
assertEquals(new RecordWithIgnoreAccessor(123, null), value);
8987
}
9088

9189
/*

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

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)