Skip to content

Commit 10d5ab3

Browse files
authored
Merge pull request #1534 from Tillerino/2.8
Fix #1533: Respect ACCEPT_EMPTY_STRING_AS_NULL_OBJECT for polymorphism.
2 parents ce37efc + d55d422 commit 10d5ab3

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsPropertyTypeDeserializer.java

+7
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ protected Object _deserializeTypedUsingDefaultImpl(JsonParser p, Deserialization
153153
// or, something for which "as-property" won't work, changed into "wrapper-array" type:
154154
if (p.getCurrentToken() == JsonToken.START_ARRAY) {
155155
return super.deserializeTypedFromAny(p, ctxt);
156+
} else if (p.getCurrentToken() == JsonToken.VALUE_STRING) {
157+
if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
158+
String str = p.getText().trim();
159+
if (str.isEmpty()) {
160+
return null;
161+
}
162+
}
156163
}
157164
ctxt.reportWrongTokenException(p, JsonToken.FIELD_NAME,
158165
"missing property '"+_typePropertyName+"' that is to contain type id (for class "+baseTypeName()+")");

src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicWithDefaultImpl.java

+28
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ static class BaseWrapper {
129129
public BaseClass value;
130130
}
131131

132+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY,
133+
property = "type")
134+
static class AsProperty {
135+
136+
}
137+
138+
static class AsPropertyWrapper {
139+
public AsProperty value;
140+
}
141+
132142
/*
133143
/**********************************************************
134144
/* Unit tests, deserialization
@@ -247,6 +257,24 @@ public void testUnknownClassAsSubtype() throws Exception
247257
assertNull(w.value);
248258
}
249259

260+
public void testWithoutEmptyStringAsNullObject1533() throws Exception
261+
{
262+
ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
263+
try {
264+
mapper.readValue("{ \"value\": \"\" }", AsPropertyWrapper.class);
265+
fail("Expected " + JsonMappingException.class);
266+
} catch (JsonMappingException e) {
267+
// expected
268+
}
269+
}
270+
271+
public void testWithEmptyStringAsNullObject1533() throws Exception
272+
{
273+
ObjectMapper mapper = new ObjectMapper().enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
274+
AsPropertyWrapper wrapper = mapper.readValue("{ \"value\": \"\" }", AsPropertyWrapper.class);
275+
assertNull(wrapper.value);
276+
}
277+
250278
/*
251279
/**********************************************************
252280
/* Unit tests, serialization

0 commit comments

Comments
 (0)