Repository navigation
fix(E1011): emit compact JSON from the Fn::ToJsonString resolver - #4972
Merged
kddejong merged 1 commit intoNov 2, 2026
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fn::ToJsonStringis resolved with a barejson.dumps, which inserts a space after:and,:CloudFormation renders
Fn::ToJsonStringcompactly, 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. AFn::FindInMapsecond-level key produced byFn::ToJsonStringcannot be found, because cfn-lint looks up the spaced form while the template (correctly) declares the compact one:Minimal reproducer — valid, deployable, and rejected before this change:
Fix
sort_keysis 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
create-stackprobes differing only in a mapping key's spacing. The spaced form failed withMappings not found in template for key /TopKey1/{"To":"Json","String":"Function"}— CloudFormation naming back the compact string it had produced. The compact form deployed.test/fixtures/templates/good/functions_findinmap_enhanced.yamldeclares that mapping key in compact form, i.e. it was written to match CloudFormation rather than to match cfn-lint.Fn::ToJsonStringblock as"{\"start\":\"-PT6H\",\"periodOverride\":\"inherit\",...}"— compact.Scope
Fn::ToJsonStringis 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.FunctionEnvironmentSizeandStringLengthalready passseparators=(",", ":").template/transforms/_language_extensions.pymatchesFn::ToJsonStringbut deliberately returns the block unresolved, so it serialises nothing.Verification
Run from a worktree with
PYTHONPATH=<tree>/srcand schemas refreshed viacfn-lint --update-specs(schema_date 2026-10-31T16:10:54Z).Reproducer — before: the
E1011above. After: gone (the unrelatedE7001on the non-alphanumeric mapping key is pre-existing onmainand untouched).Resolver, 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"}Fixture corpus — no change, measured against a noise floor.
good/+bad/, YAML and JSON, with-c I: 3159 output lines, 1051 findings.mainwas run against itself three times first, becauseE3019renders Python sets andE3043renders condition order into prose; that floor was 32–80 diff lines.mainvs this branch is 64 diff lines, and every differing line isE3019orE3043. Order-insensitive checks agree exactly: the rule-id multiset and thefile:linemultiset 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_stringcovering a dict, a nested dict, an array, a string value with an internal space, non-string scalars, and an unresolvableFn::GetAtt. Mutation-checked: revertingseparatorsfails 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-filespasses.Notes
rules/functions/ToJsonString.pyand 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.yamlcurrently reportsE7001because cfn-lint requires second-level mapping keys to match^[a-zA-Z0-9]+$, which CloudFormation does not. Pre-existing and out of scope here.