Skip to content
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

incorrect marshalling of sibling objects if one of them overrides parent object's property #1812

Open
j-a-young opened this issue Sep 4, 2024 · 1 comment

Comments

@j-a-young
Copy link

This issue first manifested in 4.0.0 and is still present in 4.0.5. In 3.0.2 this issue does not happen.

If you have 3 objects, Parent (fields: ID), ChildA (fields: name), ChildB (fields: Id, name) when you try to marshal an instance of ChildA, the ID field from Parent will not be marshaled if the Id filed is defined in ChildB.

Example XML, Parent.ID field missing:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Request xmlns="http://example.org/wsdl/testAPI">
    <Objects xsi:type="ChildA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Name>test-name</Name>
    </Objects>
</Request>

Example XML, Parent.ID present when Id removed from definition of ChildB:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Request xmlns="http://example.org/wsdl/testAPI">
    <Objects xsi:type="ChildA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ID>1234</ID>
        <Name>test-name</Name>
    </Objects>
</Request>

Sample code:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ChildA", propOrder = {
        "name"
})
public class ChildA extends Parent {
    @XmlElement(name = "Name")
    protected String name;

    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ChildB", propOrder = {
        "name", "id"
})
public class ChildB extends Parent {
    @XmlElement(name = "Name")
    protected String name;

    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }

    @XmlElement(name = "Id")
    protected Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer value) {
        this.id = value;
    }
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Parent", propOrder = {
        "id"
})
@XmlSeeAlso({ChildA.class, ChildB.class})
public class Parent {
    @XmlElement(name = "ID")
    protected Integer id;

    public Integer getID() {
        return id;
    }

    public void setID(Integer value) {
        this.id = value;
    }
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Request", propOrder = {
        "objects"
})
@XmlRootElement(name = "Request")
public class Request {
    @XmlElement(name = "Objects", required = true)
    protected List<Parent> objects;

    public List<Parent> getObjects() {
        if (objects == null) {
            objects = new ArrayList<>();
        }
        return this.objects;
    }
}

test to manifest issue:

    @Test
    public void test() throws JAXBException {
        ChildA child = new ChildA();
        child.setName("test-name");
        child.setID(1234);

        Request req = new Request();
        req.getObjects().add(child);

        final Marshaller marshaller = JAXBContext.newInstance(Request.class).createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter stringWriter = new StringWriter();
        marshaller.marshal(req, stringWriter);
        String xmlAsString = stringWriter.toString();
        System.out.println(xmlAsString);
    }

Note: an instance of ChildB isn't even created and the issue manifests. I first noticed this with objects automatically generated from a SOAP WSDL, but I was able to reproduce it with these hand crafted objects.

@denAbramoff
Copy link

Any news?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants