From b6a39f70968b8e217eb50307e55c9b362403b670 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Fri, 5 Jun 2026 14:34:17 +0200 Subject: [PATCH] docs(api): reject arbitrary any conversion --- Tests/ArbitraryAnyDecisionTests.swift | 36 +++++++++++++++++++++++++++ docs/DESIGN.md | 17 +++++++++++++ docs/MIGRATION.md | 26 ++++++++++++++++++- docs/USAGE.md | 9 +++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 Tests/ArbitraryAnyDecisionTests.swift diff --git a/Tests/ArbitraryAnyDecisionTests.swift b/Tests/ArbitraryAnyDecisionTests.swift new file mode 100644 index 0000000..172f040 --- /dev/null +++ b/Tests/ArbitraryAnyDecisionTests.swift @@ -0,0 +1,36 @@ +@testable import PureYAML +import Testing + +@Suite("Arbitrary Any API Decision") +struct ArbitraryAnyDecisionTests { + @Test("Model values preserve diagnostics that dictionary-shaped projections erase") + func test_modelValuesPreserveDiagnosticsThatDictionaryShapedProjectionsErase() { + let value = PureYAML.Model.Value.mapping(.init([ + .init(key: "title", value: .string("First")), + .init(key: "title", value: .string("Second")), + .init(key: "slug", value: .string("example")), + ])) + + guard case let .mapping(mapping) = value else { + recordIssue("expected mapping fixture") + return + } + + let projectedDictionary = Dictionary( + mapping.pairs.map { ($0.key, $0.value) }, + uniquingKeysWith: { _, second in second }, + ) + let validationResult = PureYAML.Validation.Validator().collect(value) + + #expect(mapping.pairs.map(\.key) == ["title", "title", "slug"]) + #expect(projectedDictionary.keys.sorted() == ["slug", "title"]) + #expect(projectedDictionary["title"] == .string("Second")) + #expect(validationResult.issues == [ + .init( + severity: .error, + reason: "Duplicate mapping key 'title'", + path: .init([.key("title")]), + ), + ]) + } +} diff --git a/docs/DESIGN.md b/docs/DESIGN.md index a3737c2..35d24e2 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -27,6 +27,8 @@ blocking browser deployment. - No C source in the public package. - No external SwiftPM dependency. - No JavaScript parser or build-time generator. +- No arbitrary `Any` load or dump API that collapses YAML mappings into + dictionary-shaped values before validation. - No claim of complete YAML 1.2 coverage until the test corpus proves it. ## Current Architecture @@ -136,6 +138,21 @@ Typed decoding runs default validation before Swift `Decodable` construction. That keeps ambiguous YAML states, such as duplicate mapping keys, from being silently collapsed by keyed property lookup. +## Rejected API Shape: Arbitrary Any + +PureYAML does not provide a public arbitrary `Any` loader or dumper. Unknown YAML +must stay in `PureYAML.Model.Value`; known YAML should use typed `Codable`. + +This is a deterministic validation decision. A Swift dictionary-shaped value +cannot preserve all YAML mapping entries when keys repeat, cannot represent +complex mapping keys with the current public model, and makes merge/tag +normalization ambiguous. The research constructor also includes +Foundation-specific scalar conversions, which would break the dependency-free +and WASM-compatible library boundary. + +Application code may project `Model.Value` into JSON-like dictionaries after +validation, but that projection must own the data-loss policy explicitly. + ## Planned Compatibility Work Compatibility should be added in small, test-backed slices: diff --git a/docs/MIGRATION.md b/docs/MIGRATION.md index 7ef59eb..c53f058 100644 --- a/docs/MIGRATION.md +++ b/docs/MIGRATION.md @@ -25,7 +25,31 @@ See [../ATTRIBUTION.md](../ATTRIBUTION.md) for the project attribution. | Decode a `Decodable` type from YAML | `PureYAML.decode(_:from:)` | Runs default validation before typed decoding. | | Encode an `Encodable` value to YAML | `PureYAML.encodeToYAML(_:options:)` | Encodes through `PureYAML.Model.Value`, then dumps. | | Validate document structure | `PureYAML.validate(_:using:strict:)` | Default rule reports duplicate mapping keys with exact paths. | -| Load arbitrary `Any` dictionaries and arrays | Use `Model.Value` or typed `Codable` | There is no arbitrary `Any` conversion API yet. | +| Load arbitrary `Any` dictionaries and arrays | Use `Model.Value` or typed `Codable` | There is deliberately no public arbitrary `Any` conversion API. Dictionary-shaped values erase YAML diagnostics. | + +## Arbitrary Any Decision + +PureYAML does not expose `loadAny`, `dumpAny`, or dictionary-and-array `Any` +entry points. That is a deliberate compatibility boundary, not a missing +wrapper. + +The research implementation's arbitrary-value constructor is useful to study, +but its observable shape conflicts with PureYAML's first principles: + +- mapping conversion becomes dictionary-shaped, so duplicate keys and insertion + order are not a first-class contract; +- merge keys may be flattened during construction instead of remaining visible + to validation; +- scalar construction includes Foundation-specific values such as binary data, + dates, and null objects; +- custom constructors can hide whether a document was parsed, normalized, + flattened, or validated. + +For unknown schemas, use `PureYAML.Model.Value` and validate before projecting +into project-specific values. For known schemas, use typed `Codable`. If a +caller truly needs a JSON-like dictionary after validation, build that projection +in the application layer where duplicate-key and unsupported-shape policies are +explicit. ## Supported Today diff --git a/docs/USAGE.md b/docs/USAGE.md index 4ea29cb..23f3d0b 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -48,6 +48,10 @@ Those tests are the executable contract for the short-form examples. | Encode a typed value to a value tree | `PureYAML.encode(_:)` | `PureYAML.Model.Value` | | Encode a typed value to YAML | `PureYAML.encodeToYAML(_:options:)` | `String` | +PureYAML intentionally does not expose arbitrary `Any` dictionary-and-array load +or dump APIs. Use `Model.Value` for unknown schemas and typed `Codable` for +known schemas. + ## Model Usage Use `PureYAML.Model.Value` when the caller needs to inspect YAML structure, @@ -68,6 +72,11 @@ Mappings preserve insertion order and retain duplicate keys. That is deliberate: validation can report duplicate-key diagnostics instead of losing information by collapsing a mapping into a Swift dictionary too early. +If a caller needs a JSON-like dictionary after validation, build that projection +in application code after deciding how duplicate keys, unsupported tags, merge +keys, and complex keys should behave. Keeping that step outside PureYAML makes +the lossy boundary explicit. + ## Typed Conversion Use typed conversion when the YAML shape is already known. Current coverage