Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Unexpected output with arrays starting with a null/empty element #90

Closed
cheseaux opened this issue Sep 3, 2015 · 3 comments
Closed
Milestone

Comments

@cheseaux
Copy link

cheseaux commented Sep 3, 2015

Take this small example :

public class MyPojo {

    public String[] getVal1() {
        return new String[]{"", "foo"};
    }

    public String[] getVal2() {
        return new String[]{null, "bar"};
    }

    public String[] getVal3() {
        return new String[]{"baz",null};
    }

    public static void main(String[] args) throws JsonProcessingException {
        MyPojo jacksonBug = new MyPojo();
        CsvMapper mapper = new CsvMapper();
        String csvContent = mapper.writer(mapper.schemaFor(MyPojo.class).withHeader()).writeValueAsString(jacksonBug);
        System.out.println(csvContent);
    }

}

Output is :

val1,val2,val3
foo,bar,baz;

But should be :

val1,val2,val3
;foo,;bar,baz;

@cowtowncoder
Copy link
Member

Which version?

@cheseaux
Copy link
Author

cheseaux commented Sep 3, 2015

Sorry, forgot to mention it : v 2.6.1 but was also present in 2.5

@cheseaux
Copy link
Author

cheseaux commented Sep 3, 2015

Ok, I've found where the bug is.

In CsvGenerator.java, the two methods _addToArray inserts the array separator character if at least one character was already appended to _arrayContents :

protected void _addToArray(String value) {
    if (_arrayContents.length() > 0) {
        _arrayContents.append((char) _arraySeparator);
    }
    _arrayContents.append(value);
}

protected void _addToArray(char[] value) {
    if (_arrayContents.length() > 0) {
        _arrayContents.append((char) _arraySeparator);
    }
    _arrayContents.append(value);
}

But when the first element of the array is empty or null, then this condition will return false and no array separator character will be appended.

This doesn't seem to be easily solvable, but I'll keep you updated if I find something.

@cowtowncoder cowtowncoder added this to the 2.6.4 milestone Oct 23, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants