diff --git a/CHANGELOG.md b/CHANGELOG.md index 731e6a9..0f4a011 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- An `xs:all` group may no longer appear in a `complexContent` extension of a base that already has element content (XSD 1.0 `cos-all-limited.1.2`, #170). The effective content model of such an extension is `sequence(base content, extension content)`, which nests the `all` and violates the rule that an `all` must be the whole content model. Detected from the compiled base type: rejected only when the base resolves (in the schema's own target namespace) to a complex type with element-only or mixed content. An explicitly-empty base (an empty model group with `mixed="false"`, whose content is effectively empty) may still be extended by an `all`, since the `all` becomes the whole content; a simple-content, empty, foreign, or unresolved base is left alone. Checked for a self-contained schema. XSTS: invalid schemas accepted 411 to 403, no other bucket raised. - A simple type whose `final` contains `list` (or `#all`) may no longer be used as a list `itemType`, and one whose `final` contains `union` (or `#all`) may no longer be a union `memberType` (XSD 1.0 simple-type `final`, #170). The restriction and extension directions were already enforced by `finalRespected`; the list and union directions were unchecked because `final="list"`/`"union"` is not modelled by the derivation-method set, so the `final` attribute is read directly. The reference is matched only when it resolves to the schema's own target namespace, so a list or union of a built-in (whose final is empty) is not confused with a user type of the same local name. Checked for a self-contained schema. XSTS: invalid schemas accepted 413 to 411, no other bucket raised. - A `whiteSpace` facet may only strengthen, never relax, its base type's effective whiteSpace (XSD Part 2 ยง4.3.6, #170). The order is `preserve` < `replace` < `collapse`, so a base fixed to `collapse` admits only `collapse`, and a `replace` base admits `replace` or `collapse`. A derivation that relaxes it (for example `whiteSpace="replace"` over a `collapse` base, or restricting `xs:token` to `replace`) was accepted and is now rejected. The base's effective whiteSpace is its own facet, falling back to its built-in intrinsic; the check is atomic-only and, when a base type's own whiteSpace has not yet resolved, can only under-detect, never reject a valid restriction. XSTS: invalid schemas accepted 422 to 413, no other bucket raised. - A top-level `xs:element` declaration may no longer carry `ref`, `form`, `minOccurs`, or `maxOccurs` (XSD 1.0 schema-for-schemas `topLevelElement`, #170). A global element is a declaration, never a particle or a reference, so those local-use attributes are invalid. The top-level-attribute applicability scan added earlier is generalized to cover both kinds; a local element particle inside a content model is unaffected. XSTS: invalid schemas accepted 424 to 422, no other bucket raised. diff --git a/Sources/Schema/ParticleRestriction.swift b/Sources/Schema/ParticleRestriction.swift index 7c1e6d8..5f11c46 100644 --- a/Sources/Schema/ParticleRestriction.swift +++ b/Sources/Schema/ParticleRestriction.swift @@ -172,7 +172,7 @@ extension PureXML.Schema { /// Whether a particle can never contribute a child element or wildcard: it /// has `maxOccurs=0`, or is a group all of whose members are content-free /// (an empty group included). Its only legal content is the empty sequence. - private static func contentFree(_ particle: Particle) -> Bool { + static func contentFree(_ particle: Particle) -> Bool { if particle.maxOccurs == 0 { return true } switch particle.term { case .element, .wildcard: diff --git a/Sources/Schema/SchemaExtensionAllGroup.swift b/Sources/Schema/SchemaExtensionAllGroup.swift new file mode 100644 index 0000000..d7c967a --- /dev/null +++ b/Sources/Schema/SchemaExtensionAllGroup.swift @@ -0,0 +1,52 @@ +private typealias ExtAllNode = PureXML.Schema.XSDNode + +extension PureXML.Schema.XSDParser { + /// XSD 1.0 `cos-all-limited.1.2` reached through extension: an `xs:all` group + /// must be the whole content model of a complex type. When a complex type + /// extends a base that already has element content, the effective content model + /// is `sequence(base content, extension content)`, so an `all` in the extension + /// is nested in that sequence and is invalid. Such schemas were accepted. + /// + /// Detected from the compiled base type: the extension is rejected only when its + /// base resolves (in this schema's own target namespace) to a complex type whose + /// content is element-only or mixed. An empty base (extending it with an `all` + /// makes the `all` the whole content, which is valid), a simple-content base, or + /// an unresolved/foreign base is left alone. Checked for a self-contained schema. + static func extensionAllGroupErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext, _ types: [String: PureXML.Schema.ElementType]) -> [String] { + guard !hasExternalReference(schema) else { return [] } + let bindings = context.namespaceBindings + var errors: [String] = [] + for content in descendants(schema, named: "complexContent") { + guard let ext = ExtAllNode.firstChild(content, named: "extension"), + extensionHasAllGroup(ext), + let base = ExtAllNode.attribute(ext, "base"), + ExtAllNode.referenceNamespace(base, bindings) == context.targetNamespace, + case let .complex(complex)? = types[ExtAllNode.stripPrefix(base)], + baseHasElementContent(complex.content) + else { continue } + errors.append("an 'all' group may not extend the type '\(ExtAllNode.stripPrefix(base))', which has its own content; an all group must be the whole content model") + } + return errors + } + + private static func extensionHasAllGroup(_ extension: XSDTree) -> Bool { + ExtAllNode.elementChildren(`extension`).contains { child in + child.name?.namespaceURI == xsdNamespace && ExtAllNode.localName(child) == "all" + } + } + + /// Whether the base type's content would wrap an `all` extension in a sequence. + /// Mixed content always does (a mixed base, even with an empty particle, may not + /// be extended by an `all`, per the resolution of W3C Bug 6202). Element-only + /// content does only when its particle can actually contribute a child: an + /// explicit but empty model group (``) is effectively empty, and + /// extending empty content with an `all` makes the `all` the whole content, + /// which is valid. Empty and simple-content bases carry no element content. + private static func baseHasElementContent(_ content: PureXML.Schema.ContentType) -> Bool { + switch content { + case .mixed: true + case let .elementOnly(particle): !PureXML.Schema.ParticleRestriction.contentFree(particle) + case .empty, .simpleContent: false + } + } +} diff --git a/Sources/Schema/SchemaIDAttribute.swift b/Sources/Schema/SchemaIDAttribute.swift index aaff692..9362b7b 100644 --- a/Sources/Schema/SchemaIDAttribute.swift +++ b/Sources/Schema/SchemaIDAttribute.swift @@ -71,5 +71,6 @@ extension PureXML.Schema.XSDParser { + idValueConstraintErrors(schema, context) + substitutionTypeErrors(schema, derivation, typeMaps.named) + userTypeValueConstraintErrors(schema, context, typeMaps.named) + + extensionAllGroupErrors(schema, context, typeMaps.named) } } diff --git a/Tests/SchemaExtensionAllGroupTests.swift b/Tests/SchemaExtensionAllGroupTests.swift new file mode 100644 index 0000000..bd3b6d3 --- /dev/null +++ b/Tests/SchemaExtensionAllGroupTests.swift @@ -0,0 +1,59 @@ +@testable import PureXML +import Testing + +@Suite("XSD all-group in extension (cos-all-limited.1.2 via extension)") +struct SchemaExtensionAllGroupTests { + private let xsd = "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" + + private func compiles(_ body: String) -> Bool { + (try? PureXML.Schema.Document("\(body)")) != nil + } + + private func ext(base: String, content: String) -> String { + "" + + "\(content)" + + "" + } + + private let seqBase = "" + + "" + private let emptyBase = "" + private let allContent = "" + + @Test("An all group extending a base with content is rejected") + func test_allExtendingContentRejected() { + #expect(!compiles(seqBase + ext(base: "myType", content: allContent))) + } + + @Test("An all group extending an empty base compiles") + func test_allExtendingEmptyAccepted() { + #expect(compiles(emptyBase + ext(base: "emptyType", content: allContent))) + } + + @Test("A sequence extension of a content base compiles") + func test_sequenceExtensionAccepted() { + #expect(compiles(seqBase + ext(base: "myType", content: ""))) + } + + @Test("An all group as the whole content of a complex type compiles") + func test_allAsWholeContentAccepted() { + #expect(compiles("\(allContent)")) + } + + /// A base whose content is an explicit but empty model group (``) + /// has effectively empty content, so extending it with an `all` makes the `all` + /// the whole content model, which is valid (W3C Bug 6202). + @Test("An all group extending an explicitly-empty (non-mixed) base compiles") + func test_allExtendingExplicitEmptyAccepted() { + let base = "" + #expect(compiles(base + ext(base: "emptyGroup", content: allContent))) + } + + /// A mixed base, even with an empty particle, may not be extended by an `all` + /// (the resolution of W3C Bug 6202 keeps this invalid). + @Test("An all group extending a mixed empty base is rejected") + func test_allExtendingMixedEmptyRejected() { + let base = "" + #expect(!compiles(base + ext(base: "mixedEmpty", content: allContent))) + } +} diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 7681e9a..4bd49a0 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -29,7 +29,7 @@ struct XSTSSuiteTests { /// XSTS` (debug is far slower and the corpus is large). Per-case deviations /// are written to /tmp/xsts-failures.txt for the burn-down. private let knownSchemaValidRejected = 1 - private let knownSchemaInvalidAccepted = 411 + private let knownSchemaInvalidAccepted = 403 private let knownInstanceValidRejected = 180 private let knownInstanceInvalidAccepted = 158 diff --git a/docs/schema-validity-burndown.md b/docs/schema-validity-burndown.md index 27ba76e..780a195 100644 --- a/docs/schema-validity-burndown.md +++ b/docs/schema-validity-burndown.md @@ -10,7 +10,7 @@ irreducible tail. Do not deviate to new features while this is open. | Bucket | Count | Reading | |---|---|---| | valid-schemas-rejected | **72 -> 4** | production target is 0: rejecting a correct schema is the most user-hostile defect (#148). Particle-restriction over-rejection fixed: pointless `maxOccurs=0` particles removed, content-free restriction of an EMPTY base, empty-derived emptiable-base, and (#163) the pairwise check over the normalized content model with occurrence subsumption applied only to same-kind pairings (cross-kind element/group, group/element, group/wildcard match occurrence inside, by effective total range / member). #164 resolved element-ref namespaces by prefix binding (the `particlesJj/Jk/Jm/Jn/Js`, `particlesQ/R` cluster, -28). Content-free (`maxOccurs=0`) derived groups and substitution-group declaration order closed `particlesW006`/`elemZ027_a`; the W3C MapAndSum rule (Sequence:Choice, #165) closed `particlesV003`. Remaining 1 (`particlesZ001`): a documented spec ambiguity (RecurseAsIfGroup; the reference rejects it), not a defect. | -| **invalid-schemas-accepted** | **2461 -> 411** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion, all-group reference placement, substitution-member type derivation, default/fixed mutual exclusion landed). | +| **invalid-schemas-accepted** | **2461 -> 403** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion, all-group reference placement, substitution-member type derivation, default/fixed mutual exclusion landed). | | valid-instances-rejected | **233 -> 180** | (instance side; #164's element-ref namespace fix is shared with instance validation, -34) | | invalid-instances-accepted | **160 -> 158** | (instance side; resolving an inline `` restriction base preserves list/union variety, so a `length` facet counts items and two more invalid instances are caught) |