From 16351944341f23228a314f5f5fb02229e02caaa9 Mon Sep 17 00:00:00 2001 From: Aizal Khan Date: Fri, 20 Nov 2026 00:51:32 +0530 Subject: [PATCH 1/2] resolve fixed QName values against the instance context in Validator --- .../xmlbeans/impl/validator/Validator.java | 26 ++++++- .../misc/checkin/FixedQNameValidateTest.java | 77 +++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/test/java/misc/checkin/FixedQNameValidateTest.java diff --git a/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java b/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java index 502a251a5..61f98d80d 100644 --- a/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java +++ b/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java @@ -1055,10 +1055,30 @@ private String validateSimpleType( return null; } - XmlObject val = type.newValue(value); - XmlObject def = type.newValue(fixedValue); + // Comparing the instance value against the fixed value may have to + // resolve QName or NOTATION prefixes in the instance value, so push + // the namespace context the same way the enumeration comparisons do. + // The fixed value was already resolved against the schema at compile + // time, so compare against that compiled value rather than reparsing + // its lexical form (whose prefixes belong to the schema) against the + // instance here. + boolean valueEquals; - if (!val.valueEquals(def)) { + NamespaceContext.push(new NamespaceContext(event)); + + try { + XmlObject val = type.newValue(value); + XmlObject def = field.getDefaultValue(); + + valueEquals = + val.valueEquals(def == null ? type.newValue(fixedValue) : def); + } catch (XmlValueOutOfRangeException e) { + valueEquals = false; + } finally { + NamespaceContext.pop(); + } + + if (!valueEquals) { // TODO (dutta) - make this more verbose if (field.isAttribute()) { // KHK: check for is cvc-complex-type.3.1 or cvc-au diff --git a/src/test/java/misc/checkin/FixedQNameValidateTest.java b/src/test/java/misc/checkin/FixedQNameValidateTest.java new file mode 100644 index 000000000..665f6d193 --- /dev/null +++ b/src/test/java/misc/checkin/FixedQNameValidateTest.java @@ -0,0 +1,77 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package misc.checkin; + +import org.apache.xmlbeans.SchemaTypeLoader; +import org.apache.xmlbeans.XmlBeans; +import org.apache.xmlbeans.XmlObject; +import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class FixedQNameValidateTest { + + private static final String XSD = + "" + + " " + + " " + + " " + + " " + + ""; + + private static SchemaTypeLoader loader; + + @BeforeAll + static void compileSchema() throws Exception { + loader = XmlBeans.loadXsd(SchemaDocument.Factory.parse(XSD)); + } + + private static boolean validates(String instance) throws Exception { + return loader.parse(instance, null, null).validate(); + } + + @Test + void fixedQNameElementValidates() throws Exception { + // used to throw XmlValueOutOfRangeException out of validate(): the fixed + // value comparison called newValue() with no namespace context pushed + assertTrue(validates("p:foo")); + } + + @Test + void fixedQNameAttributeValidates() throws Exception { + assertTrue(validates("")); + } + + @Test + void mismatchedFixedQNameIsReported() throws Exception { + // a wrong value is reported as invalid, not raised as an exception + assertFalse(validates("p:bar")); + assertFalse(validates("")); + } + + @Test + void fixedQNameComparedByValueNotPrefix() throws Exception { + // same namespace reached through a different instance prefix still matches + assertTrue(validates("z:foo")); + // rebinding the prefix to another namespace does not match the fixed value + assertFalse(validates("p:foo")); + } +} From 39b155873441a3b306263296fbd5edd2d42994d8 Mon Sep 17 00:00:00 2001 From: Aizal Khan Date: Sat, 21 Nov 2026 12:44:24 +0530 Subject: [PATCH 2/2] skip the lexical fixed value check when the cooked value is available The lexical check resolved the schema's prefix against the instance, so an instance that reached the fixed namespace through its own prefix was still rejected. It is only needed for the fallback that reparses the lexical form. Covers the undeclared schema prefix and an xsi:type narrowed instance type. --- .../xmlbeans/impl/validator/Validator.java | 20 ++++++++++++----- .../misc/checkin/FixedQNameValidateTest.java | 22 ++++++++++++++++--- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java b/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java index 61f98d80d..e9c37f448 100644 --- a/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java +++ b/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java @@ -1051,24 +1051,32 @@ private String validateSimpleType( XmlWhitespace.collapse( field.getDefaultText(), type.getWhiteSpaceRule()); - if (!validateSimpleType(type, fixedValue, event)) { + // The fixed value was already resolved against the schema at compile + // time, so compare against that cooked value. Reparsing its lexical + // form here would resolve the schema's prefixes against the instance, + // which is also why only the fallback that has to reparse it needs the + // lexical check. + XmlObject def; + + try { + def = field.getDefaultValue(); + } catch (XmlValueOutOfRangeException e) { + def = null; + } + + if (def == null && !validateSimpleType(type, fixedValue, event)) { return null; } // Comparing the instance value against the fixed value may have to // resolve QName or NOTATION prefixes in the instance value, so push // the namespace context the same way the enumeration comparisons do. - // The fixed value was already resolved against the schema at compile - // time, so compare against that compiled value rather than reparsing - // its lexical form (whose prefixes belong to the schema) against the - // instance here. boolean valueEquals; NamespaceContext.push(new NamespaceContext(event)); try { XmlObject val = type.newValue(value); - XmlObject def = field.getDefaultValue(); valueEquals = val.valueEquals(def == null ? type.newValue(fixedValue) : def); diff --git a/src/test/java/misc/checkin/FixedQNameValidateTest.java b/src/test/java/misc/checkin/FixedQNameValidateTest.java index 665f6d193..bcf76ec6c 100644 --- a/src/test/java/misc/checkin/FixedQNameValidateTest.java +++ b/src/test/java/misc/checkin/FixedQNameValidateTest.java @@ -18,7 +18,6 @@ import org.apache.xmlbeans.SchemaTypeLoader; import org.apache.xmlbeans.XmlBeans; -import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -28,9 +27,12 @@ public class FixedQNameValidateTest { + private static final String XSI = "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"; + private static final String XSD = "" + + " " + " " + " " + " " + @@ -69,9 +71,23 @@ void mismatchedFixedQNameIsReported() throws Exception { @Test void fixedQNameComparedByValueNotPrefix() throws Exception { - // same namespace reached through a different instance prefix still matches - assertTrue(validates("z:foo")); + // the schema's own prefix is never looked up in the instance: the fixed + // namespace reached through a different instance prefix still matches, + // with 'p' undeclared on the instance + assertTrue(validates("z:foo")); + assertTrue(validates("")); // rebinding the prefix to another namespace does not match the fixed value assertFalse(validates("p:foo")); } + + @Test + void fixedQNameComparedAcrossXsiType() throws Exception { + // xsi:type narrows the instance type, so the two sides of the comparison + // come from different types: the compiled fixed value keeps the field's + // declared xs:QName while the instance value is built from t:qname + assertTrue(validates( + "z:foo")); + assertFalse(validates( + "z:bar")); + } }