fix(schema): extend the xsi:type not-derived check to atomic substitutes; invalid-instances-accepted 66 to 60 (#181, #147)
cvc-elt.4.3.2.1 requires an xsi:type to name a type derived from the element's declared type. The not-derived check covered only complex substitutes, so an xsi:type naming an atomic type unrelated to the declared atomic type, such as a sibling restriction or the declared type's own ancestor, was accepted. notDerivedSubstitutionError now admits atomic simple substitutes too (via isAtomicOrComplex, which excludes list/union, handled separately). It still fires only when both the substitute and the declared type are recorded in the namespaced derivation backbone and derivationMethods finds no path between them. Built-in types and the ur-types are absent from the backbone, so a built-in substitute or declared type leaves the check silent: a valid built-in derivation such as xsi:type="xs:int" on an xs:decimal element is never flagged, which was the source of an earlier false-positive regression. A genuine user restriction chain stays inside the backbone the whole way down, so a validly derived substitute is accepted. invalid-instances-accepted 66 to 60, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved. An adversarial review probed built-in derivation in both directions, deep and cross-namespace restriction chains, and sibling/ancestor substitutes, and found no over-rejection.
This commit is contained in:
parent
c80659d80d
commit
31a4ebacfb
@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- XSTS invalid-instances-accepted (#181, #147): the `xsi:type` not-derived check (`cvc-elt.4.3.2.1`) now covers ATOMIC simple substitutes, not only complex ones. An `xsi:type` naming an atomic type that is not derived from the element's declared type, such as a sibling restriction or the declared type's own ancestor, is rejected. The check still fires only when both the substitute and the declared type are recorded in the namespaced derivation backbone with no path between them, so a built-in substitute or declared type (absent from the backbone) and the ur-types stay silent (under-reject), avoiding any built-in-derivation false positive; a genuinely derived restriction substitute is accepted. invalid-instances-accepted 66 → 60, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved.
|
||||
|
||||
- XSTS invalid-instances-accepted (#181, #147): an `xsi:type` naming a list or union simple type is now rejected unless the element's declared type is a ur-type (`anySimpleType`/`anyType`). A list or union type derives only from `anySimpleType` (XSD Part 2), so it cannot validly substitute for an element of any more specific atomic or complex type (`cvc-elt.4.3.2.1`); naming one on, say, an `xs:int`-declared element is invalid. A genuinely derived (restriction) substitute, a list/union under an `anySimpleType` declaration, and a declared type that is itself a list or union are all left alone, so no valid instance is newly rejected. The declared type is resolved through the element-ref / type-reference chain, so the check also fires for an element reached via `<xs:element ref>` whose global declaration is atomic. This widening became safe once the derivation backbone was keyed by namespaced identity (#186); the conservative atomic/built-in not-derived cases remain under-rejected pending the full type-identity relation. invalid-instances-accepted 81 → 66, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0), no other bucket moved.
|
||||
|
||||
- XSTS invalid-instances-accepted (#186, #147): the instance-validity derivation and substitution tables are now keyed by namespaced identity (`{ns}local`) instead of bare local name, so two same-local-name types or elements in different namespaces no longer collide. `DerivationTables` keeps its bare-keyed fields for the schema-validity machinery (Particle-Valid-Restriction and the consistency rules) and gains parallel namespaced fields built in the same pass; only the instance consumers (the `xsi:type` blocked/not-derived/abstract checks, the substitution-group closure, and `referenceParticle`) read the namespaced tables, reducing a `type:{ns}local` reference, a bare name, or an element ref to a `{ns}local` key. A key miss stays permissive (no constraint), so the re-keying cannot over-reject; the single-document corpus behaviour is unchanged. With the namespaced backbone in place, a schema's `blockDefault` now supplies the `{disallowed substitutions}` (`block`) of every element and complex type that states none (own `block` overrides; `block=""` clears; intersected with the methods each component admits), without one namespace's defaults shadowing another's. invalid-instances-accepted 89 → 81, no false positive (valid-instances-rejected and valid-schemas-rejected held at 0; an adversarial review exercised same- and cross-namespace substitutions, no-targetNamespace keys, transitive substitution groups, and blockDefault override cases), no other bucket moved.
|
||||
|
||||
@ -87,6 +87,17 @@ extension PureXML.Schema.ComplexValidator {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a substitute type participates in the not-derived check: a complex
|
||||
/// type, or an ATOMIC simple type. A list or union substitute is excluded here
|
||||
/// (it is handled by ``listUnionSubstitutionError``).
|
||||
static func isAtomicOrComplex(_ type: PureXML.Schema.ElementType) -> Bool {
|
||||
switch type {
|
||||
case .complex: true
|
||||
case let .simple(simple): !isListOrUnion(simple)
|
||||
case .typeReference: false
|
||||
}
|
||||
}
|
||||
|
||||
/// The error when an element's declared type is an abstract complex type and
|
||||
/// the element supplies no `xsi:type` to name a concrete derived type. An
|
||||
/// abstract type cannot itself be the type of an instance element.
|
||||
@ -141,13 +152,14 @@ extension PureXML.Schema.ComplexValidator {
|
||||
}
|
||||
|
||||
/// cvc-elt.4.3.2.1: an `xsi:type` must resolve to a type DERIVED from the
|
||||
/// element's declared type. This flags ONLY the case where both the declared
|
||||
/// type and the substitute resolve to COMPLEX types recorded in the derivation
|
||||
/// backbone and there is no derivation path between them (a different branch of
|
||||
/// the hierarchy). It deliberately does NOT touch a simple/list/union substitute,
|
||||
/// a substitute or declared type absent from the backbone, or the ur-types: there
|
||||
/// the relationship cannot be confirmed from the complex-type backbone alone, so
|
||||
/// the rule stays silent (under-reject) rather than risk a false positive.
|
||||
/// element's declared type. This flags the case where both the declared type and
|
||||
/// the substitute resolve to complex or atomic-simple types recorded in the
|
||||
/// derivation backbone and there is no derivation path between them (a different
|
||||
/// branch of the hierarchy, including a substitute that is the declared type's
|
||||
/// own ancestor). A list or union substitute is handled separately by
|
||||
/// ``listUnionSubstitutionError``; a substitute or declared type ABSENT from the
|
||||
/// backbone (a built-in, or the ur-types) leaves the relationship unconfirmable,
|
||||
/// so the rule stays silent (under-reject) rather than risk a false positive.
|
||||
func notDerivedSubstitutionError(
|
||||
declared: PureXML.Schema.ElementType,
|
||||
child: PureXML.Model.Element,
|
||||
@ -157,7 +169,8 @@ extension PureXML.Schema.ComplexValidator {
|
||||
guard case let .typeReference(reference) = declared,
|
||||
let substituteLabel = Self.xsiTypeName(child),
|
||||
let substituteRef = Self.xsiTypeReference(child, namespaceBindings: namespaceBindings),
|
||||
case .complex? = Self.resolveNamedType(substituteRef, in: types)
|
||||
let substituteType = Self.resolveNamedType(substituteRef, in: types),
|
||||
Self.isAtomicOrComplex(substituteType)
|
||||
else { return nil }
|
||||
let substituteKey = Self.derivationKey(fromReference: substituteRef)
|
||||
let declaredName = resolvedDeclaredTypeName(reference)
|
||||
|
||||
@ -58,4 +58,29 @@ struct SchemaListUnionXsiTypeTests {
|
||||
let xml = #"<root><leaf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="myList">1 2</leaf></root>"#
|
||||
#expect(try !schema.validate(xml).isEmpty)
|
||||
}
|
||||
|
||||
/// The not-derived check also covers ATOMIC substitutes recorded in the
|
||||
/// backbone: an `xsi:type` naming a sibling type, or the declared type's own
|
||||
/// ancestor, is rejected; a genuinely derived restriction is accepted.
|
||||
@Test("an atomic xsi:type not derived from the declared type is rejected")
|
||||
func test_atomicNotDerivedRejected() throws {
|
||||
let schema = try PureXML.Schema.Document("""
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||
<xs:element name="e" type="A"/>
|
||||
<xs:simpleType name="Base"><xs:restriction base="xs:int"/></xs:simpleType>
|
||||
<xs:simpleType name="A"><xs:restriction base="Base"><xs:minInclusive value="0"/></xs:restriction></xs:simpleType>
|
||||
<xs:simpleType name="Sibling"><xs:restriction base="Base"><xs:maxInclusive value="9"/></xs:restriction></xs:simpleType>
|
||||
<xs:simpleType name="Derived"><xs:restriction base="A"><xs:maxInclusive value="9"/></xs:restriction></xs:simpleType>
|
||||
</xs:schema>
|
||||
""")
|
||||
func doc(_ type: String) -> String {
|
||||
#"<e xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="\#(type)">5</e>"#
|
||||
}
|
||||
// A sibling (also restricts Base) is not derived from A: rejected.
|
||||
#expect(try !schema.validate(doc("Sibling")).isEmpty)
|
||||
// The declared type's own ancestor Base is not derived from A: rejected.
|
||||
#expect(try !schema.validate(doc("Base")).isEmpty)
|
||||
// A genuine restriction of A is validly derived: accepted.
|
||||
#expect(try schema.validate(doc("Derived")).isEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
@ -150,13 +150,13 @@ struct XSTSSuiteTests {
|
||||
private let knownSchemaValidRejected = 0
|
||||
private let knownSchemaInvalidAccepted = 69
|
||||
private let knownInstanceValidRejected = 0
|
||||
/// Instance bucket: xsi:type must derive from the declared type (133 -> 107); block="substitution"
|
||||
/// bars members even for an untyped head (107 -> 101); ur-type `xsd:anyType` wildcards are `lax`
|
||||
/// (101 -> 95); identity fields compare in the decimal value space (95 -> 94); attribute wildcard
|
||||
/// is the INTERSECTION of its sources (94 -> 89); `blockDefault` supplies a missing `block` after
|
||||
/// re-keying derivation by namespace (89 -> 81); a list/union xsi:type is not derived from a
|
||||
/// non-ur declared type, resolved through an element-ref chain (81 -> 66).
|
||||
private let knownInstanceInvalidAccepted = 66
|
||||
/// Instance bucket (133 -> 60), per-step deltas in CHANGELOG.md: xsi:type must derive from the
|
||||
/// declared type (complex, then atomic and list/union, resolved through the element-ref chain);
|
||||
/// block/blockDefault (incl. "substitution" and an untyped head) bar substitutions after the
|
||||
/// derivation backbone was re-keyed by namespace; ur-type `xsd:anyType` wildcards are `lax`;
|
||||
/// identity fields compare in the decimal value space; a complex type's attribute wildcard is the
|
||||
/// INTERSECTION of its `anyAttribute` sources.
|
||||
private let knownInstanceInvalidAccepted = 60
|
||||
|
||||
@Test("Every XSTS case behaves: compile, reject, validate, invalidate")
|
||||
func test_suite() throws {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user