PureXML/Tests/SchemaSubstitutionBlockTests.swift
Mihaela Mihaljevic 8e873dc929 fix(schema): an xsi:type must be derived from the declared type (cvc-elt.4.3.2.1); invalid-instances-accepted 133 to 107 (#181)
First instance-bucket reduction of the campaign. An instance xsi:type naming a
complex type must be derived from the element's declared type. notDerivedSubstitutionError
flags a complex substitute on a different branch of the hierarchy; previously
only a derived-but-blocked substitution was caught (blockedSubstitutionError
returns nil when there is no derivation path), so a non-derived xsi:type was
silently accepted (the test006 cluster, 32 instances, and more).

Conservatively scoped after a broader first cut took 20 false positives (now
reverted): it fires only when the substitute resolves to a COMPLEX type and both
substitute and declared names are in the derivation backbone with no path between
them. Simple/list/union substitutes, ur-types, and unresolvable references are
left alone -- under-reject, never over-reject. An adversarial review confirmed
the conservative guards eliminate the prior false positives; the one residual is
the pre-existing bare-local-name keying of the derivation backbone (shared with
blockedSubstitutionError), disclosed in the code, corpus-absent.

The three xsi:type override checks (blocked / not-derived / abstract) are folded
into one xsiTypeOverrideError helper to keep validateChild under the complexity limit.

XSTS: invalid-instances-accepted 133 to 107 (26 caught), valid-instances-rejected
and valid-schemas-rejected both held at 0, no other bucket moved.
2026-06-18 10:07:05 +02:00

67 lines
3.5 KiB
Swift

@testable import PureXML
import Testing
/// `block="substitution"` on a substitution-group head forbids any member from
/// standing in for it, regardless of the member's type derivation (#147, XSTS
/// disallowedSubst set). Only type-derivation blocks (`extension`/`restriction`)
/// had been applied, so a same-type member still substituted under a
/// substitution block.
@Suite("Substitution-group block")
struct SchemaSubstitutionBlockTests {
private func schema(headBlock: String) throws -> PureXML.Schema.Document {
let attribute = headBlock.isEmpty ? "" : " block=\"\(headBlock)\""
return try PureXML.Schema.Document("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="urn:s" targetNamespace="urn:s" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType><xs:sequence>
<xs:element ref="Head" maxOccurs="unbounded"/>
</xs:sequence></xs:complexType>
</xs:element>
<xs:element name="Head" type="T"\(attribute)/>
<xs:complexType name="T"><xs:sequence><xs:element name="x" type="xs:string"/></xs:sequence></xs:complexType>
<xs:element name="Member" type="T" substitutionGroup="Head"/>
</xs:schema>
""")
}
private let withMember = #"<root xmlns="urn:s"><Head><x>a</x></Head><Member><x>b</x></Member></root>"#
private let headsOnly = #"<root xmlns="urn:s"><Head><x>a</x></Head><Head><x>b</x></Head></root>"#
@Test("block='substitution' on the head rejects a substituting member")
func test_substitutionBlocked() throws {
#expect(try !schema(headBlock: "substitution").validate(withMember).isEmpty)
// Plain heads are still fine.
#expect(try schema(headBlock: "substitution").validate(headsOnly).isEmpty)
}
@Test("without a substitution block the member may substitute")
func test_memberAllowedWithoutBlock() throws {
#expect(try schema(headBlock: "").validate(withMember).isEmpty)
}
/// cvc-elt.4.3.2.1: an xsi:type naming a complex type must be derived from the
/// element's declared type. A complex type on a different branch of the
/// hierarchy is rejected; the declared type itself is valid.
@Test("an xsi:type must be derived from the element's declared type")
func test_xsiTypeMustDeriveFromDeclared() throws {
let schema = try PureXML.Schema.Document("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:s" targetNamespace="urn:s" elementFormDefault="qualified">
<xs:complexType name="B"><xs:sequence><xs:element name="f" type="xs:string"/></xs:sequence></xs:complexType>
<xs:complexType name="De"><xs:complexContent><xs:extension base="B"/></xs:complexContent></xs:complexType>
<xs:complexType name="Dr"><xs:complexContent><xs:restriction base="B">
<xs:sequence><xs:element name="f" type="xs:string"/></xs:sequence>
</xs:restriction></xs:complexContent></xs:complexType>
<xs:element name="e" type="De"/>
</xs:schema>
""")
func doc(_ type: String) -> String {
#"<e xmlns="urn:s" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="\#(type)"><f>x</f></e>"#
}
// Dr (restriction of B) is on a different branch, not derived from De: invalid.
#expect(try !schema.validate(doc("Dr")).isEmpty)
// The declared type itself is valid.
#expect(try schema.validate(doc("De")).isEmpty)
}
}