Skip to content

Commit 0a4cfc4

Browse files
authored
Avoid mutator-inference for Records (to avoid pulling in Fields as mutators) (#3737)
1 parent 30e7bec commit 0a4cfc4

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Project: jackson-databind
2626
(reported by João G)
2727
#3708: Seems like `java.nio.file.Path` is safe for Android API level 26
2828
(contributed by @pjfanning)
29+
#3736: Try to avoid auto-detecting Fields for Record types
2930

3031
2.14.2 (not yet released)
3132

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public class POJOPropertiesCollector
6161
*/
6262
protected final boolean _useAnnotations;
6363

64+
/**
65+
* @since 2.15
66+
*/
67+
protected final boolean _isRecordType;
68+
6469
/*
6570
/**********************************************************
6671
/* Collected property information
@@ -170,6 +175,7 @@ protected POJOPropertiesCollector(MapperConfig<?> config, boolean forSerializati
170175
_forSerialization = forSerialization;
171176
_type = type;
172177
_classDef = classDef;
178+
_isRecordType = _type.isRecordType();
173179
if (config.isAnnotationProcessingEnabled()) {
174180
_useAnnotations = true;
175181
_annotationIntrospector = _config.getAnnotationIntrospector();
@@ -225,7 +231,7 @@ public JavaType getType() {
225231
* @since 2.15
226232
*/
227233
public boolean isRecordType() {
228-
return _type.isRecordType();
234+
return _isRecordType;
229235
}
230236

231237
public AnnotatedClass getClassDef() {
@@ -431,7 +437,12 @@ protected void collectAll()
431437
LinkedHashMap<String, POJOPropertyBuilder> props = new LinkedHashMap<String, POJOPropertyBuilder>();
432438

433439
// First: gather basic data
434-
_addFields(props); // note: populates _fieldRenameMappings
440+
441+
// 15-Jan-2023, tatu: [databind#3736] Let's avoid detecting fields of Records
442+
// altogether (unless we find a good reason to detect them)
443+
if (!isRecordType()) {
444+
_addFields(props); // note: populates _fieldRenameMappings
445+
}
435446
_addMethods(props);
436447
// 25-Jan-2016, tatu: Avoid introspecting (constructor-)creators for non-static
437448
// inner classes, see [databind#1502]
@@ -994,7 +1005,10 @@ protected void _removeUnwantedProperties(Map<String, POJOPropertyBuilder> props)
9941005
*/
9951006
protected void _removeUnwantedAccessor(Map<String, POJOPropertyBuilder> props)
9961007
{
997-
final boolean inferMutators = _config.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS);
1008+
// 15-Jan-2023, tatu: Avoid pulling in mutators for Records; Fields mostly
1009+
// since there should not be setters.
1010+
final boolean inferMutators = !isRecordType()
1011+
&& _config.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS);
9981012
Iterator<POJOPropertyBuilder> it = props.values().iterator();
9991013

10001014
while (it.hasNext()) {

src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorsTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonValue;
66

77
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
89

910
public class RecordCreatorsTest extends BaseMapTest
1011
{
@@ -62,11 +63,11 @@ public void testDeserializeWithAltCtor() throws Exception {
6263
try {
6364
MAPPER.readValue("{\"id\":2812,\"name\":\"Bob\"}",
6465
RecordWithAltCtor.class);
65-
6666
fail("should not pass");
67-
} catch (JsonMappingException e) {
68-
verifyException(e, "Can not set final java.lang.String field");
69-
verifyException(e, "RecordWithAltCtor.name");
67+
} catch (UnrecognizedPropertyException e) {
68+
verifyException(e, "Unrecognized");
69+
verifyException(e, "\"name\"");
70+
verifyException(e, "RecordWithAltCtor");
7071
}
7172
}
7273

0 commit comments

Comments
 (0)