Skip to content

Missing collection item when they are not wrapped during unmarshal with multiple namespaces #307

@wanchongtai

Description

@wanchongtai
  1. I have the following xml to unmarshal
<?xml version="1.0" encoding="utf-8"?>
<customer xmlns="http://www.archer-tech.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <customerId>1</customerId>
    <customerName>Michael Judy</customerName>
    <account>
        <accountId>100</accountId>
        <accountName>Michael</accountName>
        <postcode xsi:nil="true"></postcode>
    </account>
    <account>
        <accountId>200</accountId>
        <accountName>Judy</accountName>
        <postcode xsi:nil="true"></postcode>
    </account>	
</customer>
  1. domains
@JacksonXmlRootElement(localName = "customer")
public class CustomerWithoutWrapper {
    Long customerId;
    String customerName;

    @JacksonXmlElementWrapper(useWrapping = false)
    List<Account> account = new ArrayList<>();
...setter/getter...
}

public class Account {
    Long accountId;
    String accountName;
    String postcode;
...setter/getter...
}
  1. unmarshal process
        ObjectMapper objectMapper = new XmlMapper();
        CustomerWithoutWrapper customerWithOutWrapper =
                objectMapper.readValue(getCustomerWithoutWrapperXml(), CustomerWithoutWrapper.class);
  1. result

there is not exception, however only the last account item is kept. the first account item is missing from account collection after unmarshalling

incorrect result

CustomerWithoutWrapper{customerId=1, customerName='Michael Judy', account=[Account{accountId=200, accountName='Judy', postcode=''}]}
  1. however when account items are wrapped as following, and change the domain mapping accordingly, then Jackson can unmarshall xml correctly.
    e.g.
<?xml version="1.0" encoding="utf-8"?>
<customer xmlns="http://www.archer-tech.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<customerId>1</customerId>
	<customerName>Michael Judy</customerName>
	<accounts>
		<account>
			<accountId>100</accountId>
			<accountName>Michael</accountName>
			<postcode xsi:nil="true"></postcode>
		</account>
		<account>
			<accountId>200</accountId>
			<accountName>Judy</accountName>
			<postcode xsi:nil="true"></postcode>
		</account>	
	</accounts>
</customer>
@JacksonXmlRootElement(localName = "customer")
public class CustomerWithWrapper {
    Long customerId;

    String customerName;

    @JacksonXmlElementWrapper(localName = "accounts")
    List<Account> account = new ArrayList<>();
...
}

result - correct when accounts are wrapped.

CustomerWithWrapper{customerId=1, customerName='Michael Judy', account=[Account{accountId=100, accountName='Michael', postcode=''}, Account{accountId=200, accountName='Judy', postcode=''}]}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions