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

[Tests] Escaped characters in YAML keys break encoding/decoding #418

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2890,3 +2890,117 @@ func TestSameNameInineStruct(t *testing.T) {
t.Fatalf("failed to decode")
}
}

// Test newline in key as encoded by alternative YAML tools:
func TestUnmarshalNewlineInKey(t *testing.T) {
tests := []struct {
name string
spec string
}{
{
name: "multiline-key-with-newline-01",
/* As rendered by this library from this JSON:
{
"a": "a",
"c\nc": "cc",
"d": "d"
}
*/
spec: `a: a
|-
c
c: cc
d: d
`,
},
{
name: "quoted-key-with-newline",
spec: `a: a
"c\nc": "cc"
d: d
`,
},
{
name: "multiline-key-with-newline-02",
// As encoded by alternative online YAML tools (e.g. https://www.json2yaml.com/)
spec: `a: a
? |-
c
c
: cc
d: d
`,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
decoder := yaml.NewDecoder(strings.NewReader(test.spec))
var out map[string]interface{}
for i := 0; ; i++ {
err := decoder.Decode(&out)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Error on iteration %d: %v", i, err)
}
}
})
}
}

func TestUnmarshalBackslashInKey(t *testing.T) {
spec := `outer:
"b\\b": b
d : d
`

decoder := yaml.NewDecoder(strings.NewReader(spec))
var out map[string]interface{}
for i := 0; ; i++ {
err := decoder.Decode(&out)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Error on iteration %d: %v", i, err)
}
}
}

func TestUnmarshalQuotesInKey(t *testing.T) {
spec := `outer:
"a\"b\"c": a
"d\"e\"f": d
`
decoder := yaml.NewDecoder(strings.NewReader(spec))
var out map[string]interface{}
for i := 0; ; i++ {
err := decoder.Decode(&out)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Error on iteration %d: %v", i, err)
}
}
}

func TestUnmarshalCollectedEscapesInKey(t *testing.T) {
spec := `outer:
"a\"b\"c\\d\ne": a
"d\"e\"f": d
`
decoder := yaml.NewDecoder(strings.NewReader(spec))
var out map[string]interface{}
for i := 0; ; i++ {
err := decoder.Decode(&out)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Error on iteration %d: %v", i, err)
}
}
}
33 changes: 33 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math"
"reflect"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -1630,3 +1631,35 @@ b:
t.Fatalf("failed to encode. expected %s but got %s", expected, got)
}
}

func TestMarshalNewlineInKey(t *testing.T) {
spec := map[string]string{
"a": "a",
"c\nc": "cc",
"d": "d",
}
expected := []string{
`a: a
"c\nc": "cc"
d: d
`,
`---
a: a
? |-
c
c
: cc
d: d
`}
y, err := yaml.Marshal(spec)
if err != nil {
t.Fatalf("Error marshalling YAML: %v", err)
}
for _, exp := range expected {
if string(y) == exp {
return
}
}

t.Fatalf("testNewlineEncode FAILED\nExpected: '%s'\nGot: '%s'", strings.Join(expected, "\nor\n"), string(y))
}
Loading