Skip to content

XmlMapper maps null value of xml element with atribute #734

@stefanCotruta

Description

@stefanCotruta

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Jackson 2.17.2 correctly maps the xml element containing an attribute to a Java record. If I use 2.18.3 (was: 2.18.2), it only recognizes the attribute but not the value.

Version Information

2.18.2 (tested with 2.18.3 too)

Reproduction

public class DemoApplication {

    public static void main(String[] args) {
        String xml = "<Amt Ccy=\"EUR\">1</Amt>";

        XmlMapper mapper = new XmlMapper();
        Assertions.assertDoesNotThrow(() -> mapper.readValue(xml, Amount.class));        
    }

    private record Amount(@JacksonXmlProperty String value, @JacksonXmlProperty(isAttribute = true, localName = "Ccy") String currency) {}
}
// Using 2.17.2 for both dependencies instead of 2.18.2 works correctly without any code changes
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.18.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.18.2</version>
        </dependency>

Output

Exception in thread "main" org.opentest4j.AssertionFailedError: Unexpected exception thrown: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" (class com.example.demo.DemoApplication$Amount), not marked as ignorable (2 known properties: "value", "Ccy"])
 at [Source: (StringReader); line: 1, column: 23] (through reference chain: com.example.demo.DemoApplication$Amount[""])
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:152)
	at org.junit.jupiter.api.AssertDoesNotThrow.createAssertionFailedError(AssertDoesNotThrow.java:84)
	at org.junit.jupiter.api.AssertDoesNotThrow.assertDoesNotThrow(AssertDoesNotThrow.java:75)
	at org.junit.jupiter.api.AssertDoesNotThrow.assertDoesNotThrow(AssertDoesNotThrow.java:58)
	at org.junit.jupiter.api.Assertions.assertDoesNotThrow(Assertions.java:3228)
	at com.example.demo.DemoApplication.main(DemoApplication.java:13)
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" (class com.example.demo.DemoApplication$Amount), not marked as ignorable (2 known properties: "value", "Ccy"])
 at [Source: (StringReader); line: 1, column: 23] (through reference chain: com.example.demo.DemoApplication$Amount[""])
	at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:1153)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:2245)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1821)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperties(BeanDeserializerBase.java:1771)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:561)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1497)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
	at com.fasterxml.jackson.dataformat.xml.deser.XmlDeserializationContext.readRootValue(XmlDeserializationContext.java:104)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4931)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3868)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3836)
	at com.example.demo.DemoApplication.lambda$main$0(DemoApplication.java:13)

Additional context

Same issue with 2.18.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateIssue is duplicate of another issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions