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

Fix #653: incorrectly strips quotes from string values that resemble YAML booleans #654

Merged
merged 1 commit into from
Mar 4, 2025
Merged
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
105 changes: 105 additions & 0 deletions cft/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,108 @@ Outputs {
t.Fatalf("Got:\n[%s]\nExpected:\n[%s]\n", output, expected)
}
}

func TestAmbiguousScalarOn(t *testing.T) {
input := `
Resources:
MyResource:
Type: AWS::RDS::DBClusterParameterGroup
Properties:
Parameters:
require_secure_transport: "ON"
`
// We expect the ambiguous value "ON" to remain quoted in the output.
expected := `Resources:
MyResource:
Type: AWS::RDS::DBClusterParameterGroup
Properties:
Parameters:
require_secure_transport: "ON"
`
template, err := parse.String(input)
if err != nil {
t.Fatal(err)
}

actual := format.String(template, format.Options{Unsorted: true})
if d := cmp.Diff(strings.TrimSpace(expected), strings.TrimSpace(actual)); d != "" {
t.Fatalf("Diff: %s", d)
}
}

func TestAmbiguousScalarsInParameters(t *testing.T) {
input := `
Parameters:
Param1:
Default: "ON"
Param2:
Default: "OFF"
Param3:
Default: "Yes"
Param4:
Default: "No"
Param5:
Default: "True"
Param6:
Default: "False"
Param7:
Default: "Maybe"
`
// The ambiguous values (Param1 through Param6) should be rendered with quotes,
// while a non-ambiguous value (Param7) may be unquoted.
expected := `Parameters:
Param1:
Default: "ON"

Param2:
Default: "OFF"

Param3:
Default: "Yes"

Param4:
Default: "No"

Param5:
Default: "True"

Param6:
Default: "False"

Param7:
Default: Maybe
`
template, err := parse.String(input)
if err != nil {
t.Fatal(err)
}

actual := format.String(template, format.Options{Unsorted: true})
if d := cmp.Diff(strings.TrimSpace(expected), strings.TrimSpace(actual)); d != "" {
t.Fatalf("Diff: %s", d)
}
}

func TestNonAmbiguousScalar(t *testing.T) {
input := `
Resources:
MyResource:
Properties:
example_value: "OnX"
`
// Since "OnX" is not an ambiguous token, it can be rendered without quotes.
expected := `Resources:
MyResource:
Properties:
example_value: OnX
`
template, err := parse.String(input)
if err != nil {
t.Fatal(err)
}

actual := format.String(template, format.Options{Unsorted: true})
if d := cmp.Diff(strings.TrimSpace(expected), strings.TrimSpace(actual)); d != "" {
t.Fatalf("Diff: %s", d)
}
}
18 changes: 16 additions & 2 deletions cft/format/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,25 @@ func formatNode(n *yaml.Node) *yaml.Node {
n.Style = yaml.DoubleQuotedStyle
}
case "":
// Default style for consistent formatting
n.Style = 0
// Default style for consistent formatting:
// Force double quotes on ambiguous scalar strings.
if n.Kind == yaml.ScalarNode && n.Tag == "!!str" && isAmbiguousScalar(n.Value) {
n.Style = yaml.DoubleQuotedStyle
} else {
n.Style = 0
}
default:
panic("invalid --node-style: " + NodeStyle)
}

return n
}

func isAmbiguousScalar(val string) bool {
switch strings.ToLower(val) {
case "y", "yes", "n", "no", "true", "false", "on", "off":
return true
default:
return false
}
}