|
| 1 | +package com.fasterxml.jackson.dataformat.xml.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 4 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 5 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 6 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 7 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText; |
| 8 | + |
| 9 | +public class EmptyBeanDeser318Test extends XmlTestBase |
| 10 | +{ |
| 11 | + static class Wrapper { |
| 12 | + @JacksonXmlProperty(localName = "id") |
| 13 | + String id; |
| 14 | + @JacksonXmlProperty(localName = "nested") |
| 15 | + Nested nested; |
| 16 | + } |
| 17 | + |
| 18 | + static class Nested { |
| 19 | + @JacksonXmlProperty(localName = "nested2") |
| 20 | + Nested2 nested2; |
| 21 | + } |
| 22 | + |
| 23 | + static class Nested2 { |
| 24 | + @JacksonXmlProperty(localName = "attr", isAttribute = true) |
| 25 | + String attr; |
| 26 | + @JacksonXmlText |
| 27 | + String value; |
| 28 | + } |
| 29 | + |
| 30 | + /* |
| 31 | + /********************************************************************** |
| 32 | + /* Test methods |
| 33 | + /********************************************************************** |
| 34 | + */ |
| 35 | + |
| 36 | + private final XmlMapper MAPPER = newMapper(); |
| 37 | + |
| 38 | + public void testEmptyString() throws Exception { |
| 39 | + String s = "<wrapper>" |
| 40 | + + " <id>id</id>" |
| 41 | + + " <nested></nested>" |
| 42 | + + "</wrapper>"; |
| 43 | + |
| 44 | + Wrapper value = MAPPER.readValue(s, Wrapper.class); |
| 45 | + assertEquals("id", value.id); |
| 46 | + assertNull(value.nested); |
| 47 | + } |
| 48 | + |
| 49 | + public void testBlankString() throws Exception { |
| 50 | + String s = "<wrapper>" |
| 51 | + + " <id>id</id>" |
| 52 | + + " <nested> </nested>" |
| 53 | + + "</wrapper>"; |
| 54 | + |
| 55 | + // This fails with the following exception: |
| 56 | + // com.fasterxml.jackson.databind.exc.MismatchedInputException: |
| 57 | + // Cannot construct instance of `JacksonXMLTest$Nested` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (' ') |
| 58 | + Wrapper value = MAPPER.readValue(s, Wrapper.class); |
| 59 | + assertEquals("id", value.id); |
| 60 | + assertNull(value.nested); |
| 61 | + } |
| 62 | + |
| 63 | + public void testBlankString2() throws Exception { |
| 64 | + String s = "<wrapper>" |
| 65 | + + " <id>id</id>" |
| 66 | + + " <nested> </nested>" |
| 67 | + + "</wrapper>"; |
| 68 | + |
| 69 | + // This fails with the following exception: |
| 70 | + // com.fasterxml.jackson.databind.exc.MismatchedInputException: |
| 71 | + // Cannot construct instance of `JacksonXMLTest$Nested` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (' ') |
| 72 | + Wrapper value = MAPPER.readerFor(Wrapper.class) |
| 73 | + .with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT) |
| 74 | + .readValue(s); |
| 75 | + assertEquals("id", value.id); |
| 76 | + assertNull(value.nested); |
| 77 | + } |
| 78 | + |
| 79 | + public void testMissing() throws Exception { |
| 80 | + String s = "<wrapper>" |
| 81 | + + " <id>id</id>" |
| 82 | + + "</wrapper>"; |
| 83 | + |
| 84 | + Wrapper value = MAPPER.readValue(s, Wrapper.class); |
| 85 | + assertEquals("id", value.id); |
| 86 | + assertNull(value.nested); |
| 87 | + } |
| 88 | + |
| 89 | + public void testValidStructure() throws Exception { |
| 90 | + String s = "<wrapper>" |
| 91 | + + " <id>id</id>" |
| 92 | + + " <nested>" |
| 93 | + + " <nested2 attr=\"test\"><![CDATA[Some text]]></nested2>" |
| 94 | + + " </nested>" |
| 95 | + + "</wrapper>"; |
| 96 | + |
| 97 | + Wrapper value = MAPPER.readValue(s, Wrapper.class); |
| 98 | + assertEquals("id", value.id); |
| 99 | + assertEquals("test", value.nested.nested2.attr); |
| 100 | + assertEquals("Some text", value.nested.nested2.value); |
| 101 | + } |
| 102 | +} |
0 commit comments