PureYAML/Sources/Emitting/Dumper.Stream.swift
Mihaela Mihaljevic 2fcf54684e fix: correct emitter and tagged round-trip and validation bugs
Seven correctness fixes to the recently landed anchors/styles, canonical,
timestamp/binary, and full-node-key work, each reproduced and covered by a
regression test.

- Stream dump doubled the `---` marker under explicitDocumentStart or
  canonical: the single-document body now renders marker-free and the
  stream path owns marker emission, so a two-document stream reparses as
  two documents, not four.
- Tagged flow-mapping keys used block-context safety, so a key like `a,b`
  emitted bare and reparsed as several entries; flow keys now use the
  flow-aware plain-scalar check.
- Plain scalars ending in `:` reparse as an empty-valued mapping key, so
  both the model and tagged emit paths now fall back to quoting them.
- An alias mapping key pointing at a defined non-scalar anchor raised the
  misleading undefinedAlias; it now raises expectedScalarKey, and a truly
  undefined alias key still raises undefinedAlias.
- Tagged duplicate-key detection keyed on source spelling: it now keys on
  the resolved scalar value plus effective tag, so 0x1 and 1 collide while
  1 and "1" do not and custom-tagged keys stay distinct.
- Dumping a tree nested past the emitter limit overflowed the stack.

- Timestamp now accepts a leap-second value of 60, counted as 60 elapsed
  seconds in the epoch math.
- Tagged.Dumper honors sortKeys in its block and canonical paths for
  all-plain-string-key mappings, matching the model dumper.

Nesting-guard design: dump is public and non-throwing across 90+ call
sites, so making it throwing would be a breaking change. A tree nested
deeper than the parser's own maxNestingDepth cannot round-trip through
PureYAML (the parser rejects it), so no faithful emission exists. Each
dump entry point now measures depth iteratively (no recursion, so the
guard itself cannot overflow) and, past the limit, returns a documented,
reparseable marker document that resolves to null. Under the limit the
existing recursive renderers run byte-identically, so parsed trees and
every realistic hand-built tree are unaffected. This mirrors the parser's
enforceNestingLimit, which bounds depth before any recursive consumer.
2026-07-16 16:29:08 +02:00

20 lines
961 B
Swift

extension PureYAML.Emitting.Dumper {
/// Serializes a stream of documents. The stream path owns the document
/// markers: it emits one `---` start per document and, when
/// `explicitDocumentEnd` is set, one `...` end per document. Each document
/// body is rendered marker-free so `explicitDocumentStart` and `canonical`
/// cannot stack a second `---` onto the separator, which previously turned
/// every document into two (an empty one plus the real one) on reparse.
func dump(_ documents: [PureYAML.Stream.Document]) -> String {
documents
.map { document in
let body = PureYAML.Emitting.exceedsNestingLimit(document.value)
? PureYAML.Emitting.nestingLimitDocument
: documentBody(document.value)
let end = options.explicitDocumentEnd ? "...\n" : ""
return "---\n" + body + end
}
.joined()
}
}