You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2026/10/23 13:27:39 UTC
(poi-xmlbeans) branch trunk updated: resolve defaulted QName identity fields against the schema (#143)
This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git
The following commit(s) were added to refs/heads/trunk by this push:
new 85e8a7d1 resolve defaulted QName identity fields against the schema (#143)
85e8a7d1 is described below
commit 85e8a7d187bee6c6ddee381c7849c94b73d2a468
Author: PJ Fanning <pj...@users.noreply.github.com>
AuthorDate: Fri Oct 23 14:27:33 2026 +0100
resolve defaulted QName identity fields against the schema (#143)
A defaulted or fixed attribute picked by an identity constraint field
was built from its default text using the instance's namespaces, so a
QName default either failed to resolve or resolved to the wrong
namespace. Use SchemaLocalAttribute.getDefaultValue(), which resolves
against the schema.
Extend QNameIdentityConstraintValidateTest to cover element-text
fields, default-namespace and unprefixed QNames, and defaulted QName
attributes, each through both XmlObject.validate() and
ValidatingXMLStreamReader.
Co-authored-by: Claude Opus 5.5 <no...@anthropic.com>
---
.../xmlbeans/impl/common/IdentityConstraint.java | 35 +++++-
.../apache/xmlbeans/impl/validator/Validator.java | 2 +-
.../QNameIdentityConstraintValidateTest.java | 119 +++++++++++++++++----
3 files changed, 131 insertions(+), 25 deletions(-)
diff --git a/src/main/java/org/apache/xmlbeans/impl/common/IdentityConstraint.java b/src/main/java/org/apache/xmlbeans/impl/common/IdentityConstraint.java
index 456408e3..0e456138 100644
--- a/src/main/java/org/apache/xmlbeans/impl/common/IdentityConstraint.java
+++ b/src/main/java/org/apache/xmlbeans/impl/common/IdentityConstraint.java
@@ -24,6 +24,7 @@ import org.apache.xmlbeans.impl.xpath.XPathExecutionContext;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import java.util.*;
+import java.util.function.Supplier;
/**
* Identity constraint engine. Performs streaming validation of identity constraints.
@@ -84,6 +85,15 @@ public class IdentityConstraint {
}
}
+ /**
+ * An attribute absent from the instance but given a default or fixed value by the schema.
+ */
+ public void defaultAttr(Event e, SchemaLocalAttribute sla) {
+ for (ConstraintState cs = _constraintStack; cs != null; cs = cs._next) {
+ cs.defaultAttr(e, sla);
+ }
+ }
+
public void text(Event e, SchemaType st, String value, boolean emptyContent) {
for (ConstraintState cs = _constraintStack; cs != null; cs = cs._next) {
cs.text(e, st, value, emptyContent);
@@ -199,6 +209,15 @@ public class IdentityConstraint {
}
}
+ private static XmlObject defaultValue(SchemaLocalAttribute sla) {
+ try {
+ return sla.getDefaultValue();
+ } catch (IllegalArgumentException e) {
+ // Ignore these exceptions, as in newValue
+ return null;
+ }
+ }
+
private static XmlObject newValue(SchemaType st, String value, Event event) {
// QName and NOTATION values need the in-scope namespaces of the instance
NamespaceContext.push(new NamespaceContext(event));
@@ -244,6 +263,10 @@ public class IdentityConstraint {
abstract void attr(Event e, QName name, SchemaType st, String value);
+ void defaultAttr(Event e, SchemaLocalAttribute sla) {
+ attr(e, sla.getName(), sla.getType(), sla.getDefaultText());
+ }
+
abstract void text(Event e, SchemaType st, String value, boolean emptyContent);
abstract void remove(Event e);
@@ -439,9 +462,19 @@ public class IdentityConstraint {
return;
}
+ attr(e, name, () -> newValue(st, value, e));
+ }
+
+ @Override
+ void defaultAttr(Event e, SchemaLocalAttribute sla) {
+ // The default is resolved against the schema, so QName prefixes need not be bound in the instance
+ attr(e, sla.getName(), () -> defaultValue(sla));
+ }
+
+ private void attr(Event e, QName name, Supplier<XmlObject> value) {
for (int i = 0; i < _contexts.length; i++) {
if (_contexts[i].attr(name)) {
- XmlObject o = newValue(st, value, e);
+ XmlObject o = value.get();
// Ignore invalid values. Assume that validation catches these
if (o == null) {
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 5ffeada1..0f7fc97b 100644
--- a/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java
+++ b/src/main/java/org/apache/xmlbeans/impl/validator/Validator.java
@@ -611,7 +611,7 @@ public final class Validator
new Object[]{QNameHelper.pretty(sla.getName())},
sla.getName(), null, null, XmlValidationError.INCORRECT_ATTRIBUTE, state._type);
} else if (sla.isDefault() || sla.isFixed()) {
- _constraintEngine.attr(event, sla.getName(), sla.getType(), sla.getDefaultText());
+ _constraintEngine.defaultAttr(event, sla);
// We don't need to validate attribute defaults because this is done at compiletime.
/*
diff --git a/src/test/java/misc/checkin/QNameIdentityConstraintValidateTest.java b/src/test/java/misc/checkin/QNameIdentityConstraintValidateTest.java
index c419cb74..7c74d156 100644
--- a/src/test/java/misc/checkin/QNameIdentityConstraintValidateTest.java
+++ b/src/test/java/misc/checkin/QNameIdentityConstraintValidateTest.java
@@ -20,26 +20,38 @@ import org.apache.xmlbeans.XmlBeans;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
+import org.apache.xmlbeans.impl.validator.ValidatingXMLStreamReader;
import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+import javax.xml.stream.XMLInputFactory;
+import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class QNameIdentityConstraintValidateTest {
+ enum Mode { XML_OBJECT, STREAM }
+
+ // xmlns:s is bound only in the schema, and xmlns:p means different things in the schema and the instance
private static String schema(String constraint) {
return
"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' " +
- "xmlns:t='urn:t' targetNamespace='urn:t' elementFormDefault='qualified'>" +
+ "xmlns:t='urn:t' xmlns:s='urn:p' xmlns:p='urn:s' targetNamespace='urn:t' elementFormDefault='qualified'>" +
" <xs:complexType name='named'><xs:attribute name='n' type='xs:QName' use='required'/></xs:complexType>" +
+ " <xs:complexType name='defaultS'><xs:attribute name='n' type='xs:QName' default='s:a'/></xs:complexType>" +
+ " <xs:complexType name='defaultP'><xs:attribute name='n' type='xs:QName' default='p:a'/></xs:complexType>" +
+ " <xs:complexType name='wrapped'><xs:sequence><xs:element name='v' type='xs:QName'/></xs:sequence></xs:complexType>" +
" <xs:element name='root'>" +
" <xs:complexType><xs:sequence>" +
" <xs:element name='k' type='t:named' minOccurs='0' maxOccurs='unbounded'/>" +
+ " <xs:element name='ds' type='t:defaultS' minOccurs='0' maxOccurs='unbounded'/>" +
+ " <xs:element name='dp' type='t:defaultP' minOccurs='0' maxOccurs='unbounded'/>" +
+ " <xs:element name='w' type='t:wrapped' minOccurs='0' maxOccurs='unbounded'/>" +
" <xs:element name='ref' type='t:named' minOccurs='0' maxOccurs='unbounded'/>" +
" </xs:sequence></xs:complexType>" +
constraint +
@@ -48,38 +60,99 @@ public class QNameIdentityConstraintValidateTest {
"</xs:schema>";
}
- private static final String KEY =
- "<xs:key name='kk'><xs:selector xpath='t:k'/><xs:field xpath='@n'/></xs:key>";
+ private static String constraint(String kind, String selector, String field) {
+ return "<xs:" + kind + " name='kk'><xs:selector xpath='" + selector + "'/><xs:field xpath='" + field + "'/></xs:" + kind + ">";
+ }
+
+ private static final String KEY = constraint("key", "t:k", "@n");
- private static final String UNIQUE =
- "<xs:unique name='kk'><xs:selector xpath='t:k'/><xs:field xpath='@n'/></xs:unique>";
+ private static final String UNIQUE = constraint("unique", "t:k", "@n");
- private static List<XmlError> validate(String xsd, String body) throws Exception {
+ private static final String UNIQUE_DEFAULTED = constraint("unique", "t:k|t:ds|t:dp", "@n");
+
+ private static final String UNIQUE_ELEMENT = constraint("unique", "t:w", "t:v");
+
+ private static List<XmlError> validate(Mode mode, String xsd, String body) throws Exception {
SchemaTypeLoader loader = XmlBeans.loadXsd(new XmlObject[]{SchemaDocument.Factory.parse(xsd)});
- XmlObject doc = loader.parse("<t:root xmlns:t='urn:t' xmlns:p='urn:p'>" + body + "</t:root>", null, null);
+ String instance = "<t:root xmlns:t='urn:t' xmlns:p='urn:p'>" + body + "</t:root>";
List<XmlError> errors = new ArrayList<>();
- boolean valid = doc.validate(new XmlOptions().setErrorListener(errors));
+ boolean valid;
+ if (mode == Mode.XML_OBJECT) {
+ XmlObject doc = loader.parse(instance, null, null);
+ valid = doc.validate(new XmlOptions().setErrorListener(errors));
+ } else {
+ ValidatingXMLStreamReader valXsr = new ValidatingXMLStreamReader();
+ valXsr.init(XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(instance)),
+ true, null, loader, null, errors);
+ while (valXsr.hasNext()) {
+ valXsr.next();
+ }
+ valid = valXsr.isValid();
+ }
assertEquals(errors.isEmpty(), valid, errors.toString());
return errors;
}
- @Test
- void prefixedQNameKeyIsFilled() throws Exception {
- List<XmlError> errors = validate(schema(KEY), "<t:k n='p:a'/><t:ref n='p:a'/>");
+ private static void assertSingleError(String code, List<XmlError> errors) {
+ assertEquals(1, errors.size(), errors.toString());
+ assertEquals(code, errors.get(0).getErrorCode());
+ }
+
+ @ParameterizedTest
+ @EnumSource(Mode.class)
+ void prefixedQNameKeyIsFilled(Mode mode) throws Exception {
+ List<XmlError> errors = validate(mode, schema(KEY), "<t:k n='p:a'/><t:ref n='p:a'/>");
assertTrue(errors.isEmpty(), errors.toString());
}
- @Test
- void prefixedQNameDuplicateUniqueIsReported() throws Exception {
- List<XmlError> errors = validate(schema(UNIQUE), "<t:k n='p:a'/><t:k n='p:a'/>");
- assertEquals(1, errors.size(), errors.toString());
- assertEquals("cvc-identity-constraint.4.1", errors.get(0).getErrorCode());
+ @ParameterizedTest
+ @EnumSource(Mode.class)
+ void prefixedQNameDuplicateUniqueIsReported(Mode mode) throws Exception {
+ List<XmlError> errors = validate(mode, schema(UNIQUE), "<t:k n='p:a'/><t:k n='p:a'/>");
+ assertSingleError("cvc-identity-constraint.4.1", errors);
+ }
+
+ @ParameterizedTest
+ @EnumSource(Mode.class)
+ void prefixedQNameDanglingKeyrefIsReported(Mode mode) throws Exception {
+ List<XmlError> errors = validate(mode, schema(UNIQUE), "<t:k n='p:a'/><t:ref n='p:zzz'/>");
+ assertSingleError("cvc-identity-constraint.4.3", errors);
}
- @Test
- void prefixedQNameDanglingKeyrefIsReported() throws Exception {
- List<XmlError> errors = validate(schema(UNIQUE), "<t:k n='p:a'/><t:ref n='p:zzz'/>");
- assertFalse(errors.isEmpty());
- assertEquals("cvc-identity-constraint.4.3", errors.get(0).getErrorCode());
+ @ParameterizedTest
+ @EnumSource(Mode.class)
+ void defaultNamespaceQNameMatchesPrefixed(Mode mode) throws Exception {
+ List<XmlError> errors = validate(mode, schema(UNIQUE), "<t:k xmlns='urn:p' n='a'/><t:k n='p:a'/>");
+ assertSingleError("cvc-identity-constraint.4.1", errors);
+ }
+
+ @ParameterizedTest
+ @EnumSource(Mode.class)
+ void unprefixedQNameWithoutDefaultNamespaceIsDistinct(Mode mode) throws Exception {
+ List<XmlError> errors = validate(mode, schema(UNIQUE), "<t:k n='a'/><t:k n='p:a'/>");
+ assertTrue(errors.isEmpty(), errors.toString());
+ }
+
+ @ParameterizedTest
+ @EnumSource(Mode.class)
+ void prefixedQNameElementDuplicateUniqueIsReported(Mode mode) throws Exception {
+ List<XmlError> errors = validate(mode, schema(UNIQUE_ELEMENT), "<t:w><t:v>p:a</t:v></t:w><t:w><t:v>p:a</t:v></t:w>");
+ assertSingleError("cvc-identity-constraint.4.1", errors);
+ }
+
+ @ParameterizedTest
+ @EnumSource(Mode.class)
+ void defaultedQNameUsesSchemaPrefixUnboundInInstance(Mode mode) throws Exception {
+ // the default s:a is {urn:p}a, the same as p:a in the instance
+ List<XmlError> errors = validate(mode, schema(UNIQUE_DEFAULTED), "<t:k n='p:a'/><t:ds/>");
+ assertSingleError("cvc-identity-constraint.4.1", errors);
+ }
+
+ @ParameterizedTest
+ @EnumSource(Mode.class)
+ void defaultedQNameUsesSchemaPrefixRedefinedInInstance(Mode mode) throws Exception {
+ // the default p:a is {urn:s}a, not the instance's p:a
+ List<XmlError> errors = validate(mode, schema(UNIQUE_DEFAULTED), "<t:k n='p:a'/><t:dp/>");
+ assertTrue(errors.isEmpty(), errors.toString());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org