JDK-8298087 : XML Schema Validation reports an required attribute twice via ErrorHandler
  • Type: Bug
  • Component: xml
  • Sub-Component: javax.xml.validation
  • Affected Version: 17.0.5,19,20
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2022-12-02
  • Updated: 2023-12-04
  • Resolved: 2023-05-02
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 17 JDK 21
17.0.11-oracleFixed 21 b21Fixed
Related Reports
Duplicate :  
Duplicate :  
Description
ADDITIONAL SYSTEM INFORMATION :
Java 17.0.5, Java 11.0.17

A DESCRIPTION OF THE PROBLEM :
XML Schema Validation reports an required attribute twice via `ErrorHandler`. The bug is in `com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaValidator.java:3220` (Java 17.05). With prior a version the validation error is reported only once.

REGRESSION : Last worked in version 17

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run provided source code

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
error is reported once
ACTUAL -
error is reported twice

---------- BEGIN SOURCE ----------
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;

import static java.util.Collections.unmodifiableList;
import static java.util.Locale.ENGLISH;
import static javax.xml.validation.SchemaFactory.newInstance;

public class Main {

    private final static String xsd = """
            <?xml version="1.0" encoding="UTF-8" ?>
            <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="https://agebhar1.github.io/schema/Example"
                    elementFormDefault="qualified">
                        
                <element name="root">
                    <complexType>
                        <sequence>
                            <element name="a">
                                <complexType>
                                    <simpleContent>
                                        <extension base="string">
                                            <attribute name="enabled" type="boolean" use="required"/>
                                        </extension>
                                    </simpleContent>
                                </complexType>
                            </element>
                        </sequence>
                    </complexType>
                </element>
                        
            </schema>
            """;

    private final static String xml = """
            <e:root xmlns:e="https://agebhar1.github.io/schema/Example">
                <e:a>string</e:a>
            </e:root>
            """;

    private static final Function<String, Source> toSource = (String it) -> new StreamSource(new StringReader(it));

    public static void main(String[] args) {
        Locale.setDefault(ENGLISH);
        var ex = validateXMLWithSchema(xsd.transform(toSource), xml.transform(toSource));
        assert ex.size() == 1;
    }

    public static List<SAXParseException> validateXMLWithSchema(final Source xsd, final Source xml) {
        final List<SAXParseException> exceptions = new ArrayList<>();
        try {
            final var factory = newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            final var schema = factory.newSchema(xsd);
            final var validator = schema.newValidator();
            validator.setErrorHandler(new ErrorHandler() {
                @Override
                public void warning(final SAXParseException exception) {
                    System.err.printf("Warning: %s%n", exception);
                    exceptions.add(exception);
                }

                @Override
                public void error(final SAXParseException exception) {
                    System.err.printf("Error: %s%n", exception);
                    exceptions.add(exception);
                }

                @Override
                public void fatalError(final SAXParseException exception) {
                    System.err.printf("Fatal: %s%n", exception);
                    exceptions.add(exception);
                }
            });

            validator.validate(xml);

        } catch (final SAXException | IOException e) {
            System.err.printf("Exception: %s%n", e.getMessage());
        }
        return unmodifiableList(exceptions);
    }

}
---------- END SOURCE ----------

FREQUENCY : always



Comments
Fix request [17u] I backport this for parity with 17.0.11-oracle. Low risk. Simple fix with good test case. Limited to xml schema. Clean backport. Test passes and fails without the fix. SAP nightly testing passed.
30-11-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk17u-dev/pull/1996 Date: 2023-11-29 14:29:03 +0000
29-11-2023

Changeset: 2179a8f2 Author: Joe Wang <joehw@openjdk.org> Date: 2023-05-02 03:12:06 +0000 URL: https://git.openjdk.org/jdk/commit/2179a8f2d622f832aa21eb7f48e8ab055bc55731
02-05-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/13722 Date: 2023-04-28 17:08:10 +0000
28-04-2023

The observations on Windows 10: JDK 11: The reproducer can not run. JDK 17.0.4.1.1: Passed. JDK 17.0.5ea+1: Failed, error is reported twice. JDK 19: Failed. JDK 20ea+23: Failed.
05-12-2022