highsource / ogc-schemas

XML<->Java and XML<->JS for OGC XSDs.
BSD 2-Clause "Simplified" License
82 stars 49 forks source link

Unable to Marshal or Unmarshal SosInsertionMetadata on swes:InsertSensor #192

Closed charlesmoore99 closed 6 years ago

charlesmoore99 commented 6 years ago

I tested this against both the 2.6.1 release version and the 2.6.2-SNAPSHOT.

Unmarshalling a valid swes:InsertSensor document throws the following exception when an sos2:SosInsertionMetadata element is encountered

Note: without a ValidationEventHandler on the unmarshaller, JAXB silently fails to process the SosInsertionMetadata Element and calls to the getMetadata() method in the resulting insertSensor object return null.

java.lang.RuntimeException: unexpected element (uri:"http://www.opengis.net/sos/2.0", local:"SosInsertionMetadata"). Expected elements are <{http://www.opengis.net/swes/2.0}InsertionMetadata>
    at dev.scratch.mavenProject.SosMetadataTest$1.handleEvent(SosMetadataTest.java:86)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:716)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.childElement(Loader.java:90)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.childElement(StructureLoader.java:237)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2784)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
    at dev.scratch.mavenProject.SosMetadataTest.testSosMetadata(SosMetadataTest.java:91)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

The following java code demonstrates the above exception. Also included is a unit test that show that it is possible to trick JAXB into unmarshalling the SosInsertionMetadata via an xsi:type attribute

package dev.scratch.mavenProject;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.io.FileNotFoundException;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;

import org.junit.Test;

import net.opengis.sos.v_2_0.SosInsertionMetadataType;
import net.opengis.swes.v_2_0.InsertSensorType;
import net.opengis.swes.v_2_0.InsertSensorType.Metadata;

/**
 * Unit test demonstrating inability to unmarshal SosInsertionMetadata
 * on an swes:InsertSensor request
 */
public class SosMetadataTest {

    // this is a valid insertSensor document, but JAXB doesn't know how to marshal
    // the sos2:SosInsertionMetadata element
    public static final String validButUnmarshallableInsertSensorWithMetadata = ""
            + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
            + "<swes:InsertSensor " 
            + "version=\"2.0.0\" "
            + "service=\"SOS\" " 
            + "xmlns:swes=\"http://www.opengis.net/swes/2.0\" "
            + "xmlns:sos2=\"http://www.opengis.net/sos/2.0\" "
            + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" 
            + " <swes:procedureDescriptionFormat/>"
            + " <swes:procedureDescription>" 
            + "     <auto-generated_for_wildcard/>"
            + " </swes:procedureDescription>" 
            + " <swes:observableProperty/>" 
            + " <swes:metadata>"
            + "     <sos2:SosInsertionMetadata>" 
            + "         <sos2:observationType>Obs_type</sos2:observationType>"
            + "         <sos2:featureOfInterestType>FOI_type</sos2:featureOfInterestType>"
            + "     </sos2:SosInsertionMetadata>" 
            + " </swes:metadata>" 
            + "</swes:InsertSensor>";

    // this is insertSensor document fails validation in XMLSpy because the 
    // swes:InsertionMetadata element is defined as abstract.  But, JAXB interprets 
    // the xsi:type attribute and stores the contents in an SosInsertionMeta object
    public static final String wierdButUnmarshalledInsertSensorWithMetadata = ""
            + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
            + "<swes:InsertSensor " 
            + "version=\"2.0.0\" "
            + "service=\"SOS\" " 
            + "xmlns:swes=\"http://www.opengis.net/swes/2.0\" "
            + "xmlns:sos2=\"http://www.opengis.net/sos/2.0\" "
            + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" 
            + " <swes:procedureDescriptionFormat/>"
            + " <swes:procedureDescription>" 
            + "     <auto-generated_for_wildcard/>"
            + " </swes:procedureDescription>" 
            + " <swes:observableProperty/>" 
            + " <swes:metadata>"
            + "     <swes:InsertionMetadata xsi:type='sos2:SosInsertionMetadataType'>"
            + "         <sos2:observationType>Obs_type</sos2:observationType>"
            + "         <sos2:featureOfInterestType>FOI_type</sos2:featureOfInterestType>"
            + "     </swes:InsertionMetadata>" 
            + " </swes:metadata>" 
            + "</swes:InsertSensor>";

