diff --git a/CHANGELOG.md b/CHANGELOG.md index a3955fe..4a67a35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Sources/Schema/SchemaDerivationControl.swift b/Sources/Schema/SchemaDerivationControl.swift index c984b7a..659cf2e 100644 --- a/Sources/Schema/SchemaDerivationControl.swift +++ b/Sources/Schema/SchemaDerivationControl.swift @@ -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 { + 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 + } } diff --git a/Sources/Schema/XSDDerivation.swift b/Sources/Schema/XSDDerivation.swift index 11f2a8c..10e6015 100644 --- a/Sources/Schema/XSDDerivation.swift +++ b/Sources/Schema/XSDDerivation.swift @@ -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 = 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 } } diff --git a/Tests/SchemaSubstitutionTypeTests.swift b/Tests/SchemaSubstitutionTypeTests.swift index b7a08b4..492453f 100644 --- a/Tests/SchemaSubstitutionTypeTests.swift +++ b/Tests/SchemaSubstitutionTypeTests.swift @@ -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(""" - - + + diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index ff89af5..30e7bf6 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -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