Skip to content

Commit

Permalink
Ignore ConstructorDetector.SingleArgConstructor.PROPERTIES preference…
Browse files Browse the repository at this point in the history
… for Records.
  • Loading branch information
yihtserns committed Jun 7, 2023
1 parent a1e6208 commit bc51a21
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,9 @@ protected void _addImplicitConstructorCreators(DeserializationContext ctxt,
final AnnotationIntrospector intr = ccState.annotationIntrospector();
final VisibilityChecker<?> vchecker = ccState.vchecker;
List<AnnotatedWithParams> implicitCtors = null;
final boolean preferPropsBased = config.getConstructorDetector().singleArgCreatorDefaultsToProperties();
final boolean preferPropsBased = config.getConstructorDetector().singleArgCreatorDefaultsToProperties()
// [databind#3968]: Only Record's canonical constructor is allowed to be considered for properties-based creator
&& !beanDesc.isRecordType();

for (CreatorCandidate candidate : ctorCandidates) {
final int argCount = candidate.paramCount();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.fasterxml.jackson.databind.records;

import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

public class RecordImplicitSingleValueUsePropertiesBasedCreatorsTest extends BaseMapTest
{

private final ObjectMapper MAPPER = jsonMapperBuilder()
.annotationIntrospector(new Jdk8ConstructorParameterNameAnnotationIntrospector())
.constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED)
.build();

record RecordWithMultiValueCanonAndSingleValueAltConstructor(int id, String name) {

public RecordWithMultiValueCanonAndSingleValueAltConstructor(int id) {
this(id, "AltConstructor");
}
}

record RecordWithSingleValueCanonAndMultiValueAltConstructor(String name) {

public RecordWithSingleValueCanonAndMultiValueAltConstructor(String name, String email) {
this("AltConstructor");
}
}

/*
/**********************************************************************
/* Test methods, multi-value canonical constructor + single-value alt constructor
/**********************************************************************
*/

public void testDeserializeMultipleConstructors_UsingMultiValueCanonicalConstructor() throws Exception {
RecordWithMultiValueCanonAndSingleValueAltConstructor value = MAPPER.readValue(
a2q("{'id':123,'name':'Bob'}"),
RecordWithMultiValueCanonAndSingleValueAltConstructor.class);

assertEquals(new RecordWithMultiValueCanonAndSingleValueAltConstructor(123, "Bob"), value);
}

public void testDeserializeMultipleConstructors_WillIgnoreSingleValueAltConstructor() throws Exception {
RecordWithMultiValueCanonAndSingleValueAltConstructor value = MAPPER.readValue(
a2q("{'id':123}"),
RecordWithMultiValueCanonAndSingleValueAltConstructor.class);

assertEquals(new RecordWithMultiValueCanonAndSingleValueAltConstructor(123, null), value);
}

/*
/**********************************************************************
/* Test methods, single-value canonical constructor + multi-value alt constructor
/**********************************************************************
*/

public void testDeserializeMultipleConstructors_UsingSingleValueCanonicalConstructor() throws Exception {
RecordWithSingleValueCanonAndMultiValueAltConstructor value = MAPPER.readValue(
a2q("{'name':'Bob'}"),
RecordWithSingleValueCanonAndMultiValueAltConstructor.class);

assertEquals(new RecordWithSingleValueCanonAndMultiValueAltConstructor("Bob"), value);
}

public void testDeserializeMultipleConstructors_WillIgnoreMultiValueAltConstructor() throws Exception {
try {
MAPPER.readValue(a2q("{'name':'Bob','email':'[email protected]'}"), RecordWithSingleValueCanonAndMultiValueAltConstructor.class);
} catch (UnrecognizedPropertyException e) {
verifyException(e, "Unrecognized");
verifyException(e, "\"email\"");
verifyException(e, "RecordWithSingleValueCanonAndMultiValueAltConstructor");
}
}
}

0 comments on commit bc51a21

Please sign in to comment.