From 5d76d184cb12c59c9787c9b482fd918605481729 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Tue, 23 Jun 2026 03:58:12 +0200 Subject: [PATCH] fix(parsing): treat leading '#' in block scalars as literal content --- CHANGELOG.md | 9 ++++ Sources/Parsing/Scanner.Indentation.swift | 9 +++- Tests/ParsingMultilineScalarTests.swift | 54 +++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcbc094..9c7f388 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.1.3] - 2026-06-23 + +### Fixed + +- Parse a leading `#` in block scalar content (`>`, `>-`, `|`, `|-`) as literal + text instead of a comment. A block scalar whose first content line began with + `#`, such as an OpenAPI `$ref: >-` folded onto `#/components/...`, was + previously dropped to an empty string. + ## [0.1.2] - 2026-06-06 ### Added diff --git a/Sources/Parsing/Scanner.Indentation.swift b/Sources/Parsing/Scanner.Indentation.swift index 58f4733..85573cc 100644 --- a/Sources/Parsing/Scanner.Indentation.swift +++ b/Sources/Parsing/Scanner.Indentation.swift @@ -54,7 +54,14 @@ extension PureYAML.Parsing.Scanner { guard !state.reader.isAtEnd, !isLineBreak(state.reader.peek()) else { return false } - guard state.reader.peek() != "#" else { + // A leading '#' normally starts a comment line, which must not drive + // indent/dedent processing. Inside a block scalar it is different: when + // the header is still pending or the content is active, a leading '#' is + // literal scalar content, so indentation must be processed so the block + // scalar's indentation resolves and its content lines are consumed. + // Bailing here would leave `blockScalarIndentation` unset and the main + // loop would mis-scan the line as a comment, dropping the content. + if state.reader.peek() == "#", !state.pendingBlockScalarIndentation, state.blockScalarIndentation == nil { return false } return state.flowDepth == 0 diff --git a/Tests/ParsingMultilineScalarTests.swift b/Tests/ParsingMultilineScalarTests.swift index a0a9e5d..d11a654 100644 --- a/Tests/ParsingMultilineScalarTests.swift +++ b/Tests/ParsingMultilineScalarTests.swift @@ -196,4 +196,58 @@ struct ParsingMultilineScalarTests { """, )) } + + @Test("Block scalars keep a leading '#' as literal content, not a comment") + func test_blockScalarLeadingHashIsLiteralContent() throws { + let root = try requireMapping(PureYAML.parse( + """ + folded: + $ref: >- + #/components/schemas/foo + literal: + $ref: |- + #/components/schemas/bar + """, + )) + + #expect(root?.mapping("folded")?["$ref"] == .string("#/components/schemas/foo")) + #expect(root?.mapping("literal")?["$ref"] == .string("#/components/schemas/bar")) + } + + @Test("A leading '#' is preserved across every line of a block scalar") + func test_blockScalarLeadingHashAcrossMultipleLines() throws { + let root = try requireMapping(PureYAML.parse( + """ + doc: |- + #/components/schemas/foo + plain line + #/components/schemas/bar + after: tail + """, + )) + + #expect(root?["doc"] == .string( + """ + #/components/schemas/foo + plain line + #/components/schemas/bar + """, + )) + // The block scalar ends at the dedent; the following key is unaffected. + #expect(root?["after"] == .string("tail")) + } + + @Test("A genuine trailing comment after a plain value is still ignored") + func test_plainValueTrailingCommentStillIgnored() throws { + let root = try requireMapping(PureYAML.parse( + """ + kind: widget # this is a comment + # whole-line comment + count: 3 + """, + )) + + #expect(root?["kind"] == .string("widget")) + #expect(root?["count"] == .int(3)) + } }