Skip to content

Commit

Permalink
Add test for (now) fixed #301
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 21, 2020
1 parent ab310eb commit fd3926b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Project: jackson-dataformat-xml
(reported by Eric S)
#294: XML parser error with nested same element names
(reported by Alexei V)
#301: Problem deserializing POJO with unwrapped `List`, ignorable attribute value
#393: `MismatchedInputException` for nested repeating element name in `List`
(reported by kaizenHorse@github)
#399: Can not deserialize unwrapped list when `@JacksonXmlProperty` localName matches
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.*;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.xml.*;
Expand Down Expand Up @@ -40,7 +41,7 @@ public static class Value {
public int id;
}

// [Issue#108]: unwrapped lists, more than one entry, id attributes
// [dataformat-xml#108]: unwrapped lists, more than one entry, id attributes

static class Foo {
@JacksonXmlElementWrapper(useWrapping = false)
Expand All @@ -56,21 +57,45 @@ static class Bar {
public int id;
}

// [dataformat-xml#301]: mismatched attribute to skip
static class Parent301 {
@JacksonXmlProperty(localName = "MY_ATTR", isAttribute = true)
public String myAttribute;

@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "MY_PROPERTY")
public List<ChildA301> childrenA = new ArrayList<>();

@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "CHILDB")
public List<ChildB301> childrenB = new ArrayList<>();

}

static class ChildA301 { }

@JsonIgnoreProperties(ignoreUnknown = true)
static class ChildB301 {
@JacksonXmlProperty(localName = "MY_PROPERTY")
public Double value;
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

private final ObjectMapper MAPPER = newMapper();

// [Issue#43]
public void testIssue43() throws Exception
{
String xmlData = "<roomName><names>"
+"<name language=\"en\">SPECIAL</name>"
+"</names></roomName>";

XmlMapper xmlMapper = new XmlMapper();
RoomName roomName = xmlMapper.readValue(xmlData, RoomName.class);
RoomName roomName = MAPPER.readValue(xmlData, RoomName.class);
assertEquals(1, roomName.names.size());
assertEquals("SPECIAL", roomName.names.get(0).text);
}
Expand All @@ -82,16 +107,15 @@ public void testListWithAttributes() throws Exception
+ " <value id=\"1\"/>"
+ " <fail/>"
+ "</Root>";
ObjectMapper mapper = new XmlMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Root root = mapper.readValue(source, Root.class);
ObjectReader r = MAPPER.readerFor(Root.class)
.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Root root = r.readValue(source, Root.class);
assertNotNull(root.values);
assertEquals(1, root.values.size());
}

// [Issue#108]: unwrapped lists, more than one entry, id attributes
public void testIdsFromAttributes() throws Exception {
XmlMapper xmlMapper = new XmlMapper();
Foo foo = new Foo();
Bar bar1 = new Bar();
bar1.id = 1;
Expand All @@ -101,8 +125,23 @@ public void testIdsFromAttributes() throws Exception {
bar2.value = "SECOND";
bar2.id = 2;
foo.secondBar.add(bar2);
String string = xmlMapper.writeValueAsString(foo);
Foo fooRead = xmlMapper.readValue(string, Foo.class);
String string = MAPPER.writeValueAsString(foo);
Foo fooRead = MAPPER.readValue(string, Foo.class);
assertEquals(foo.secondBar.get(0).id, fooRead.secondBar.get(0).id);
}

public void testIssue301WithAttr() throws Exception {
final String XML =
"<PARENT>" +
" <CHILDB MY_ATTR='TEST_VALUE'>" +
" <MY_PROPERTY>12.25</MY_PROPERTY>" +
" </CHILDB>" +
"</PARENT>";
Parent301 result = MAPPER.readValue(XML, Parent301.class);
assertNotNull(result);
assertNotNull(result.childrenB);
assertEquals(1, result.childrenB.size());
assertEquals(Double.valueOf(12.25), result.childrenB.get(0).value);
}
}

0 comments on commit fd3926b

Please sign in to comment.