Skip to content

[v2] Use custom YAML emitter for CloudFormation dumps#10164

Open
hssyoo wants to merge 1 commit intov2from
cfn-yaml
Open

[v2] Use custom YAML emitter for CloudFormation dumps#10164
hssyoo wants to merge 1 commit intov2from
cfn-yaml

Conversation

@hssyoo
Copy link
Contributor

@hssyoo hssyoo commented Mar 25, 2026

Addresses #10156

Context

ruamel.yaml version 0.17.23 added a heuristic to determine when adding backslashes in wrapped quoted strings is necessary changelog. This seems technically compliant but downstream consumers with a different YAML parser may not be compatible with the change. This PR fixes the issue by creating a custom Emitter class that overrides the method responsible for writing double quoted values, restoring the pre-0.17.23 behavior, unconditionally adding backslashes.

Considered but dropped

  • We could have instead yaml.width to an impossibly high value like float('inf') so that there are never line breaks. Without line breaks, there wouldn't be a need for the backslashes. This was not pursued because it may produce extremely long lines that may cause buffering issues for consumers when reading line-by-line. It also changes the output behavior from previous AWS CLI versions and consumers may be depending on consistent formatting.
  • It would've been nice to have a test that catches drift from source YAML emitter to the custom emitter, but because AWS CLI supports a range of ruamel.yaml versions instead of pinning a specific version, it wouldn't make sense. I'm also not sure in general how valuable it is to limit drift if the emitter works fine for the AWS CLI's use case.

Confirmed that the original output is restored given the test template in the referenced issue:

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  EnvName:
    Type: String
    Default: test
Resources:
  TestResource:
    Type: AWS::SSM::Parameter
    Properties:
      Name:
        Fn::Sub: ${EnvName}-test
      Type: String
      Value:
        Fn::Sub:
        - "template: |\n  {{ define \"test.message\" }}\n  {{- $a := index .Alerts\
          \ 0 -}}\n  {{- $envName := printf \"%s\" \"${EnvName}\" -}}\n  {{- $queryExprClean\
          \ := \"\" }}\n  {{- $queryExprTemplated := reReplaceAll \"\\\\]\" \"%5D\"\
          \ (\n      reReplaceAll \"\\\\[\" \"%5B\" (\n      reReplaceAll \"=\" \"\
          %3D\" (\n      reReplaceAll \"\\\\}\" \"%7D\" (\n      reReplaceAll \"\\\
          \\{\" \"%7B\" (\n      reReplaceAll \">\" \"%3E\" (\n      reReplaceAll\
          \ \"<\" \"%3C\" (\n      reReplaceAll \" \" \"%20\" (\n      reReplaceAll\
          \ \"\\\"\" \"%22\" (\n      reReplaceAll \"'\" \"%27\" (\n        $queryExprClean\n\
          \      )))))))))) }}\n  {{- $desc := printf \"%s\\nEnvironment: %s\\nState\
          \ Change Time: %s\\nNext Steps:\\n- Upstream URL: %s\\n- %s\" $descClean\
          \ $envName ($a.StartsAt.Format \"Mon, 02 Jan 2006 15:04:05 MST\") $grafanaUrl\
          \ $formattedSteps -}}\n  {{ end }}\n"
        - {}

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

Successfully merging this pull request may close these issues.

1 participant