Skip to content

Commit

Permalink
Fix #4724 (delegating creator, Records)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 9, 2024
1 parent cbf71a3 commit 6cd3ce8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
3 changes: 3 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,9 @@ Antti Lampinen (arlampin@github)
* Reported #3897: 2.15.0 breaks deserialization when POJO/Record only has a single field
and is marked `Access.WRITE_ONLY`
(2.15.1)
* Reported #4724: Deserialization behavior change with Records, `@JsonCreator` and
`@JsonValue` between 2.17 and 2.18
(2.18.1)
Dmitry Bolotin (dbolotin@github)
* Reported #1172: `@JsonView` doesn't work with `@JsonCreator`
Expand Down
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Project: jackson-databind
(reported by @MaximValeev)
(fix by Joo-Hyuk K)
#4718: Should not fail on trying to serialize `java.time.DateTimeException`
#4724: Deserialization behavior change with Records, `@JsonCreator` and
`@JsonValue` between 2.17 and 2.18
(reported by Antti L)

2.18.0 (26-Sep-2024)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,12 @@ protected void _addCreators(Map<String, POJOPropertyBuilder> props)
|| factories.remove(defaultCreator)) {
// But wait! Could be delegating
if (_isDelegatingConstructor(defaultCreator)) {
creators.addExplicitDelegating(defaultCreator);
// 08-Oct-2024, tatu: [databind#4724] Only add if no explicit
// candidates added
if (!creators.hasDelegating()) {
// ... not technically explicit but simpler this way
creators.addExplicitDelegating(defaultCreator);
}
} else {
creators.setPropertiesBased(_config, defaultCreator, "Primary");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public void setImplicitDelegating(List<PotentialCreator> implicitConstructors,
/**********************************************************************
*/

// @since 2.18.1
public boolean hasDelegating() {
return (explicitDelegating != null) && !explicitDelegating.isEmpty();
}

public boolean hasPropertiesBased() {
return (propertiesBased != null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package com.fasterxml.jackson.databind.tofix;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected;

import org.junit.jupiter.api.Test;

// [databind#4724] Deserialization behavior change with Java Records, JsonCreator and JsonValue between 2.17.2 => 2.18.0
public class RecordJsonCreatorAndJsonValue4724Test
extends DatabindTestUtil
{

public record Something(String value) {
public Something {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException("Value cannot be null or empty");
}
}

// should be considered Delegating due to @JsonValue later on
@JsonCreator
public static Something of(String value) {
if (value.isEmpty()) {
Expand All @@ -34,10 +33,8 @@ public String toString() {
}
}

@JacksonTestFailureExpected
@Test
void deserialization() throws Exception {
newJsonMapper().readValue("\"\"", Something.class);
}

}

0 comments on commit 6cd3ce8

Please sign in to comment.