fix(schema): inherit finalDefault as a complex type's final (#145)

A complex type without its own final attribute now inherits the schema's
finalDefault as its {final}, so finalDefault=#all or restriction/extension
blocks a complexContent restriction or extension derived from it. An explicit
final= overrides the default to empty. Complex types previously honoured only
an explicit final (elements already inherited finalDefault).

XSTS invalid-schemas-accepted 189 to 185, no other bucket moved.
This commit is contained in:
Mihaela Mihaljevic 2026-06-17 15:24:05 +02:00
parent 96373cd6a3
commit 4460508924
5 changed files with 43 additions and 23 deletions

View File

@ -22,6 +22,8 @@ Second pre-release. Continues the W3C XSTS schema-validity campaign: **invalid-s
### Fixed
- XSTS invalid-schemas-accepted (#145): a complex type without its own `final` attribute now inherits the schema's `finalDefault` as its `{final}` property, so `finalDefault="#all"` or `"restriction"`/`"extension"` blocks a `complexContent` restriction or extension derived from it just as an explicit `final` would (an explicit `final=""` overrides the default back to no restriction). Complex types previously honoured only an explicit `final`. Caught four invalid schemas: invalid-schemas-accepted 189 → 185, no other bucket moved.
- XSTS invalid-schemas-accepted (#145): the global-only element properties `abstract`, `final`, and `substitutionGroup` are now rejected on a local element declaration (an element particle, not a direct child of `schema`). They belong to the `topLevelElement` form only, so their presence on a local element is invalid at any value (`abstract="false"` is rejected as well as `"true"`). Caught five invalid schemas: invalid-schemas-accepted 194 → 189, no other bucket moved.
- XSTS invalid-schemas-accepted (#145): a `complexContent` extension that explicitly states `mixed` must agree with its base type's mixedness (`cos-ct-extends.1.4.3.2.2.1`): a mixed extension may extend only a mixed base, and an element-only extension only an element-only base. An extension that omits `mixed` inherits the base's setting and stays valid, so only an explicit conflict is rejected. Caught three invalid schemas: invalid-schemas-accepted 197 → 194, no other bucket moved.

View File

@ -48,4 +48,30 @@ extension PureXML.Schema.XSDParser {
}
return nil
}
/// The `{final}` derivation methods of a complex type or element: its own
/// `final` when present, otherwise the enclosing schema's `finalDefault`, each
/// intersected with the extension/restriction methods these components admit. An
/// explicit `final=""` overrides the default back to no restriction.
static func finalMethods(of node: XSDTree) -> Set<PureXML.Schema.DerivationMethod> {
if let own = PureXML.Schema.XSDNode.attribute(node, "final") {
return methodSet(own).intersection([.extension, .restriction])
}
if let finalDefault = schemaFinalDefault(of: node) {
return methodSet(finalDefault).intersection([.extension, .restriction])
}
return []
}
/// The `finalDefault` of the `schema` enclosing `node`, or nil.
static func schemaFinalDefault(of node: XSDTree) -> String? {
var current: XSDTree? = node
while let element = current {
if element.name?.namespaceURI == xsdNamespace, PureXML.Schema.XSDNode.localName(element) == "schema" {
return PureXML.Schema.XSDNode.attribute(element, "finalDefault")
}
current = element.parent
}
return nil
}
}

View File

@ -58,7 +58,7 @@ extension PureXML.Schema.XSDParser {
if XSDDerivNode.attribute(type, "abstract") == "true" { tables.abstractTypes.insert(name) }
let block = methodSet(XSDDerivNode.attribute(type, "block"))
if !block.isEmpty { tables.typeBlock[name] = block }
let finalSet = methodSet(XSDDerivNode.attribute(type, "final"))
let finalSet = finalMethods(of: type)
if !finalSet.isEmpty { tables.typeFinal[name] = finalSet }
if let derivation = derivationInfo(type) { tables.typeDerivation[name] = derivation }
}
@ -73,24 +73,7 @@ extension PureXML.Schema.XSDParser {
$0.name?.namespaceURI == xsdNamespace && XSDDerivNode.localName($0) == "schema"
} ?? false
if isGlobal {
var schemaFinalDefault: String?
var current: XSDTree? = element
while let parentSchema = current {
if parentSchema.name?.namespaceURI == xsdNamespace, XSDDerivNode.localName(parentSchema) == "schema" {
schemaFinalDefault = XSDDerivNode.attribute(parentSchema, "finalDefault")
break
}
current = parentSchema.parent
}
let finalAttr = XSDDerivNode.attribute(element, "final")
let finalSet: Set<PureXML.Schema.DerivationMethod> = if let finalAttr {
methodSet(finalAttr).intersection([.extension, .restriction])
} else if let schemaFinalDefault {
methodSet(schemaFinalDefault).intersection([.extension, .restriction])
} else {
[]
}
let finalSet = finalMethods(of: element)
if !finalSet.isEmpty { tables.elementFinal[name] = finalSet }
}

View File

@ -141,13 +141,17 @@ struct SchemaSubstitutionTypeTests {
@Test("Substitution group member final exclusions: local name collisions and built-ins")
func test_finalSubstitutionExclusionEdgeCases() {
// Local element name collision: local element does not overwrite global final setting
// Local element name collision: local element does not overwrite global final setting.
// Base carries an explicit empty final so the schema's finalDefault="extension" (which a
// complex type also inherits as its {final}) does not itself block Ext's extension of Base;
// the point under test is that the LOCAL element named "head" inside Ext does not clobber
// the GLOBAL "head" element's final="restriction".
#expect((try? PureXML.Schema.Document("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" finalDefault="extension">
<xs:element name="head" type="Base" final="restriction"/>
<xs:element name="member" substitutionGroup="head" type="Ext"/>
<xs:complexType name="Base"><xs:sequence/></xs:complexType>
<xs:complexType name="Ext">
<xs:complexType name="Base" final=""><xs:sequence/></xs:complexType>
<xs:complexType name="Ext" final="">
<xs:complexContent>
<xs:extension base="Base">
<xs:sequence>

View File

@ -139,8 +139,13 @@ struct XSTSSuiteTests {
/// direct child of schema; invalid at any value, so `abstract="false"` is rejected
/// too) then caught five more invalid schemas: invalid-schemas accepted 194 → 189,
/// no other bucket moved.
/// Inheriting the schema's `finalDefault` as a complex type's `{final}` when the
/// type states no `final` of its own (so `finalDefault="#all"`/`"restriction"`
/// blocks a restriction or extension derivation from it; explicit `final=""`
/// overrides) then caught four more invalid schemas: invalid-schemas accepted
/// 189 → 185, no other bucket moved.
private let knownSchemaValidRejected = 1
private let knownSchemaInvalidAccepted = 189
private let knownSchemaInvalidAccepted = 185
private let knownInstanceValidRejected = 3
private let knownInstanceInvalidAccepted = 132