diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 158b531eda..211f1aa1ec 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -26,6 +26,7 @@ Project: jackson-databind (reported by João G) #3708: Seems like `java.nio.file.Path` is safe for Android API level 26 (contributed by @pjfanning) +#3736: Try to avoid auto-detecting Fields for Record types 2.14.2 (not yet released) diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java index 3ccc87ae40..653162054e 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java @@ -61,6 +61,11 @@ public class POJOPropertiesCollector */ protected final boolean _useAnnotations; + /** + * @since 2.15 + */ + protected final boolean _isRecordType; + /* /********************************************************** /* Collected property information @@ -170,6 +175,7 @@ protected POJOPropertiesCollector(MapperConfig config, boolean forSerializati _forSerialization = forSerialization; _type = type; _classDef = classDef; + _isRecordType = _type.isRecordType(); if (config.isAnnotationProcessingEnabled()) { _useAnnotations = true; _annotationIntrospector = _config.getAnnotationIntrospector(); @@ -225,7 +231,7 @@ public JavaType getType() { * @since 2.15 */ public boolean isRecordType() { - return _type.isRecordType(); + return _isRecordType; } public AnnotatedClass getClassDef() { @@ -431,7 +437,12 @@ protected void collectAll() LinkedHashMap props = new LinkedHashMap(); // First: gather basic data - _addFields(props); // note: populates _fieldRenameMappings + + // 15-Jan-2023, tatu: [databind#3736] Let's avoid detecting fields of Records + // altogether (unless we find a good reason to detect them) + if (!isRecordType()) { + _addFields(props); // note: populates _fieldRenameMappings + } _addMethods(props); // 25-Jan-2016, tatu: Avoid introspecting (constructor-)creators for non-static // inner classes, see [databind#1502] @@ -994,7 +1005,10 @@ protected void _removeUnwantedProperties(Map props) */ protected void _removeUnwantedAccessor(Map props) { - final boolean inferMutators = _config.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS); + // 15-Jan-2023, tatu: Avoid pulling in mutators for Records; Fields mostly + // since there should not be setters. + final boolean inferMutators = !isRecordType() + && _config.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS); Iterator it = props.values().iterator(); while (it.hasNext()) { diff --git a/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorsTest.java b/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorsTest.java index 225db2274e..558223ec9f 100644 --- a/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorsTest.java +++ b/src/test-jdk14/java/com/fasterxml/jackson/databind/records/RecordCreatorsTest.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; public class RecordCreatorsTest extends BaseMapTest { @@ -62,11 +63,11 @@ public void testDeserializeWithAltCtor() throws Exception { try { MAPPER.readValue("{\"id\":2812,\"name\":\"Bob\"}", RecordWithAltCtor.class); - fail("should not pass"); - } catch (JsonMappingException e) { - verifyException(e, "Can not set final java.lang.String field"); - verifyException(e, "RecordWithAltCtor.name"); + } catch (UnrecognizedPropertyException e) { + verifyException(e, "Unrecognized"); + verifyException(e, "\"name\""); + verifyException(e, "RecordWithAltCtor"); } }