Skip to content

Commit

Permalink
Fix #172: ignore non-Record-field properties for deser introspection (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Nov 5, 2024
1 parent 93a30dc commit 44c5656
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.fasterxml.jackson.jr.ob.impl;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

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

static List<String> recordPropertyNames(Class<?> cls) {
// Let's base this on public fields
return Arrays.asList(cls.getDeclaredFields()).stream().map(Field::getName)
.collect(Collectors.toList());
}

private static Class<?>[] componentTypes(Class<?> recordType)
throws ReflectiveOperationException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
if (len == 0) {
propMap = Collections.emptyMap();
} else {
Set<String> recordProps = isRecord
? new HashSet<>(RecordsHelpers.recordPropertyNames(raw)) : null;
propMap = caseInsensitive
? new TreeMap<>(String.CASE_INSENSITIVE_ORDER)
// 13-May-2021, tatu: Let's retain ordering here:
Expand All @@ -475,6 +477,11 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
}
}
if (isRecord) {
// Records can only deserialize propreties that are declared in the record;
// other virtual properties (getter methods) need to be ignored
if (!recordProps.contains(rawProp.name)) {
continue;
}
try {
field = raw.getDeclaredField(rawProp.name);
} catch (NoSuchFieldException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jr.failing;
package jr;

import java.time.Instant;
import java.time.ZoneId;
Expand Down
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Modules:
=== Releases ===
------------------------------------------------------------------------

2.18.2 (not yet released)

#172: Record deserialisation with instance methods fails
(reported by Giovanni V-d-S)

2.18.1 (28-Oct-2024)

#167: Deserialization of record fails on constructor parameter ordering
Expand Down

0 comments on commit 44c5656

Please sign in to comment.