From 0a76658a28df25d46ae263958792ec72b6c0a97e Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Sun, 21 Jun 2026 21:40:54 +0200 Subject: [PATCH] fix(schema): check UPA over an extension's effective content model Unique Particle Attribution checked only a complexContent extension's own particles, missing the cross-boundary non-determinism with its base: the effective content of an extension is the base type's content followed by the extension's, so a base particle that can recur or end optionally competes with the extension's leading particles. An element matching either was a UPA violation the single-node check did not see, and the schema was wrongly accepted (XSTS particlesZ022). Build the content model over the effective sequence (base content prepended, resolved recursively). The base is followed only for a local (target-namespace), non-cyclic base chain; a foreign, built-in (xs:anyType), unresolved, or cyclic base leaves the check on the extension's own content, so no model that cannot be fully assembled is rejected (an under-rejection, never a false positive). For a non-extension the effective list is a single node, so sequence([x]) reproduces the prior automaton exactly. Verified against the full W3C XSTS 2006-11-06 suite: invalid-schemas-accepted 31 -> 29 (particlesZ022 and one more), valid-schemas-rejected held at 0, no other bucket moved. Adds UPA tests for an ambiguous extension and a deterministic one (the over-rejection guard); moves the content-model-node and effective-model helpers to a new file to stay within the type-body and file-length budgets. --- CHANGELOG.md | 1 + Sources/Schema/ContentModelDeterminism.swift | 41 +++++-------- .../Schema/ContentModelEffectiveModel.swift | 60 +++++++++++++++++++ Tests/ContentModelDeterminismTests.swift | 23 +++++++ Tests/XSTSSuiteTests.swift | 4 +- 5 files changed, 102 insertions(+), 27 deletions(-) create mode 100644 Sources/Schema/ContentModelEffectiveModel.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index d83a2ee..5b1ffd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Unique Particle Attribution now checks a `complexContent` extension against its EFFECTIVE content model (the base type's content followed by the extension's), not just the extension's own particles. A base whose trailing particle can recur or end optionally competes with the extension's leading particles, so an element could match either across the extension boundary; that cross-boundary non-determinism was missed and the schema wrongly accepted. The base content is resolved and prepended only for a local (target-namespace), non-cyclic base chain; a foreign, built-in (`xs:anyType`), unresolved, or cyclic base leaves the check on the extension's own content, so no valid schema is newly rejected. XSTS invalid-schemas-accepted 31 → 29 (particlesZ022 and one more), valid-schemas-rejected held at 0. - Unique Particle Attribution now detects overlapping `xs:any` wildcards involving `##other`. `wildcardsOverlap` returned false for any pair involving `##other`, so two competing wildcards whose namespaces actually intersect, two `##other` constraints (which always share infinitely many namespaces) or `##other` against an enumerated set naming a non-target, non-absent namespace, were not flagged as non-deterministic, and the schema was wrongly accepted. The overlap is now computed correctly: `##other`∩`##other` always overlaps, and `##other`∩enumerated overlaps iff the set names a namespace that is neither absent (`##local`) nor the target. Disjoint cases (`##other` against `##local` or against the target namespace) remain deterministic, so no valid schema is newly rejected. XSTS invalid-schemas-accepted 35 → 31 (wildI009/wildI013/wildI014 and one more), valid-schemas-rejected held at 0. - The content-model matcher no longer blows up on a nested model group with a huge but finite `maxOccurs` (#192, partial). The counted-automaton active set is bounded by the diff --git a/Sources/Schema/ContentModelDeterminism.swift b/Sources/Schema/ContentModelDeterminism.swift index 61c64c2..9c9d5fc 100644 --- a/Sources/Schema/ContentModelDeterminism.swift +++ b/Sources/Schema/ContentModelDeterminism.swift @@ -28,11 +28,13 @@ public extension PureXML.Schema { var findings: [PureXML.Schema.SchemaLocatedFinding] = [] var seen: Set = [] forEachComplexType(schema) { complexType in - guard let model = contentModelNode(complexType), - let conflict = violation(model, bindings, context) - else { return } + let models = effectiveModelNodes(complexType, bindings, context, []) + guard !models.isEmpty, let conflict = violation(models, bindings, context) else { return } + // The conflicting particle the author can act on is in this type's own + // content, so locate on its own content-model node. + let node = contentModelNode(complexType) ?? complexType if seen.insert(conflict).inserted { - findings.append(PureXML.Schema.SchemaLocatedFinding(reason: conflict, node: model)) + findings.append(PureXML.Schema.SchemaLocatedFinding(reason: conflict, node: node)) } } return findings @@ -63,28 +65,17 @@ public extension PureXML.Schema { } } - /// The model-group node (`sequence`/`choice`/`all`/`group`) that holds a - /// complex type's content model, reached through `complexContent` and its - /// `restriction`/`extension`, or nil for simple or empty content. - private static func contentModelNode(_ complexType: XSDTree) -> XSDTree? { - if let direct = modelGroupChild(complexType) { return direct } - guard let complexContent = PureXML.Schema.XSDNode.firstChild(complexType, named: "complexContent") else { return nil } - for derivation in PureXML.Schema.XSDNode.elementChildren(complexContent) { - if let model = modelGroupChild(derivation) { return model } - } - return nil - } - - private static func modelGroupChild(_ node: XSDTree) -> XSDTree? { - PureXML.Schema.XSDNode.elementChildren(node).first { - ["sequence", "choice", "all", "group"].contains(PureXML.Schema.XSDNode.localName($0) ?? "") - } - } - - /// A UPA-violation message for the content model rooted at `model`, or nil. - private static func violation(_ model: XSDTree, _ bindings: [String: String], _ context: XSDContext) -> String? { + /// A UPA-violation message for the content model formed by `models` in + /// sequence, or nil. The effective content of a `complexContent` extension is + /// its base's content followed by its own, so the models are built as one + /// sequence: a base particle that can recur or end optionally then competes + /// with the extension's leading particles, the cross-boundary + /// non-determinism a single-node check misses (XSTS particlesZ022). For a + /// non-extension the list is a single node, so `sequence([x])` reproduces the + /// prior single-model automaton exactly. + private static func violation(_ models: [XSDTree], _ bindings: [String: String], _ context: XSDContext) -> String? { var automaton = PureXML.Schema.PositionAutomaton() - let root = build(model, &automaton, bindings, context, []) + let root = sequence(models.map { build($0, &automaton, bindings, context, []) }, &automaton) // A model that hit the position cap was only partially built; skip it // rather than risk a result from an incomplete automaton. if automaton.labels.count > positionCap { return nil } diff --git a/Sources/Schema/ContentModelEffectiveModel.swift b/Sources/Schema/ContentModelEffectiveModel.swift new file mode 100644 index 0000000..a8f1ce4 --- /dev/null +++ b/Sources/Schema/ContentModelEffectiveModel.swift @@ -0,0 +1,60 @@ +extension PureXML.Schema.ContentModelDeterminism { + /// The model-group node (`sequence`/`choice`/`all`/`group`) that holds a + /// complex type's content model, reached through `complexContent` and its + /// `restriction`/`extension`, or nil for simple or empty content. + static func contentModelNode(_ complexType: XSDTree) -> XSDTree? { + if let direct = modelGroupChild(complexType) { return direct } + guard let complexContent = PureXML.Schema.XSDNode.firstChild(complexType, named: "complexContent") else { return nil } + for derivation in PureXML.Schema.XSDNode.elementChildren(complexContent) { + if let model = modelGroupChild(derivation) { return model } + } + return nil + } + + private static func modelGroupChild(_ node: XSDTree) -> XSDTree? { + PureXML.Schema.XSDNode.elementChildren(node).first { + ["sequence", "choice", "all", "group"].contains(PureXML.Schema.XSDNode.localName($0) ?? "") + } + } + + /// The ordered model-group nodes whose concatenation is a complex type's + /// EFFECTIVE content model. For a `complexContent` extension this is the + /// base type's effective content followed by this type's own content (XSD + /// 1.0 3.4.2: a content type derived by extension is the base content type's + /// particle followed by the extension's). A `restriction` or the shorthand + /// form contributes only its own content. The base is followed only when it + /// resolves to a local (target-namespace) complex type whose chain is + /// non-cyclic; a foreign, built-in (e.g. `xs:anyType`), unresolved, or cyclic + /// base cannot be assembled here, so the cross-boundary check stands down to + /// this type's own content, never rejecting a model it cannot fully see (an + /// under-rejection, never a false positive). + static func effectiveModelNodes( + _ complexType: XSDTree, + _ bindings: [String: String], + _ context: PureXML.Schema.XSDContext, + _ visiting: Set, + ) -> [XSDTree] { + let own = contentModelNode(complexType).map { [$0] } ?? [] + guard let base = extensionBaseName(complexType), + PureXML.Schema.XSDNode.referenceNamespace(base, bindings) == context.targetNamespace + else { + return own + } + let baseName = PureXML.Schema.XSDNode.stripPrefix(base) + guard !visiting.contains(baseName), let baseNode = context.complexTypeNodes[baseName] else { + return own + } + return effectiveModelNodes(baseNode, bindings, context, visiting.union([baseName])) + own + } + + /// The `base` of a `complexContent` extension, or nil when the type is not a + /// complexContent extension (a restriction or the shorthand form). + private static func extensionBaseName(_ complexType: XSDTree) -> String? { + guard let content = PureXML.Schema.XSDNode.firstChild(complexType, named: "complexContent"), + let ext = PureXML.Schema.XSDNode.firstChild(content, named: "extension") + else { + return nil + } + return PureXML.Schema.XSDNode.attribute(ext, "base") + } +} diff --git a/Tests/ContentModelDeterminismTests.swift b/Tests/ContentModelDeterminismTests.swift index f3884c2..ac5dcac 100644 --- a/Tests/ContentModelDeterminismTests.swift +++ b/Tests/ContentModelDeterminismTests.swift @@ -65,6 +65,29 @@ struct ContentModelDeterminismTests { #expect(!rejects(complexType(""))) } + @Test("an extension is checked against its base's effective content model") + func test_extensionEffectiveModelAmbiguous() { + // The base's trailing `a*` is followed by the extension's `a`, so an `a` + // matches either across the extension boundary (XSTS particlesZ022). The + // check must see the base content prepended, not the extension's own only. + let body = """ + + + """ + #expect(rejects(body)) + } + + @Test("an extension adding distinct content is deterministic and compiles") + func test_extensionEffectiveModelDeterministic() { + // base `a` followed by the extension's `b` is `a, b`: deterministic, so + // prepending the base content must not reject a valid extension. + let body = """ + + + """ + #expect(!rejects(body)) + } + @Test("two same-name members of an all group are ambiguous") func test_allWithSameName() { #expect(rejects(complexType(#""#))) diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index f349861..9d9f448 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -146,9 +146,9 @@ struct XSTSSuiteTests { /// model-group occurrence, identity-constraint content, derivation base kind, attribute/simple-type, /// composition, simpleContent inline-type, complexContent mixedness, and substitution-group anySimpleType checks. /// src-resolve (#185) 43->39; xsi:Not-Allowed (#183) 39->37; NameAndTypeOK restriction-only + cos-all-limited group-ref (#158) 37->35. - /// UPA wildcard-overlap (`##other` was treated as overlapping nothing) 35->31 (wildI009/I013/I014 + one more), no other bucket moved. + /// UPA: `##other` overlap 35->31 (wildI009/I013/I014+1); effective extension content model 31->29 (particlesZ022+1); valid-rejected held at 0. private let knownSchemaValidRejected = 0 - private let knownSchemaInvalidAccepted = 31 + private let knownSchemaInvalidAccepted = 29 private let knownInstanceValidRejected = 0 /// Instance bucket (133 -> 26), per-step deltas in CHANGELOG.md: xsi:type must derive from the /// declared type (complex/atomic/list/union via the element-ref chain); anyType cannot stand in