Skip to content

fix(E1011): emit compact JSON from the Fn::ToJsonString resolver - #4972

Merged
kddejong merged 1 commit into
aws-cloudformation:mainfrom
kddejong:fix/tojsonstring-compact
Nov 2, 2026
Merged

kddejong merged 1 commit into
aws-cloudformation:mainfrom
kddejong:fix/tojsonstring-compact

Conversation

@kddejong

Copy link
Copy Markdown
Contributor

Problem

Fn::ToJsonString is resolved with a bare json.dumps, which inserts a space after : and ,:

yield json.dumps(value), v, err

CloudFormation renders Fn::ToJsonString compactly, with no whitespace after : or ,. So whenever cfn-lint compares its resolved string against a literal the user wrote to match CloudFormation's real output, the two disagree.

The visible symptom is a spurious E1011. A Fn::FindInMap second-level key produced by Fn::ToJsonString cannot be found, because cfn-lint looks up the spaced form while the template (correctly) declares the compact one:

E1011 '{"To": "Json", "String": "Function"}' is not one of ['{"To":"Json","String":"Function"}'] for mapping 'MapName' and key 'TopKey1'

Minimal reproducer — valid, deployable, and rejected before this change:

Transform: AWS::LanguageExtensions
Mappings:
  MapName:
    TopKey1:
      '{"To":"Json","String":"Function"}': mybucketname123
Resources:
  Bucket1:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !FindInMap
        - MapName
        - TopKey1
        - Fn::ToJsonString:
            To: Json
            String: Function

Fix

-        yield json.dumps(value), v, err
+        yield json.dumps(value, separators=(",", ":")), v, err

sort_keys is deliberately not used. The language-extensions RFC guarantees the opposite: "Conversion will always retain the same order of key-value pairs such that the converted strings of the same input template block are guaranteed to not change." Sorting would be a second, unwanted behaviour change.

Evidence for compact output

  • Deployment (us-east-1, 2026-10-28). Two create-stack probes differing only in a mapping key's spacing. The spaced form failed with Mappings not found in template for key /TopKey1/{"To":"Json","String":"Function"} — CloudFormation naming back the compact string it had produced. The compact form deployed.
  • A template already in this repo. test/fixtures/templates/good/functions_findinmap_enhanced.yaml declares that mapping key in compact form, i.e. it was written to match CloudFormation rather than to match cfn-lint.
  • The RFC's own example (0014-ToJsonString) shows the hand-written equivalent of a Fn::ToJsonString block as "{\"start\":\"-PT6H\",\"periodOverride\":\"inherit\",...}" — compact.

Scope

Fn::ToJsonString is serialised in exactly one place. grep -rn 'json.dumps' src/cfnlint/ returns 17 hits; the other 16 are unrelated and were left alone:

  • helpers.format_json_string, formatters/json.py, graph.py — output rendering, not function resolution.
  • helpers.get_hash, conditions/_utils.py, conditions/_equals.py, _language_extensions.py:237 — hash/sort keys, never compared against template text.
  • _keywords.py:628 (pattern), EqualsIsUseful, Split, EventsLogGroupName — unrelated instances.
  • FunctionEnvironmentSize and StringLength already pass separators=(",", ":").

template/transforms/_language_extensions.py matches Fn::ToJsonString but deliberately returns the block unresolved, so it serialises nothing.

Verification

Run from a worktree with PYTHONPATH=<tree>/src and schemas refreshed via cfn-lint --update-specs (schema_date 2026-10-31T16:10:54Z).

Reproducer — before: the E1011 above. After: gone (the unrelated E7001 on the non-alphanumeric mapping key is pre-existing on main and untouched).

Resolver, before → after:

input before after
{"A": "B", "C": "D"} {"A": "B", "C": "D"} {"A":"B","C":"D"}
{"A": {"B": "C"}} {"A": {"B": "C"}} {"A":{"B":"C"}}
["A", "B"] ["A", "B"] ["A","B"]
{"A": "B C"} {"A": "B C"} {"A":"B C"} (inner space kept)
{"A": 1, "B": true, "C": null} {"A": 1, "B": true, "C": null} {"A":1,"B":true,"C":null}
{"Fn::GetAtt": "MyResource.Arn"} unresolved unresolved

Fixture corpus — no change, measured against a noise floor. good/ + bad/, YAML and JSON, with -c I: 3159 output lines, 1051 findings. main was run against itself three times first, because E3019 renders Python sets and E3043 renders condition order into prose; that floor was 32–80 diff lines. main vs this branch is 64 diff lines, and every differing line is E3019 or E3043. Order-insensitive checks agree exactly: the rule-id multiset and the file:line multiset are byte-identical across all five runs, and dropping those two rules' message lines makes the sorted output identical.

Tests. Six new cases in test/unit/module/jsonschema/test_resolvers_cfn.py::test_to_json_string covering a dict, a nested dict, an array, a string value with an internal space, non-string scalars, and an unresolvable Fn::GetAtt. Mutation-checked: reverting separators fails 5 of the 6 (the unresolvable case is separator-independent, as expected).

Full suite. Identical failure set before and after — the same 6 pre-existing failures from missing optional extras (pydot, junit-xml, sarif), with passes 3328 → 3334 (the six new cases). pre-commit run --all-files passes.

Notes

  • Does not overlap fix(W1040): report resolved Fn::ToJsonString errors under W1040 #4969, which touches rules/functions/ToJsonString.py and its test only. That one changes which rule reports a resolved-value error; this one changes the string produced. They compose.
  • test/fixtures/templates/good/functions_findinmap_enhanced.yaml currently reports E7001 because cfn-lint requires second-level mapping keys to match ^[a-zA-Z0-9]+$, which CloudFormation does not. Pre-existing and out of scope here.

CloudFormation renders Fn::ToJsonString without whitespace after ':' or
',', so a resolved value compared against a template literal (most visibly
a Fn::FindInMap second-level key written to match CloudFormation's output)
did not match and reported a spurious E1011.

Key order is left alone: the language-extensions RFC guarantees
Fn::ToJsonString preserves the template's key order.
@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.75%. Comparing base (b17a430) to head (51324d9).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4972      +/-   ##
==========================================
+ Coverage   94.74%   94.75%   +0.01%     
==========================================
  Files         442      442              
  Lines       16105    16105              
  Branches     3125     3125              
==========================================
+ Hits        15258    15260       +2     
+ Misses        468      466       -2     
  Partials      379      379              
Flag Coverage Δ
unittests 94.74% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kddejong
kddejong marked this pull request as ready for review November 2, 2026 16:29
@kddejong
kddejong merged commit 4c7581a into aws-cloudformation:main Nov 2, 2026
24 checks passed
@kddejong
kddejong deleted the fix/tojsonstring-compact branch November 2, 2026 16:29
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