-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
NOTE: see #4471 for current state -- this was done for 2.17.0 release but reverted for 2.17.1
Describe the bug
It looks like the fallback value in JsonNode#asText(String)
never gets a chance to appear as the 0-arg asText()
should never return null
, according to its Javadoc:
jackson-databind/src/main/java/tools/jackson/databind/JsonNode.java
Lines 604 to 623 in dca0c27
/** | |
* Method that will return a valid String representation of | |
* the container value, if the node is a value node | |
* (method {@link #isValueNode} returns true), | |
* otherwise empty String. | |
*/ | |
public abstract String asText(); | |
/** | |
* Method similar to {@link #asText()}, except that it will return | |
* <code>defaultValue</code> in cases where null value would be returned; | |
* either for missing nodes (trying to access missing property, or element | |
* at invalid item for array) or explicit nulls. | |
*/ | |
public String asText(String defaultValue) { | |
String str = asText(); | |
return (str == null) ? defaultValue : str; | |
} | |
This seems to be the case in all implementations of asText()
I can see, and has been corroborated in other issues such as #25.
The only exception is if someone calls TextNode(String)
directly with null
as the input. TextNode#valueOf(String)
already converts these cases into null
references, so the "almost" in the title is quite strong.
Version Information
2.16.1
Expected behavior
Either replace the null
check with an emptyness check (but then "legitimate empty" strings will also use the default value), or deprecate the method? Not quite sure!