Skip to content

Fix parser accepting falsy non-mapping frontmatter - #38

Open
sumitsharma01 wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
sumitsharma01:fix/reject-non-mapping-frontmatter
Open

sumitsharma01 wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
sumitsharma01:fix/reject-non-mapping-frontmatter

Conversation

@sumitsharma01

Copy link
Copy Markdown

OKFDocument.parse expects YAML frontmatter to be a mapping. However, it currently accepts false, 0, [], and '' and converts them into an empty mapping instead of raising an error.

This happens because the parser uses yaml.load(...) or {}. These values are falsy in Python, so they are replaced with {} before the mapping check runs. Other non-mapping values already raise OKFDocumentError.

The fix replaces only None with an empty mapping. This preserves the existing behavior for empty, comment-only, and null frontmatter, while allowing the mapping check to reject invalid scalar and sequence values consistently.

Added parameterized tests for the invalid values and for the empty frontmatter cases. Four regression cases failed before the fix. With the fix applied, all 50 tests pass using .venv/bin/pytest, and git diff --check passes.

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.

1 participant