Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FAIL_ON_NULL_FOR_PRIMITIVES apply to primitive arrays and other types that wrap primitives #403

Closed
harleensahni opened this issue Feb 12, 2014 · 7 comments
Milestone

Comments

@harleensahni
Copy link

harleensahni commented Feb 12, 2014

DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES only seems to work on primitive properties on beans. It doesn't seem to work on reading straight values (I don't care that much about this), and reading primitive arrays.

In addition, it would be good if this functionality, or a new DeserializationFeature was added to support failing on nulls in Lists that store wrap primitive types since people often use List as a property type instead of int[] for convenience.

Here is a test case I wrote to demonstrate this:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class TestNullPrimitives {

    public static class PrimitiveTest{
        public int value;
        public int[] valueArray;
        public List<Integer> valueList;
    }

    @Test(expected = JsonMappingException.class)
    public void testReadNullInt() throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
        // Reads value as 0
        int value  = mapper.readValue("null", int.class);
        System.out.println("value:" + value);

    }

    @Test(expected = JsonMappingException.class)
    public void testObjectReadNullProperty() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
        // throws exception
        PrimitiveTest testObject = mapper.readValue("{\"value\":null}", PrimitiveTest.class);
    }
    @Test(expected = JsonMappingException.class)
    public void testObjectReadNullArrayEntryInProperty() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
        // Reads valueArray as [0]
        PrimitiveTest testObject = mapper.readValue("{\"valueArray\":[null]}", PrimitiveTest.class);
        System.out.println("Contents:" + Arrays.toString(testObject.valueArray));
        System.out.println("Size:" + testObject.valueArray.length);
    }

    @Test(expected = JsonMappingException.class)
    public void testObjectReadNullListEntryInProperty() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
        // Reads valueList as [null]
        PrimitiveTest testObject = mapper.readValue("{\"valueList\":[null]}", PrimitiveTest.class);
        System.out.println("Contents:" + testObject.valueList);
        System.out.println("Size:" + testObject.valueList.size());
    }
}
@cowwoc
Copy link

cowwoc commented Apr 28, 2014

I've got another case that I believe should be covered by this issue (the above testcase doesn't seem to include it):

  1. I am deserializing a constructor that takes multiple @JsonPropertys.
  2. Due to a typo in a property name, one of the constructor arguments (an int) has no mapped value.
  3. Jackson maps the missing value to 0, instead of throwing an exception. This is in spite of the fact that DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES is set.

Should I file a separate bug report for this?

@cowtowncoder
Copy link
Member

@cowwoc Yes, please file a separate bug, can reference this one as related.

@cowwoc
Copy link

cowwoc commented Apr 28, 2014

Done: #446

@cowtowncoder
Copy link
Member

One comment here; I do not think this feature should fail for List<Integer>, as Integer is not primitive but a wrapper. While I understand while it sometimes would be nice (given that Java does not allow List<int>), it would be misnomer, and specifically confusing if it didn't apply to stand-alone wrapper values. Further, if it did apply to stand-alone values, it would be unintuitive as major usage reason for Integer over int is specifically to allow nulls.

Anyway; I consider this to only apply for int[] etc.

@shmosel
Copy link

shmosel commented Mar 2, 2017

Any update on the primitive array bug? Or can anyone suggest a workaround to force it to fail?

@cowtowncoder
Copy link
Member

@shmosel I try to update issues with notes when there is progress so no updates. But as luck would have it I did work in related area for 2.9.0.pr1 so I hope to have a look at this relatively soon (for 2.9.0.pr2 at least -- not sure how safe behavioral change this would be for 2.8)

@cowtowncoder cowtowncoder added this to the 2.9.0.pr2 milestone Mar 8, 2017
@cowtowncoder cowtowncoder changed the title Make FAIL_ON_NULL_FOR_PRIMITIVES apply to primitive arrays and lists for objects that wrap primitives Make FAIL_ON_NULL_FOR_PRIMITIVES apply to primitive arrays and other types that wrap primitives Mar 8, 2017
@cowtowncoder
Copy link
Member

Implemented for 2.9.0(.pr2), added tests: should cover both coercion from literal null and empty String, similar to how it works for primitive-valued POJO properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants