-
-
Notifications
You must be signed in to change notification settings - Fork 83
Description
Use case: JAXB unmarshalling with schema validation enabled, unmarshalling an invalid XML document
Environment: WstxSAXParser 6.6.0 for parsing, JDK 17 Xerces for validation, jaxb-runtime 4.0.4
Expected outcome: org.xml.sax.SAXParseException with a useful message (e.g., "Content of element x incomplete, y expected")
Actual outcome: java.lang.AssertionError without any useful message or cause
We are using the Woodstox SAX parser with JAXB as a drop-in replacement for JDK's default SAX parser due to better performance. We have enabled XML Schema validation for JAXB by calling Unmarshaller.setSchema(). As Woodstox does not provide a javax.xml.validation.SchemaFactory, JAXB automatically falls back to JDK's built-in Xerces for XML Schema validation.
We can indeed confirm that this setup works, XML messages are parsed and validated. Unfortunately, due to the error handling in WstxSAXParser, the helpful SAXParseException generated by Xerces during XML validation is not thrown, but instead jaxb-runtime throws an AssertionError in UnmarshallingContext.endDocument():
When looking at the relevant code in Woodstox, two things come to mind:
woodstox/src/main/java/com/ctc/wstx/sax/WstxSAXParser.java
Lines 624 to 631 in 4a6d227
| } catch (IOException io) { | |
| throwSaxException(io); | |
| } catch (XMLStreamException strex) { | |
| throwSaxException(strex); | |
| } finally { | |
| if (mContentHandler != null) { | |
| mContentHandler.endDocument(); | |
| } |
- the
SAXParseExceptionfrom schema validation is not handled in thecatch(might be OK?) - in
finally,endDocument()is called, which leads to theAssertionErrorinUnmarshallingContext.endDocument()
If I understand JDK's JavaDoc correctly, it seems to be wrong to call endDocument() in case of an "fatal error", which may explain the diffferent behavior when using the Xerces SAX parser:
Could the .endDocument() call be moved away from the finally without breaking anything else? Looking forward to your opinion!