Skip to content

Commit

Permalink
fix bug - Marshalling an object that overrides the parent's method, t…
Browse files Browse the repository at this point in the history
…he XML that created contains both child's and parent's tag, the problem was in serializeBody method in the reflection section. the condition should check if the super class contains the declared field that the child overrides. (eclipse-ee4j#1590)

* fix bug - Marshalling an object that overrides the parent's method, the XML that created contains both child's and parent's tag, the problem was in serializeBody method in the reflection section. the condition should check if the super class contains the declared field that the child overrides.

DTOs:
@XmlRootElement(name = "parent")
public class ParentDTO {
	Protected String name;

	@xmlelement(name= “parentName”)
	getName) {
        return name;
    }
	setName(String name) {
        this.name = name;
    }
}

@XmlRootElement(name = "child")
public class ChildDTO extends ParentDTO {

   @OverRide
   @xmlelement(name="childName")
    public String getName() {
        return name;
    }
}

Program:
Child child = new Child();
child.setName("aaa");

final Marshaller marshaller = JAXBContext.newInstance(ChildDTO.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter stringWriter = new StringWriter();
        marshaller.marshal(ChildDTO, stringWriter);
        String xmlAsString = stringWriter.toString();

XML after Marshall:
<child>
	<parentName> aa </parentName >
	<childName> aa </ childName >
</child>

(cherry picked from commit be6d4fd)
  • Loading branch information
OritMarkus authored and lukasj committed Jan 27, 2022
1 parent 0f2a40b commit 3d24738
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ public void serializeBody(BeanT bean, XMLSerializer target) throws SAXException,
if (!isThereAnOverridingProperty || bean.getClass().equals(jaxbType)) {
p.serializeBody(bean, target, null);
} else if (isThereAnOverridingProperty) {
// need to double check the override - it should be safe to do after the model has been created because it's targeted to override properties only
Class beanClass = bean.getClass();
if (Utils.REFLECTION_NAVIGATOR.getDeclaredField(beanClass, p.getFieldName()) == null) {
// need to double check the override - it should be safe to do after the model has been created because it's targeted to override properties only
Class beanSuperClass = bean.getClass().getSuperclass();
if (Utils.REFLECTION_NAVIGATOR.getDeclaredField(beanSuperClass, p.getFieldName()) == null) {
p.serializeBody(bean, target, null);
}
}
Expand Down

0 comments on commit 3d24738

Please sign in to comment.