Skip to content

Commit d85e4b1

Browse files
committed
Fix #5401 Single-arg method invocations should not fall back to input argument for null return values
Fixes #5401
1 parent 278037a commit d85e4b1

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/main/java/tools/jackson/databind/introspect/AnnotatedMethod.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ public final Object call(Object[] args) throws Exception {
107107
@Override
108108
public final Object call1(Object arg) throws Exception {
109109
try {
110-
Object ret = _invokerUnary.get().invokeExact(arg);
111-
return ret == null ? arg : ret;
110+
return _invokerUnary.get().invokeExact(arg);
112111
} catch (final Throwable e) {
113112
throw sneakyThrow(e);
114113
}

src/test/java/tools/jackson/databind/deser/creators/JsonCreatorReturningNull4938Test.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@
1616
public class JsonCreatorReturningNull4938Test
1717
extends DatabindTestUtil
1818
{
19+
static class NonEmpty {
20+
public NonEmptyString nonEmpty;
21+
}
22+
23+
static class NonEmptyString {
24+
public final String value;
25+
26+
@JsonCreator
27+
public static NonEmptyString of(String value) {
28+
if (value == null || value.isEmpty()) {
29+
return null;
30+
}
31+
return new NonEmptyString(value);
32+
}
33+
34+
private NonEmptyString(String value) {
35+
this.value = value;
36+
}
37+
}
38+
1939
static class Localized3 {
2040
public final String en;
2141
public final String de;
@@ -92,6 +112,18 @@ public void addProperty(String key, Object value) {
92112

93113
private final ObjectMapper MAPPER = newJsonMapper();
94114

115+
@Test
116+
void testDeserializeFieldToNullIfCreatorReturnsNull()
117+
throws Exception
118+
{
119+
NonEmpty result = MAPPER.readValue(
120+
"{ \"nonEmpty\": \"\" }",
121+
NonEmpty.class);
122+
123+
assertNotNull(result);
124+
assertNull(result.nonEmpty);
125+
}
126+
95127
@Test
96128
void testDeserializeToNullWhenAllPropertiesAreNull()
97129
throws Exception

0 commit comments

Comments
 (0)