1313import static org .junit .jupiter .api .Assertions .*;
1414
1515// [databind#4938] Allow JsonCreator factory method to return `null`
16+ // [databind#5401] Do not replace null from JsonCreator factory method
1617public class JsonCreatorReturningNull4938Test
1718 extends DatabindTestUtil
1819{
20+ // [databind#4938]
1921 static class Localized3 {
2022 public final String en ;
2123 public final String de ;
@@ -89,12 +91,32 @@ public void addProperty(String key, Object value) {
8991 }
9092 }
9193
94+ // [databind#5401]
95+ static class NonEmpty5401 {
96+ public NonEmptyString5401 nonEmpty ;
97+ }
98+
99+ static class NonEmptyString5401 {
100+ public final String value ;
101+
102+ @ JsonCreator
103+ public static NonEmptyString5401 of (String value ) {
104+ if (value == null || value .isEmpty ()) {
105+ return null ;
106+ }
107+ return new NonEmptyString5401 (value );
108+ }
109+
110+ private NonEmptyString5401 (String value ) {
111+ this .value = value ;
112+ }
113+ }
92114
93115 private final ObjectMapper MAPPER = newJsonMapper ();
94116
117+ // [databind#4938]
95118 @ Test
96119 void testDeserializeToNullWhenAllPropertiesAreNull ()
97- throws Exception
98120 {
99121 Localized3 result = MAPPER .readValue (
100122 "{ \" en\" : null, \" de\" : null, \" fr\" : null }" ,
@@ -105,7 +127,6 @@ void testDeserializeToNullWhenAllPropertiesAreNull()
105127
106128 @ Test
107129 void testDeserializeToNonNullWhenAnyPropertyIsNonNull ()
108- throws Exception
109130 {
110131 Localized3 result = MAPPER .readValue (
111132 "{ \" en\" : \" Hello\" , \" de\" : null, \" fr\" : null }" ,
@@ -117,7 +138,6 @@ void testDeserializeToNonNullWhenAnyPropertyIsNonNull()
117138
118139 @ Test
119140 void testDeserializeReadingAfterCreatorProps ()
120- throws Exception
121141 {
122142 // Should all fail...
123143 ObjectReader enabled = MAPPER .readerFor (Localized4 .class )
@@ -135,7 +155,6 @@ void testDeserializeReadingAfterCreatorProps()
135155 // Test to verify we are reading till the end of the OBJECT
136156 @ Test
137157 void testDeserializeReadingUntilEndObject ()
138- throws Exception
139158 {
140159 // Should all fail...
141160 ObjectReader enabled = MAPPER .readerFor (Localized4 .class )
@@ -156,7 +175,6 @@ void testDeserializeReadingUntilEndObject()
156175
157176 @ Test
158177 void testJsonCreatorNullWithAnySetter ()
159- throws Exception
160178 {
161179 String JSON = "{ \" en\" : null, \" de\" : null, \" fr\" : null, " +
162180 // These two properties are unknown
@@ -166,4 +184,16 @@ void testJsonCreatorNullWithAnySetter()
166184 .without (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES )
167185 .readValue (JSON );
168186 }
187+
188+ // [databind#5401]
189+ @ Test
190+ void deserializeFieldToNullIfDelegatingCreatorReturnsNull ()
191+ {
192+ NonEmpty5401 result = MAPPER .readValue (
193+ "{ \" nonEmpty\" : \" \" }" ,
194+ NonEmpty5401 .class );
195+
196+ assertNotNull (result );
197+ assertNull (result .nonEmpty );
198+ }
169199}
0 commit comments