jackson-dataformat-xml icon indicating copy to clipboard operation
jackson-dataformat-xml copied to clipboard

Binary data does not get reset with deserialising from XML

Open farfalena-zz opened this issue 14 years ago • 3 comments

Related jackson-dataformat-xml version 2.0.4

While reading more than one data into byte[] we see that we always get the value of first data deserialised. This happens only with xml deserialisation and not with json deserialisation. It seems as if the _binaryValue does not get reset, as for example it is done in com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken()

See test case below, where xmlReadTwoData fails while jsonReadTwoData succeeds.

import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import java.io.IOException; import org.apache.commons.codec.binary.Base64; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class XmlToMultipleByteTest{

public static class Data {

    @JsonProperty
    byte[] bytes;
}

public static class TwoData {
    @JsonProperty
    Data data1;

    @JsonProperty
    Data data2;
}

@Test
public void xmlReadTwoData() throws JsonParseException, JsonMappingException, IOException {
    String xml = 
        "<TwoData>" +
                "<data1><bytes>" + Base64.encodeBase64String("Hello".getBytes()) + "</bytes></data1>" +
                "<data2><bytes>" + Base64.encodeBase64String("World".getBytes()) + "</bytes></data2>" +
        "</TwoData>";

    TwoData two = new XmlMapper().readValue(xml, TwoData.class);
    assertThat( new String(two.data1.bytes), is("Hello"));
    assertThat( new String(two.data2.bytes), is("World"));
}

@Test
public void jsonReadTwoData() throws JsonParseException, JsonMappingException, IOException {
    String xml = 
        "{" +
            "\"data1\":{\"bytes\":\"" + Base64.encodeBase64String("Hello".getBytes()) + "\"}," +
            "\"data2\":{\"bytes\":\"" + Base64.encodeBase64String("World".getBytes()) + "\"}" +
        "}";

    TwoData two = new ObjectMapper().readValue(xml, TwoData.class);
    assertThat( new String(two.data1.bytes), is("Hello"));
    assertThat( new String(two.data2.bytes), is("World"));
}

}

farfalena-zz avatar Jul 18 '12 15:07 farfalena-zz

Thank you for reporting this! Your hypothesis sounds reasonable, if so should be easy to fix.

tatu-at-salesforce avatar Jul 18 '12 18:07 tatu-at-salesforce

Yes, one too few '_binaryValue = null' statements in there. Sigh, I fixed this from json a while ago, and thought had done so from others (pretty sure I did fix this from Smile module). Better check CSV module as well.

cowtowncoder avatar Jul 18 '12 20:07 cowtowncoder

Checked in the fix, will be in 2.0.5 release.

cowtowncoder avatar Jul 18 '12 23:07 cowtowncoder