feat(schema): enforce simpleContent derivation content order
A simpleContent restriction is (annotation?, (simpleType?, facet*), (attribute|attributeGroup)*, anyAttribute?) and an extension is (annotation?, (attribute|attributeGroup)*, anyAttribute?): no model group, no facets/simpleType in an extension, attributes before a single anyAttribute. The membership table admitted the names but not the order or the model-group ban, so a model group inside simpleContent, a facet in an extension, a misordered facet or attribute, or a second anyAttribute (the ctD family) was accepted. The derivation's children are matched against the context's ordered slot model, reusing the slot walk built for complexContent. The structure walk's per-component dispatch is factored into a helper to keep its complexity bounded. XSTS: invalid schemas accepted 594 to 580, valid schemas rejected held at 72, instance buckets unchanged.
This commit is contained in:
parent
68273205f6
commit
6a7fb1d594
@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- A `simpleContent` derivation's content is now checked for order and disallowed children (#158). A `restriction` is `(annotation?, (simpleType?, facet*), (attribute | attributeGroup)*, anyAttribute?)` and an `extension` is `(annotation?, (attribute | attributeGroup)*, anyAttribute?)`: simpleContent has no model group, an extension has no facets or `simpleType`, and the attributes precede a single trailing `anyAttribute`. The membership table admitted the names but not the model, so a model group inside simpleContent, a facet in an extension, a facet before its `simpleType`, an attribute after `anyAttribute`, or a second `anyAttribute` (the ctD family) was accepted. The derivation's children are matched against the context's ordered slot model, reusing the slot walk built for complexContent. XSTS: invalid schemas accepted 594 to 580, no other bucket raised (valid schemas rejected held at 72).
|
||||
- Particle Valid (Restriction) now checks an element's type by derivation, not just its name (NameAndTypeOK, #158). When a restriction maps a same-named element onto a base element, the restricting type must be, or derive by restriction from, the base element's type; renaming `xs:string` to `xs:int`, widening a concrete type to `xs:anyType`, or naming an unrelated type (a different user type, a wider ancestor, an absent/`anyType` type over a concrete base) was accepted because only the names were compared. Each element particle now carries its type's derivation identity (the resolved type name, `anyType` for an absent type, or nil for an inline anonymous type), named `simpleType` restriction derivations are recorded alongside complex ones, and the check walks the user restriction chain and then the built-in lattice to decide derivation by name; an ur-type base, a union base (membership is not a restriction chain), and an inline anonymous type are permitted, so no valid restriction is rejected. XSTS: invalid schemas accepted 621 to 594, invalid instances accepted 165 to 160 (more invalid schemas rejected up front), no bucket raised (valid schemas rejected held at 72).
|
||||
- An element/attribute `default`/`fixed` value must be a valid value of its `type` (Attribute/Element Locally Valid, XSD 1.0, #158). A `fixed="abc"` on an `xs:integer` or a `default="1 2"` on an `xs:int` was accepted because the value constraint was never checked at compile time. The value is now validated against its type when the type resolves to a built-in datatype (namespace-aware: only a `type` in the XSD namespace), normalising whitespace per the type as XSD requires (so `default=" 5 "` on an integer is valid); a named or inline user type is left to its own rules, so no valid value constraint is rejected. XSTS: invalid schemas accepted 638 to 621, no other bucket raised (valid schemas rejected held at 72).
|
||||
- A constraining facet must apply to its simple type's variety (XSD 1.0 Part 2, #158). A `list` may be constrained only by `length`/`minLength`/`maxLength`, `pattern`, `enumeration`, and `whiteSpace`; a `union` only by `pattern` and `enumeration`. A value-bound facet (`maxInclusive`, `totalDigits`, ...) constrains an atomic value space, so restricting a list (`list of integer`) by `maxInclusive` was wrongly accepted. The base's variety is resolved from the raw tree (the check runs before the types are compiled), namespace-aware so a user type that shares a built-in's local name does not flip the variety: a base in the XSD namespace is a built-in (only the list datatypes are list-variety), a base in the schema's own target namespace resolves to a declared global, and an external or unresolvable base is skipped, so no valid restriction is rejected. XSTS: invalid schemas accepted 664 to 638, no other bucket raised (valid schemas rejected held at 72).
|
||||
|
||||
@ -45,6 +45,35 @@ extension PureXML.Schema.XSDParser {
|
||||
return ["the content of a complexType must be a model group, then attributes, then anyAttribute: \(reason)"]
|
||||
}
|
||||
|
||||
/// Content-model order for a `simpleContent` derivation (XSD 1.0 Structures),
|
||||
/// whose model has NO model group: a `restriction` is
|
||||
/// `(annotation?, (simpleType?, facet*), (attribute | attributeGroup)*, anyAttribute?)`
|
||||
/// and an `extension` is `(annotation?, (attribute | attributeGroup)*, anyAttribute?)`.
|
||||
/// So a model group anywhere, a facet or `simpleType` in an extension, a facet
|
||||
/// before its `simpleType`, an attribute before a facet, an attribute after
|
||||
/// `anyAttribute`, or a second `anyAttribute` was accepted by the membership
|
||||
/// table but is invalid (the ctD family).
|
||||
static func simpleContentOrderErrors(_ simpleContent: XSDTree) -> [String] {
|
||||
var errors: [String] = []
|
||||
for derivation in PureXML.Schema.XSDNode.elementChildren(simpleContent) {
|
||||
guard let local = PureXML.Schema.XSDNode.localName(derivation) else { continue }
|
||||
let slots: [(members: Set<String>, max: Int?)]
|
||||
switch local {
|
||||
case "restriction": slots = simpleContentRestrictionSlots
|
||||
case "extension": slots = simpleContentExtensionSlots
|
||||
default: continue
|
||||
}
|
||||
let names = PureXML.Schema.XSDNode.elementChildren(derivation)
|
||||
.filter { $0.name?.namespaceURI == xsdNamespace }
|
||||
.compactMap(PureXML.Schema.XSDNode.localName)
|
||||
.filter { $0 != "annotation" }
|
||||
if let reason = slotOrderViolation(names, slots: slots) {
|
||||
errors.append("the content of a simpleContent '\(local)' is malformed: \(reason)")
|
||||
}
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
/// The ordered slots of a `complexContent` derivation: each lists its admitted
|
||||
/// names and how many children may fill it (nil is unbounded).
|
||||
private static let complexDerivationSlots: [(members: Set<String>, max: Int?)] = [
|
||||
@ -53,6 +82,27 @@ extension PureXML.Schema.XSDParser {
|
||||
(["anyAttribute"], 1),
|
||||
]
|
||||
|
||||
private static let constrainingFacets: Set<String> = [
|
||||
"minExclusive", "minInclusive", "maxExclusive", "maxInclusive", "totalDigits",
|
||||
"fractionDigits", "length", "minLength", "maxLength", "enumeration", "whiteSpace", "pattern",
|
||||
]
|
||||
|
||||
/// The ordered slots of a `simpleContent` `restriction`: an optional `simpleType`,
|
||||
/// then facets, then attributes, then an optional `anyAttribute`.
|
||||
private static let simpleContentRestrictionSlots: [(members: Set<String>, max: Int?)] = [
|
||||
(["simpleType"], 1),
|
||||
(constrainingFacets, nil),
|
||||
(["attribute", "attributeGroup"], nil),
|
||||
(["anyAttribute"], 1),
|
||||
]
|
||||
|
||||
/// The ordered slots of a `simpleContent` `extension`: attributes, then an
|
||||
/// optional `anyAttribute` (no facets, `simpleType`, or model group).
|
||||
private static let simpleContentExtensionSlots: [(members: Set<String>, max: Int?)] = [
|
||||
(["attribute", "attributeGroup"], nil),
|
||||
(["anyAttribute"], 1),
|
||||
]
|
||||
|
||||
/// The first child that does not fit the ordered slot model, or nil. Each child
|
||||
/// must fill the current slot or one after it (order is forward-only), and a slot
|
||||
/// with a finite `max` may not be overfilled.
|
||||
|
||||
@ -136,36 +136,46 @@ extension PureXML.Schema.XSDParser {
|
||||
let local = PureXML.Schema.XSDNode.localName(node)
|
||||
if local == "appinfo" || local == "documentation" { return }
|
||||
let children = PureXML.Schema.XSDNode.elementChildren(node)
|
||||
if node.name?.namespaceURI == xsdNamespace {
|
||||
if node.name?.namespaceURI == xsdNamespace, let local {
|
||||
errors += attributeValueErrors(node)
|
||||
errors += occurrenceOrderErrors(node)
|
||||
if let local { errors += attributeApplicabilityErrors(node, local: local) }
|
||||
errors += attributeApplicabilityErrors(node, local: local)
|
||||
let names = children.filter { $0.name?.namespaceURI == xsdNamespace }.compactMap(PureXML.Schema.XSDNode.localName)
|
||||
if let local, let allowed = allowedChildren[local] {
|
||||
errors += childErrors(local: local, children: names, allowed: allowed)
|
||||
}
|
||||
if local == "group", PureXML.Schema.XSDNode.attribute(node, "name") != nil {
|
||||
errors += namedGroupErrors(names)
|
||||
}
|
||||
if local == "complexType" {
|
||||
errors += elementDeclsConsistentErrors(node)
|
||||
errors += complexTypeOrderErrors(node)
|
||||
}
|
||||
if local == "complexContent" {
|
||||
errors += complexContentOrderErrors(node)
|
||||
}
|
||||
if let local, local == "selector" || local == "field" {
|
||||
errors += identityXPathErrors(node, local: local)
|
||||
}
|
||||
if let local, local == "any" || local == "anyAttribute" {
|
||||
errors += wildcardNamespaceErrors(node)
|
||||
}
|
||||
errors += componentSpecificErrors(node, local: local, names: names)
|
||||
}
|
||||
for child in children {
|
||||
collectStructure(child, into: &errors)
|
||||
}
|
||||
}
|
||||
|
||||
/// The structural findings specific to one XSD component: its admitted children
|
||||
/// (the content-model membership table) plus the per-component content-order,
|
||||
/// identity-XPath, wildcard-namespace, and consistency rules.
|
||||
private static func componentSpecificErrors(_ node: XSDTree, local: String, names: [String]) -> [String] {
|
||||
var errors: [String] = []
|
||||
if let allowed = allowedChildren[local] {
|
||||
errors += childErrors(local: local, children: names, allowed: allowed)
|
||||
}
|
||||
switch local {
|
||||
case "group" where PureXML.Schema.XSDNode.attribute(node, "name") != nil:
|
||||
errors += namedGroupErrors(names)
|
||||
case "complexType":
|
||||
errors += elementDeclsConsistentErrors(node)
|
||||
errors += complexTypeOrderErrors(node)
|
||||
case "complexContent":
|
||||
errors += complexContentOrderErrors(node)
|
||||
case "simpleContent":
|
||||
errors += simpleContentOrderErrors(node)
|
||||
case "selector", "field":
|
||||
errors += identityXPathErrors(node, local: local)
|
||||
case "any", "anyAttribute":
|
||||
errors += wildcardNamespaceErrors(node)
|
||||
default:
|
||||
break
|
||||
}
|
||||
return errors
|
||||
}
|
||||
|
||||
/// The enumerated value space of the schema-vocabulary attributes that take a
|
||||
/// fixed set of tokens. Each name has one meaning throughout XSD, so checking
|
||||
/// by local name is safe; `minOccurs`/`maxOccurs` are handled numerically.
|
||||
|
||||
49
Tests/SchemaContentOrderTests.swift
Normal file
49
Tests/SchemaContentOrderTests.swift
Normal file
@ -0,0 +1,49 @@
|
||||
@testable import PureXML
|
||||
import Testing
|
||||
|
||||
/// Content-model order and cardinality for the derivation contexts whose model
|
||||
/// the set-membership table cannot express: a `simpleContent` derivation (facets
|
||||
/// and attributes, never a model group). Complements the complexContent and
|
||||
/// shorthand-complexType order tests in `SchemaStructureTests` (the ctD family).
|
||||
@Suite("Schema content order")
|
||||
struct SchemaContentOrderTests {
|
||||
private func rejects(_ body: String) -> Bool {
|
||||
do {
|
||||
_ = try PureXML.Schema.Document("""
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||
\(body)
|
||||
</xs:schema>
|
||||
""")
|
||||
return false
|
||||
} catch {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private func simpleContent(_ body: String) -> String {
|
||||
#"<xs:complexType name="t"><xs:simpleContent>\#(body)</xs:simpleContent></xs:complexType>"#
|
||||
}
|
||||
|
||||
@Test("a simpleContent derivation may not contain a model group")
|
||||
func test_noModelGroup() {
|
||||
#expect(rejects(simpleContent(#"<xs:extension base="xs:string"><xs:sequence/></xs:extension>"#)))
|
||||
#expect(rejects(simpleContent(#"<xs:restriction base="xs:string"><xs:choice/></xs:restriction>"#)))
|
||||
}
|
||||
|
||||
@Test("a simpleContent extension may not carry facets or a simpleType")
|
||||
func test_extensionNoFacets() {
|
||||
#expect(rejects(simpleContent(#"<xs:extension base="xs:string"><xs:maxLength value="3"/></xs:extension>"#)))
|
||||
}
|
||||
|
||||
@Test("a simpleContent derivation's attributes and anyAttribute must be ordered")
|
||||
func test_attributeOrder() {
|
||||
#expect(rejects(simpleContent(#"<xs:extension base="xs:string"><xs:anyAttribute/><xs:anyAttribute/></xs:extension>"#)))
|
||||
#expect(rejects(simpleContent(#"<xs:restriction base="xs:string"><xs:anyAttribute/><xs:attribute name="a"/></xs:restriction>"#)))
|
||||
}
|
||||
|
||||
@Test("well-formed simpleContent derivations compile")
|
||||
func test_valid() {
|
||||
#expect(!rejects(simpleContent(#"<xs:extension base="xs:string"><xs:attribute name="a" type="xs:string"/></xs:extension>"#)))
|
||||
#expect(!rejects(simpleContent(#"<xs:restriction base="xs:string"><xs:maxLength value="3"/><xs:attribute name="a"/></xs:restriction>"#)))
|
||||
}
|
||||
}
|
||||
@ -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 = 72
|
||||
private let knownSchemaInvalidAccepted = 594
|
||||
private let knownSchemaInvalidAccepted = 580
|
||||
private let knownInstanceValidRejected = 214
|
||||
private let knownInstanceInvalidAccepted = 160
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ irreducible tail. Do not deviate to new features while this is open.
|
||||
| Bucket | Count | Reading |
|
||||
|---|---|---|
|
||||
| valid-schemas-rejected | 72 | we rarely reject a good schema |
|
||||
| **invalid-schemas-accepted** | **2461 -> 594** | we rarely catch a bad one (facet definition + applicability, default/fixed value validity, particle-restriction name+type, id, structural content-model, UPA determinism, complexContent + complexType content order, identity-constraint XPath-subset, and wildcard-namespace validity landed) |
|
||||
| **invalid-schemas-accepted** | **2461 -> 580** | we rarely catch a bad one (facet definition + applicability, default/fixed value validity, particle-restriction name+type, id, structural content-model, UPA determinism, complexContent + complexType content order, identity-constraint XPath-subset, and wildcard-namespace validity landed) |
|
||||
| valid-instances-rejected | 233 | (instance side, separately tracked) |
|
||||
| invalid-instances-accepted | 165 | |
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user