fix: discard leading comment/whitespace preamble in parseYaml stream#867
Open
ricardbejarano wants to merge 1 commit intogoogle:masterfrom
Open
fix: discard leading comment/whitespace preamble in parseYaml stream#867ricardbejarano wants to merge 1 commit intogoogle:masterfrom
ricardbejarano wants to merge 1 commit intogoogle:masterfrom
Conversation
When a YAML document stream begins with comment lines or blank lines before the first '---' document-start marker, the YAMLReader would accumulate those lines in its buffer and emit them as a document when the marker was encountered. yaml.Unmarshal of comment-only content returns nil, resulting in a spurious null element at the start of the parsed array. Per YAML spec §9.2, comments and blank lines that appear before the first document-start marker are preamble and do not form a document. Add isCommentOrWhitespace to detect this case and discard the buffer instead of returning it as a document. Fixes google#660 Signed-off-by: Tommy <tommy@bejara.net>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #660.
When a YAML document stream begins with comment lines or blank lines before the first
---document-start marker,std.parseYamlincorrectly emits a spuriousnullas the first element of the result array.Root cause:
YAMLReader.read()accumulates all input lines into a buffer until it encounters a---marker. At that point, if the buffer is non-empty it returns the buffered bytes as a document and resets. When those bytes contain only YAML comments (#...) or blank lines,yaml.Unmarshalyieldsnil, which is then appended asnullto the result.Fix: Introduce
isCommentOrWhitespaceto detect when the accumulated buffer contains only comments and blank lines (valid YAML preamble per §9.2 of the YAML spec). When that is the case, discard the buffer rather than returning it as a document.Before:
After:
Changes
yaml.go: addisCommentOrWhitespacehelper; skip preamble-only buffer inYAMLReader.read()testdata/parseYaml.jsonnet: add two regression test cases (single leading comment; multiple comments + blank lines)testdata/parseYaml.golden: update expected output accordinglyAll existing tests continue to pass (
go test ./...).