Skip to content

Commit 44c5656

Browse files
authored
Fix #172: ignore non-Record-field properties for deser introspection (#175)
1 parent 93a30dc commit 44c5656

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

Diff for: jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/RecordsHelpers.java

+9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.fasterxml.jackson.jr.ob.impl;
22

33
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.Field;
45
import java.lang.reflect.Method;
56
import java.util.Arrays;
7+
import java.util.List;
68
import java.util.Map;
9+
import java.util.stream.Collectors;
710

811
import com.fasterxml.jackson.jr.ob.impl.POJODefinition.PropBuilder;
912

@@ -80,6 +83,12 @@ static boolean isRecordType(Class<?> cls) {
8083
return (parent != null) && "java.lang.Record".equals(parent.getName());
8184
}
8285

86+
static List<String> recordPropertyNames(Class<?> cls) {
87+
// Let's base this on public fields
88+
return Arrays.asList(cls.getDeclaredFields()).stream().map(Field::getName)
89+
.collect(Collectors.toList());
90+
}
91+
8392
private static Class<?>[] componentTypes(Class<?> recordType)
8493
throws ReflectiveOperationException
8594
{

Diff for: jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueReaderLocator.java

+7
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
456456
if (len == 0) {
457457
propMap = Collections.emptyMap();
458458
} else {
459+
Set<String> recordProps = isRecord
460+
? new HashSet<>(RecordsHelpers.recordPropertyNames(raw)) : null;
459461
propMap = caseInsensitive
460462
? new TreeMap<>(String.CASE_INSENSITIVE_ORDER)
461463
// 13-May-2021, tatu: Let's retain ordering here:
@@ -475,6 +477,11 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
475477
}
476478
}
477479
if (isRecord) {
480+
// Records can only deserialize propreties that are declared in the record;
481+
// other virtual properties (getter methods) need to be ignored
482+
if (!recordProps.contains(rawProp.name)) {
483+
continue;
484+
}
478485
try {
479486
field = raw.getDeclaredField(rawProp.name);
480487
} catch (NoSuchFieldException e) {

Diff for: jr-record-test/src/test-jdk17/java/jr/failing/RecordDeser172Test.java renamed to jr-record-test/src/test-jdk17/java/jr/RecordDeser172Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jr.failing;
1+
package jr;
22

33
import java.time.Instant;
44
import java.time.ZoneId;

Diff for: release-notes/VERSION-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Modules:
1111
=== Releases ===
1212
------------------------------------------------------------------------
1313

14+
2.18.2 (not yet released)
15+
16+
#172: Record deserialisation with instance methods fails
17+
(reported by Giovanni V-d-S)
18+
1419
2.18.1 (28-Oct-2024)
1520

1621
#167: Deserialization of record fails on constructor parameter ordering

0 commit comments

Comments
 (0)