goccy / go-yaml

YAML support for the Go language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parser returns two documents instead of one

andrewkroh opened this issue · comments

The documents below both cause parser.ParseBytes to return an ast.File` that contains two documents. In each case it should only return a single document. I have included a test case that reproduces the issue.

---
script:
  line1

  line3
---
config:
  options:
# Comment
    mode: full

Playground: https://go.dev/play/p/Q62ku79uAzr

Test case:

package main

import (
	"testing"

	"github.com/goccy/go-yaml/parser"
)

func TestUnexpectedYAMLDocCountParsing(t *testing.T) {
	testCases := []struct {
		name string
		yaml string
	}{
		// Real examples:
		//  https://github.com/elastic/integrations/blob/5e886328f00bfd1639a61920e1ace60e9d7cc50a/packages/infoblox_nios/data_stream/log/elasticsearch/ingest_pipeline/pipeline_dns.yml#L80
		//  https://github.com/elastic/integrations/blob/5e886328f00bfd1639a61920e1ace60e9d7cc50a/packages/fortinet_fortigate/data_stream/log/elasticsearch/ingest_pipeline/default.yml#L53
		{
			name: "newline in multi-line string",
			yaml: `
---
script:
  line1

  line3
`,
		},
		// Real example https://github.com/elastic/integrations/blob/5e886328f00bfd1639a61920e1ace60e9d7cc50a/packages/zeronetworks/data_stream/audit/elasticsearch/ingest_pipeline/default.yml#L92
		{
			name: "comment without indentation",
			yaml: `
---
config:
  options:
# Comment
    mode: full
`,
		},
	}

	for _, tc := range testCases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			f, err := parser.ParseBytes([]byte(tc.yaml), parser.ParseComments)
			if err != nil {
				t.Fatal(err)
			}

			if len(f.Docs) != 1 {
				t.Fatalf("expected doc count 1, got %d", len(f.Docs))
			}
		})
	}
}