Skip to content

Commit

Permalink
Merge branch '2.19'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 5, 2024
2 parents b6fe9c8 + d77c6c6 commit 61e266c
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 tools.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 tools.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 @@ -470,6 +470,8 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
propMap = Collections.emptyMap();
} else {
propMap = new LinkedHashMap<>();
Set<String> recordProps = isRecord
? new HashSet<>(RecordsHelpers.recordPropertyNames(raw)) : null;
final boolean useFields = JSON.Feature.USE_FIELDS.isEnabled(_features);
for (int i = 0; i < len; ++i) {
POJODefinition.Prop rawProp = rawProps.get(i);
Expand All @@ -485,6 +487,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 @@ -16,6 +16,11 @@ Modules:
#171: Add a `JSON.Feature.WRITE_RECORD_FIELDS_IN_DECLARATION_ORDER` for
retaining Serialization order of Java Records (instead of alphabetic)

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 61e266c

Please sign in to comment.