Skip to content

Commit

Permalink
Fix #4777: make 0-args factory method become "default Creator" as in …
Browse files Browse the repository at this point in the history
…2.17 (#4799)
  • Loading branch information
cowtowncoder committed Nov 13, 2024
1 parent 66e3409 commit 8844968
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,10 @@ wrongwrong (@k163377)
serializer lookup was done on the pre-converted value when _delegateSerializer was null
(2.18.1)

Bernd Ahlers (@bernd)
* Reported #4742: Deserialization with Builder, External type id, `@JsonCreator` failing
(2.18.2)

Mike Minicki (@martel)
* Reported #4788: `EnumFeature.WRITE_ENUMS_TO_LOWERCASE` overrides `@JsonProperty` values
(2.18.2)
Expand Down
7 changes: 6 additions & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Project: jackson-databind

#4733: Wrong serialization of Type Ids for certain types of Enum values
(reported by @nlisker)
#4742: Deserialization with Builder, External type id, `@JsonCreator` failing
(reported by Bernd A)
#4777: `StdValueInstantiator.withArgsCreator` is now set for creators with
no arguments
(reported by @wrongwrong)
#4783 Possibly wrong behavior of @JsonMerge
(reported by @nlisker)
(fix by Joo-Hyuk K)
Expand Down Expand Up @@ -70,7 +75,7 @@ Project: jackson-databind
(fix by Joo-Hyuk K)
#4749: Fixed a problem with `StdDelegatingSerializer#serializeWithType` looking up the serializer
with the wrong argument
(fix by wrongwrong)
(fix by @wrongwrong)

2.18.0 (26-Sep-2024)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,16 @@ protected ValueInstantiator _constructDefaultValueInstantiator(DeserializationCo
if (potentialCreators.hasPropertiesBased()) {
PotentialCreator primaryPropsBased = potentialCreators.propertiesBased;

// Start by assigning the primary (and only) properties-based creator
_addSelectedPropertiesBasedCreator(ctxt, beanDesc, creators,
CreatorCandidate.construct(config,
primaryPropsBased.creator(), primaryPropsBased.propertyDefs()));
// 12-Nov-2024, tatu: [databind#4777] We may have collected a 0-args Factory
// method; and if so, may need to "pull it out" as default creator
if (primaryPropsBased.paramCount() == 0) {
creators.setDefaultCreator(primaryPropsBased.creator());
} else {
// Start by assigning the primary (and only) properties-based creator
_addSelectedPropertiesBasedCreator(ctxt, beanDesc, creators,
CreatorCandidate.construct(config,
primaryPropsBased.creator(), primaryPropsBased.propertyDefs()));
}
}

// Continue with explicitly annotated delegating Creators
Expand All @@ -240,9 +246,10 @@ protected ValueInstantiator _constructDefaultValueInstantiator(DeserializationCo
// First things first: the "default constructor" (zero-arg
// constructor; whether implicit or explicit) is NOT included
// in list of constructors, so needs to be handled separately.
AnnotatedConstructor defaultCtor = beanDesc.findDefaultConstructor();
if (defaultCtor != null) {
if (!creators.hasDefaultCreator() || _hasCreatorAnnotation(config, defaultCtor)) {
// However, we may have added one for 0-args Factory method earlier, so:
if (!creators.hasDefaultCreator()) {
AnnotatedConstructor defaultCtor = beanDesc.findDefaultConstructor();
if (defaultCtor != null) {
creators.setDefaultCreator(defaultCtor);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package tools.jackson.databind.tofix;
package tools.jackson.databind.deser.creators;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonCreator;

import tools.jackson.databind.*;
import tools.jackson.databind.deser.*;
import tools.jackson.databind.introspect.AnnotatedMethod;
import tools.jackson.databind.introspect.AnnotatedWithParams;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.module.SimpleModule;
import tools.jackson.databind.testutil.DatabindTestUtil;
import tools.jackson.databind.testutil.failure.JacksonTestFailureExpected;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class JsonCreatorNoArgs4777Test extends DatabindTestUtil
{
static class Foo {
private Foo() { }
static class Foo4777 {
Foo4777() { }

@JsonCreator
static Foo create() {
return new Foo();
static Foo4777 create() {
return new Foo4777();
}
}

static class Instantiators implements ValueInstantiators {
static class Instantiators4777 implements ValueInstantiators {
@Override
public ValueInstantiator modifyValueInstantiator(
DeserializationConfig config,
BeanDescription beanDesc,
ValueInstantiator defaultInstantiator
) {
if (beanDesc.getBeanClass() == Foo.class) {
if (beanDesc.getBeanClass() == Foo4777.class) {
AnnotatedWithParams dc = defaultInstantiator.getDefaultCreator();
if (!(dc instanceof AnnotatedMethod)
|| !dc.getName().equals("create")) {
Expand All @@ -52,18 +52,17 @@ public ValueInstantiator findValueInstantiator(DeserializationConfig config, Bea
// For [databind#4777]
@SuppressWarnings("serial")
@Test
@JacksonTestFailureExpected
public void testCreatorDetection4777() throws Exception {
SimpleModule sm = new SimpleModule() {
@Override
public void setupModule(SetupContext context) {
super.setupModule(context);
context.addValueInstantiators(new Instantiators());
context.addValueInstantiators(new Instantiators4777());
}
};
ObjectMapper mapper = JsonMapper.builder().addModule(sm).build();

Foo result = mapper.readValue("{}", Foo.class);
Foo4777 result = mapper.readValue("{}", Foo4777.class);
assertNotNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

// For some reason, fixed in 2.18.2, but failing for 3.0 -- possibly
// depends on another fix.
// [databind#4742] Deserialization with Builder, External type id,
// @JsonCreator not yet implemented
public class JacksonBuilderCreatorSubtype4742Test
// @JsonCreator failing
public class BuilderCreatorSubtype4742Test
extends DatabindTestUtil
{
public static class Animals {
Expand Down Expand Up @@ -110,7 +112,7 @@ public String toString() {

@JacksonTestFailureExpected
@Test
public void testDeser() throws Exception
public void testDeser4742() throws Exception
{
final Animals animals = MAPPER.readValue(
"{\n" +
Expand All @@ -123,6 +125,5 @@ public void testDeser() throws Exception
assertEquals(2, animals.animals.size());
assertInstanceOf(BirdProperties.class, animals.animals.get(0).properties);
assertInstanceOf(MammalProperties.class, animals.animals.get(1).properties);

}
}

0 comments on commit 8844968

Please sign in to comment.