Skip to content

Commit

Permalink
Merge branch '2.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 14, 2024
2 parents ac233a7 + 780d116 commit 1e57473
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ private static void _introspect(Class<?> currType, Map<String, PropBuilder> prop
_introspect(currType.getSuperclass(), props, features);

final boolean noStatics = JSON.Feature.INCLUDE_STATIC_FIELDS.isDisabled(features);
final boolean isFieldNameGettersEnabled = JSON.Feature.USE_FIELD_MATCHING_GETTERS.isEnabled(features);

// 14-Jun-2024, tatu: Need to enable "matching getters" naming style for Java Records
// too, regardless of `Feature.USE_FIELD_MATCHING_GETTERS`
final boolean isFieldNameGettersEnabled = JSON.Feature.USE_FIELD_MATCHING_GETTERS.isEnabled(features)
|| RecordsHelpers.isRecordType(currType);

final Map<String, Field> fieldNameMap = isFieldNameGettersEnabled ? new HashMap<>() : null;

Expand Down Expand Up @@ -148,14 +152,15 @@ private static void _introspect(Class<?> currType, Map<String, PropBuilder> prop
name = decap(name.substring(2));
_propFrom(props, name).withIsGetter(m);
}
} else if (isFieldNameGettersEnabled) {
} else if (isFieldNameGettersEnabled){
// 10-Mar-2024: [jackson-jr#94]:
// This will allow getters with field name as their getters,
// like the ones generated by Groovy (or JDK 17 for Records).
// If method name matches with field name, & method return
// type matches the field type only then it can be considered a getter.
Field field = fieldNameMap.get(name);
if (field != null && Modifier.isPublic(m.getModifiers()) && m.getReturnType().equals(field.getType())) {
if (field != null && Modifier.isPublic(m.getModifiers())
&& m.getReturnType().equals(field.getType())) {
// NOTE: do NOT decap, field name should be used as-is
_propFrom(props, name).withGetter(m);
}
Expand Down
6 changes: 2 additions & 4 deletions jr-record-test/src/test-jdk17/java/jr/Java17RecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public record Cow(String message, Map<String, String> object) {
// [jackson-jr#94]
public void testJava14RecordSerialization() throws Exception {
// 13-Jun-2024, tatu: why is this explicitly needed?
JSON json = JSON.builder().enable(JSON.Feature.USE_FIELD_MATCHING_GETTERS).build();
//JSON json = JSON.std;
JSON json = JSON.std;
var expectedDoc = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}";
Cow input = new Cow("MOO", Map.of("Foo", "Bar"));

Expand All @@ -28,8 +27,7 @@ public void testJava14RecordSerialization() throws Exception {
// [jackson-jr#148]
public void testJava14RecordDeserialization() throws Exception {
// 13-Jun-2024, tatu: why is this explicitly needed?
JSON json = JSON.builder().enable(JSON.Feature.USE_FIELD_MATCHING_GETTERS).build();
//JSON json = JSON.std;
JSON json = JSON.std;
String inputDoc = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}";

Cow expected = new Cow("MOO", Map.of("Foo", "Bar"));
Expand Down

0 comments on commit 1e57473

Please sign in to comment.