Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid mutator-inference for Records (to avoid pulling in Fields as mutators) #3737

Merged
merged 2 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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