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>
  • Loading branch information
OritMarkus committed Dec 9, 2021
1 parent c60c3d8 commit be6d4fd
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,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 be6d4fd

Please sign in to comment.