Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newline string literals produce invalid marshaling output #1004

Open
Alfus opened this issue Nov 18, 2023 · 2 comments
Open

Newline string literals produce invalid marshaling output #1004

Alfus opened this issue Nov 18, 2023 · 2 comments

Comments

@Alfus
Copy link

Alfus commented Nov 18, 2023

If keys or values of a map node are the string literal "\n", the encoder has odd output.

func TestYamlNewLineMaps(t *testing.T) {
	value := map[string]string{
		"\n": "\n",
	}
	data, err := yaml.Marshal(value)
	if err != nil {
		t.Fatal(err)
	}
	// Should be `"\n": "\n"`, but its garbled.
	if string(data) != "? |4+\n: |4+\n" {
		t.Fatalf("Expected garbled output, got %s", string(data))
	}
}

The output is:

? |4+
: |4+

Which doesn't appear to be valid yaml. When it should be:

"\n": "\n"
Alfus added a commit to bufbuild/protoyaml-go that referenced this issue Nov 18, 2023
And disable "\n" string tests for now (see
go-yaml/yaml#1004)

Previously, negative durations with nanos were producing invalid values,
which wasn't caught because the round trip tests didn't actually check
the results of the diff :-(.
@dolmen
Copy link

dolmen commented Mar 12, 2024

Here is a variation related to serialization of \n (on the Go Playground):

func TestNewLine(t *testing.T) {
	const orig = "\n"
	data, err := yaml.Marshal("\n")
	if err != nil {
		t.Fatal(err)
	}
	var s string
	err = yaml.Unmarshal(data, &s)
	if err != nil {
		t.Fatal(err)
	}
	if s != orig {
		t.Fatalf("got %q, expected %q", s, orig)
	}
}
=== RUN   TestNewLine
    prog_test.go:21: got "", expected "\n"
--- FAIL: TestNewLine (0.00s)
FAIL

@dolmen
Copy link

dolmen commented Mar 12, 2024

Related: #963, #964

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

No branches or pull requests

2 participants