    @Test
    public void testSosMetadata() throws JAXBException, FileNotFoundException {
        JAXBContext context = JAXBContext.newInstance("net.opengis.sos.v_2_0:net.opengis.swes.v_2_0");

        Unmarshaller unmarshaller = context.createUnmarshaller();

        // without this validation handler JAXB fails silently 
        unmarshaller.setEventHandler(new ValidationEventHandler() {
            public boolean handleEvent(ValidationEvent event) {
                throw new RuntimeException(event.getMessage(), event.getLinkedException());
            }
        });

        StringReader in = new StringReader(validButUnmarshallableInsertSensorWithMetadata);
        JAXBElement<InsertSensorType> insertSensorElement = (JAXBElement<InsertSensorType>) unmarshaller.unmarshal(in);

        InsertSensorType insertSensor = insertSensorElement.getValue();
        assertEquals("Incorrect amount of metadata", 1, insertSensor.getMetadata().size());

        Metadata metadata = insertSensor.getMetadata().get(0);
        assertNotNull("metadata is null");

        if (!metadata.isSetInsertionMetadata()) {
            fail("metadata is not set");
        }

        SosInsertionMetadataType simt = (SosInsertionMetadataType) metadata.getInsertionMetadata();
        assertNotNull("Missing SOS INnsertion Metadata", simt);

        assertEquals(1, simt.getFeatureOfInterestType().size());
        assertEquals("FOI_type", simt.getFeatureOfInterestType().get(0));

        assertEquals(1, simt.getObservationType().size());
        assertEquals("Obs_type", simt.getObservationType().get(0));

    }

    @Test
    public void testWierdSosMetadata() throws JAXBException, FileNotFoundException {
        JAXBContext context = JAXBContext.newInstance("net.opengis.sos.v_2_0:net.opengis.swes.v_2_0");

        Unmarshaller unmarshaller = context.createUnmarshaller();

        // without this validation handler JAXB fails silently 
        unmarshaller.setEventHandler(new ValidationEventHandler() {
            public boolean handleEvent(ValidationEvent event) {
                throw new RuntimeException(event.getMessage(), event.getLinkedException());
            }
        });

        StringReader in = new StringReader(wierdButUnmarshalledInsertSensorWithMetadata);
        JAXBElement<InsertSensorType> insertSensorElement = (JAXBElement<InsertSensorType>) unmarshaller.unmarshal(in);

        InsertSensorType insertSensor = insertSensorElement.getValue();
        assertEquals("Incorrect amount of metadata", 1, insertSensor.getMetadata().size());

        Metadata metadata = insertSensor.getMetadata().get(0);
        assertNotNull("metadata is null");

        if (!metadata.isSetInsertionMetadata()) {
            fail("metadata is not set");
        }

        SosInsertionMetadataType simt = (SosInsertionMetadataType) metadata.getInsertionMetadata();
        assertNotNull("Missing SOS INnsertion Metadata", simt);

        assertEquals(1, simt.getFeatureOfInterestType().size());
        assertEquals("FOI_type", simt.getFeatureOfInterestType().get(0));

        assertEquals(1, simt.getObservationType().size());
        assertEquals("Obs_type", simt.getObservationType().get(0));
    }
}
charlesmoore99 commented 6 years ago

Issue appears to be that the class net.opengis.swes.v_2_0_0.InsertionMetadataType in swes 2.0 needs to have the @XmlSeeAlso(SosInsertionMetadataType.class) annotation added to it.

I have no idea how to add this annotation using this build framework.

highsource commented 6 years ago

Could you commit your tests as PR in swes or sos modules? (Please move your sample XML to a resource.)

You include net.opengis.sos.v_2_0 into the context path, so actually SosInsertionMetadata should be known in the context.

I know how to add @XmlSeeAlso, but it is not the right solution. The JAXB context should be defined by the context path dynamically, not by annotations statically.

Please PR your tests, I'll take a look.

charlesmoore99 commented 6 years ago

Thanks!

Pull Request created.

highsource commented 6 years ago

Thanks for the PR. The problem is that metadata property of InsertSensorType must be a reference property, otherwise substitution groups are not supported.

highsource commented 6 years ago

I have patches SWES so that InsertMetadata element is generated as a reference property. With this patch I get both examples unmarshal allright, both tests are green.

Please give it a try before. You'll need to get the latest version and build it. Takes a while.

charlesmoore99 commented 6 years ago

I pulled down the changes and rebuilt. Unit tests passed. Also ran tests in a stand alone environment, those passed too.

highsource commented 6 years ago

Ok, I'll try to release in the next days then.