javaee / jaxb-v2

Other
210 stars 100 forks source link

Unmarshalling xml contents with own namespace results in null fields. #1180

Open LanceAndersen opened 6 years ago

LanceAndersen commented 6 years ago

Previously tracked as https://bugs.openjdk.java.net/browse/JDK-8191965

FULL PRODUCT VERSION : 1.8.0_152

ADDITIONAL OS VERSION INFORMATION : OS X EL Capitan 10.11.1

A DESCRIPTION OF THE PROBLEM : when I tried to unmarshal xml contents with its own namespace, every field in a tag with the namespace is null. But, with jdk version 1.8.0_101 it works without any trouble. For example, unmarshalling the xml into the java class below, you can see the id value is "foo" with jdk version 1.8.0_101 but is null with 1.8.0_152.

<?xml version="1.0" encoding="UTF-8"?>

foo

public class Message {

public Result result; 

public static class Result { 
    public String id; 
} 

}

I found the source code of [com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader] has been changed a little bit. Because of the difference of method name of the [childElement], [UnmarshallingContext] works with [com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader] in 1.8.0_101, but [com.sun.xml.internal.bind.v2.runtime.unmarshaller.Discarder] in 1.8.0_152 when starting to unmarshal the tag with namespace.

I'm not sure whether you intended this, but the thing is working differently depending on the jdk version.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : Just unmarshal the xml into the java class below. It works differently depending on jdk version. (1.8.0_152, 1.8.0_101)

<?xml version="1.0" encoding="UTF-8"?>

foo

public class Message {

public Result result; 

public static class Result { 
    public String id; 
} 

}

EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - jdk version 1.8.0_101 -> result.id = "foo" ACTUAL - jdk versoin 1.8.0_152 -> result.id = null

ERROR MESSAGES/STACK TRACES THAT OCCUR : no error message.

REPRODUCIBILITY : This bug can be reproduced always.

---------- BEGIN SOURCE ---------- @Test public void unmarshal_with_xml_including_namespace() { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><message type=\"response\" version=\"1.0.0\"><result xmlns=\"com.naver.com\">foo";

    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); 
    jaxb2Marshaller.setClassesToBeBound(Message.class); 

    Source source = new StreamSource(new StringReader(xml)); 
    Message result = (Message) jaxb2Marshaller.unmarshal(source); 

    assertThat(result.result.getId(), is(nullValue())); 
} 

---------- END SOURCE ----------