-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
XML parser error with nested same element names #294
Comments
I'd need to see plain Java version of the definition, otherwise this would be ... Kotlin? ... issue |
I can try with plain Java later but the same annotated code works just fine with JAXBContext unmarshaller instead of XmlMapper and you can assume i've post a plain java examples :) |
One thing to note is that Jackson is not a JAXB implementation (nor claims to be one). JAXB annotations are supported as configuration source, so there is sometimes assumption that behavior is (expected to be) identical. This is not to say there is attempt to work differently, just that there are sometimes differences due to bit different goals and history (JAXB being XML-specific framework, Jackson starting with json background). The biggest commonly encountered difference, which may be relevant here, is that Jackson defaults to "wrapped" Lists, whereas JAXB defaults to "unwrapped". One thing that has helped me debug issues is to start by serializing content from POJO (POKO?), and having a look if that structure differs from structure you'd expect to read. |
Here is a sample unit test in pure Java that creates an XML from object and try to parse it back. Finnaly i got the same exception.
|
@softkot Thank you. |
Any plans to fix it ? |
(copied here for convenience) import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
public class XmlMapperTest {
private Logger logger = Logger.getLogger("XmlMapperTest");
@JacksonXmlRootElement(localName = "levels")
static class RootLevel {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "sublevel")
List<Sublevel> sublevels = new ArrayList<>();
}
@JacksonXmlRootElement(localName = "sublevel")
static class Sublevel {
@JacksonXmlProperty
Integer id;
@JacksonXmlProperty
String sublevel;
}
Sublevel newSublevel(Integer id, String sublevel) {
Sublevel res = new Sublevel();
res.id = id;
res.sublevel = sublevel;
return res;
}
@Test
public void parserTest() throws IOException {
RootLevel tree = new RootLevel();
tree.sublevels.add(newSublevel(1, "Name A"));
tree.sublevels.add(newSublevel(2, "Name B"));
XmlMapper mapper = new XmlMapper();
String xml = mapper.writeValueAsString(tree);
logger.info(xml);
RootLevel resTree = mapper.readValue(xml.getBytes(), RootLevel.class);
logger.info(Integer.toString(resTree.sublevels.size()));
}
} |
@softkot I hope to find time to look into this in near future: it is one of top items, but my time is limited on working for Jackson, and there is one set of security vulnerabilities I need to get fixed to get Thank you for adding all the information here, I hope to get this fixed soon. |
Hmmh. Odd. Changing order of properties |
Is there at least a workaround to avoid this error? For example by using a custom serializer (I tried but it did not work)? |
The fix I suggest in #301 fixes this problem. |
Hello, my problem not exactly the same but may be caused by same reason. I tried to write tests for enums annotated with Jackson 2.9.8 Both serialization and deserialization works perfectly for complex objects containing this enums but enums itself fails. What I found so far: Sample code to reproduce: import static org.junit.Assert.assertEquals;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;
import java.util.Objects;
import org.junit.Test;
public class EnumTest {
private final XmlMapper xmlMapper = new XmlMapper();
// Failing
@Test
public void it_should_deserialize_simple() throws IOException {
final String xmlString = xmlMapper.writeValueAsString(SimpleEnum.FOO);
System.out.println(xmlString);
assertEquals(SimpleEnum.FOO, xmlMapper.readValue(xmlString, SimpleEnum.class));
}
// Failing
@Test
public void it_should_deserialize_complex() throws IOException {
final String xmlString = xmlMapper.writeValueAsString(ComplexEnum.BAR);
System.out.println(xmlString);
assertEquals(ComplexEnum.BAR, xmlMapper.readValue(xmlString, ComplexEnum.class));
}
// Works fine
@Test
public void it_should_deserialize_object() throws IOException {
final SomeObject object = new SomeObject();
final String xmlString = xmlMapper.writeValueAsString(object);
System.out.println(xmlString);
assertEquals(object, xmlMapper.readValue(xmlString, SomeObject.class));
}
}
enum SimpleEnum {
FOO
}
enum ComplexEnum {
BAR(18);
@JsonValue
private final int value;
ComplexEnum(int value) {
this.value = value;
}
@JsonCreator
public static ComplexEnum forValue(int value) {
if (value == 18) {
return BAR;
}
throw new IllegalArgumentException("Unsupported value");
}
}
class SomeObject {
public SimpleEnum simpleEnum = SimpleEnum.FOO;
public ComplexEnum complexEnum = ComplexEnum.BAR;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SomeObject that = (SomeObject) o;
return simpleEnum == that.simpleEnum && complexEnum == that.complexEnum;
}
@Override
public int hashCode() {
return Objects.hash(simpleEnum, complexEnum);
}
} Output (just in case):
Best regards |
Hi, Is this issue still present? I am facing a similar problem with the following xml:
I got following error message:
When adding a custom converter, eg:
everything is fine. Same issue with double. Any ideas? I am using jackson-core, jackson-databind and jackson-dataformat-xml in version 2.10.3. Best regards |
@douglasmedia I think the problem is existence fo both attribute and element for property: I don't think this is quite the same issue. I would suggest filing a separate issue since it might be little bit easier to tackle, given that |
Same problem as #393, or at least same fix: will be included in 2.11.1. |
Jackson version 2.9.5
I have a simple xml like
Note that child sublevel has a property sublevel (element with the same name as enclosing tag). That structure has an issue for XmlMapper causing an exception
XmlMapper configured as
And POJO objects annotated as
The text was updated successfully, but these errors were encountered: