FasterXML / jackson-modules-java8

Set of support modules for Java 8 datatypes (Optionals, date/time) and features (parameter names)
Apache License 2.0
399 stars 116 forks source link

StdDeserializer rejects all-whitespace strings for ints #225

Closed pburka closed 3 years ago

pburka commented 3 years ago

In Jackson 2.10.2 the standard deserializer converts empty strings and all-whitespace strings to zero when deserializing to ints. In 2.12 the default behavior changed: it accepts blank strings but not all-whitespace strings. I imagine this is related to the changes to leniency handling in 2.12. While it's great to be able to configure leniency, for backwards compatibility the library should maintain the old default behavior.

Test case:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonDeserialize
public class Jack {
    private final int j;

    @JsonCreator
    public Jack(@JsonProperty("J") int j) {
        this.j = j;
    }

    private static void test(String s) {
        final String json = "{ \"J\" : \"" + s + "\" }";
        try {
            final Jack jack = new ObjectMapper().readValue(json, Jack.class);
            System.out.println("j=" + jack.j);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // With Jackson 2.10.2 these all print j=0
        test("0");
        test("0 ");
        test(" 0");
        test(" 0 ");
        test("");
        // With Jackson 2.12.4 these throw InvalidFormatException
        test(" ");
        test("    ");
        test("\\n");
    }
}

Expected behavior: the default deserializer should behave consistently in 2.10 and 2.12.

Actual behavior: the 2.12 deserializer is less lenient and rejects non-empty strings that contain only whitespace.

pburka commented 3 years ago

Wrong component. See https://github.com/FasterXML/jackson-databind/issues/3234