Skip to content

resolve fixed QName values against the instance context in Validator - #171

Open
aizu-m wants to merge 2 commits into
apache:trunkfrom
aizu-m:fixed-qname-validate-context
Open

aizu-m wants to merge 2 commits into
apache:trunkfrom
aizu-m:fixed-qname-validate-context

Conversation

@aizu-m

@aizu-m aizu-m commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

XmlObject.validate() on a document with a fixed xsd:QName value throws instead of reporting the error through the listener:

org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException: Invalid QName value: Can't resolve prefix 'p'
    at org.apache.xmlbeans.impl.values.JavaQNameHolder.parse(JavaQNameHolder.java:135)
    at org.apache.xmlbeans.impl.values.XmlObjectBase.setStringValue(XmlObjectBase.java:1773)
    at org.apache.xmlbeans.impl.schema.SchemaTypeImpl.newValue(SchemaTypeImpl.java:1873)
    at org.apache.xmlbeans.impl.validator.Validator.validateSimpleType(Validator.java:1058)
    ...
    at org.apache.xmlbeans.impl.store.Xobj.validate(Xobj.java:1787)

Found while diffing validate() against the JDK Xerces validator over fixed and default value constraints.

Observations:

  • The fixed-value branch of validateSimpleType builds type.newValue(value) and type.newValue(fixedValue) with no NamespaceContext pushed.
  • For an xsd:QName or xsd:NOTATION fixed value (also a list or union containing one) the prefix cannot be resolved, so newValue raises XmlValueOutOfRangeException, a RuntimeException, out of validate(). Reachable from any untrusted instance whose schema puts fixed on a QName typed element or attribute.
  • The enumeration comparisons a few lines down already push new NamespaceContext(event) and catch XmlValueOutOfRangeException. The fixed-value comparison was the one that did not.
  • The fixed value was resolved against the schema at compile time; field.getDefaultValue() already holds the cooked QName. Reparsing its lexical form in the instance context is wrong anyway, as it resolves the schema prefix against the instance.

Conclusion: push the namespace context for the instance value and compare against the compiled fixed value. A wrong value is now reported as the usual cvc-elt or cvc-au fixed error rather than thrown, a value that reaches the fixed namespace through a different instance prefix still matches, and a prefix rebound to another namespace does not. Non-QName fixed values behave as before.

FixedQNameValidateTest throws on trunk and passes with the change. ./gradlew test is green (3333 tests).

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, comparing against the compiled fixed value is the right approach. Two points:

1. The fixed value's prefix is still resolved against the instance before the comparison

Just above the changed block, Validator.java:1054 still runs

if (!validateSimpleType(type, fixedValue, event)) {
    return null;
}

That checks the lexical fixed value, with the schema's prefix in it, against the instance's namespace declarations. So an instance that reaches the fixed namespace through its own prefix, and never declares p, is still rejected with a "can't resolve prefix 'p'" error:

<!-- schema: xmlns:p='urn:p', fixed='p:foo' -->
<t:qn xmlns:t='urn:t' xmlns:z='urn:p'>z:foo</t:qn>

fixedQNameComparedByValueNotPrefix passes only because it also declares xmlns:p='urn:p' on the instance, which hides this.

Suggestion: when field.getDefaultValue() is non-null, skip the lexical check of fixedValue, since it was already validated at compile time. Keep the check only for the fallback that reparses fixedValue. Please also add the case above, with p undeclared, to the test.

2. Minor: the two sides of the comparison may now have different types

The compiled value has the field's declared type, while val is built from type, which can be a different type set via xsi:type. Before, both sides came from type.newValue(...). This is probably fine, since valueEquals compares by primitive type, but a test with xsi:type on a fixed element would confirm it.

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.
@aizu-m

aizu-m commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Both done.

1. Confirmed. With the comparison fixed but the lexical check left in, <t:qn xmlns:t='urn:t' xmlns:z='urn:p'>z:foo</t:qn> still failed on "can't resolve prefix 'p'", and the spare xmlns:p on the old test was indeed hiding it. The check now only guards the reparsing fallback:

if (def == null && !validateSimpleType(type, fixedValue, event)) {
    return null;
}

getDefaultValue() gets its own catch, because the particle path that has no cooked value reparses _defaultText itself and can throw the same XmlValueOutOfRangeException for a QName. The undeclared p case is in fixedQNameComparedByValueNotPrefix now, for the element and the attribute.

2. Added fixedQNameComparedAcrossXsiType: a t:qname restriction of xs:QName selected through xsi:type, so val is built from the derived type while def keeps the field's declared xs:QName. It matches on value and still reports z:bar, so valueEquals is comparing by primitive type as you thought.

Both new cases fail on the previous commit. ./gradlew test is green, 3334 tests.

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants