Skip to content

Commit

Permalink
Avoid mutator-inference for Records (to avoid pulling in Fields as mu…
Browse files Browse the repository at this point in the history
…tators) (#3737)
  • Loading branch information
cowtowncoder authored Jan 16, 2023
1 parent 30e7bec commit 0a4cfc4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public class POJOPropertiesCollector
*/
protected final boolean _useAnnotations;

/**
* @since 2.15
*/
protected final boolean _isRecordType;

/*
/**********************************************************
/* Collected property information
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -225,7 +231,7 @@ public JavaType getType() {
* @since 2.15
*/
public boolean isRecordType() {
return _type.isRecordType();
return _isRecordType;
}

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

// 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]
Expand Down Expand Up @@ -994,7 +1005,10 @@ protected void _removeUnwantedProperties(Map<String, POJOPropertyBuilder> props)
*/
protected void _removeUnwantedAccessor(Map<String, POJOPropertyBuilder> 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<POJOPropertyBuilder> it = props.values().iterator();

while (it.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit 0a4cfc4

Please sign in to comment.