Skip to content

Treat a header with no keys as empty so AddFrontmatter fills it [patch] - #207

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/206-empty-header-add
Oct 20, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/206-empty-header-add

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #206

What was wrong

YamlSerializer.TryParseYamlObject returns false both when a block fails to parse and when it parses to nothing (# TODO, ~, null) or to an empty mapping ({}). ExtractFrontmatterObjects set hasUnreadableBlock for either case. As a result, AddFrontmatter returned placeholder-header documents unchanged, which silently dropped the caller's properties, and CombineFrontmatter would not combine a real block stacked beside an empty one.

Change

  • New internal YamlSerializer.IsEmptyYamlDocument. It deserializes the block as object and reports true for null or an empty mapping.
  • ExtractFrontmatterObjects sets hasUnreadableBlock only when a block fails TryParseYamlObject and is not empty. Empty blocks are skipped, the same way whitespace-only blocks already were.
  • The public TryParseYamlObject contract is unchanged: it still returns false for an empty document, and the existing TryParseYamlObject_EmptyYamlDocument_ReturnsFalse test still pins that.

Tests (EmptyYamlBlockTests)

  • AddFrontmatter_EmptyYamlBlock_AddsTheProperties covers # TODO: fill in, ~, null and {}. Each now yields title: Hello and keeps the body. This replaces AddFrontmatter_EmptyYamlBlock_LeavesTheDocumentUnchanged, which pinned the behaviour the issue reports as a bug.
  • CombineFrontmatter_EmptyBlockStackedOnARealOne_CombinesThem covers # c, ~ and {} stacked on title: a, and expects a single header.
  • AddFrontmatter_UnreadableBlock_LeavesTheDocumentUnchanged checks that key: [unclosed, a bare scalar and a sequence root are still left alone.
  • With the Frontmatter.cs change reverted, all 7 new add and combine cases fail. With the change, the full suite passes, 313/313 locally on net10.0.

#180 step 1 will rewrite this parser. These tests are written against the public API, so they should carry over unchanged.


Generated by Claude Code

A header that parses but holds no keys (a comment only, ~, null or {})
was reported as unreadable, so AddFrontmatter returned its input unchanged
and CombineFrontmatter would not combine a stacked block beside it. Such a
block is now skipped as empty; only a parse failure or a non-mapping root
still leaves the document alone.

The test that pinned the old AddFrontmatter behaviour is rewritten to the
behaviour the issue asks for.

Fixes #206

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UFB6HgHkLg58aFnXW56N7C
The two catches no test reaches left the new code under the 80% coverage
gate; one filter keeps the same exceptions and the same answer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UFB6HgHkLg58aFnXW56N7C
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AddFrontmatter silently drops new properties when the existing header holds only a comment, ~, null or {}

2 participants