Skip to content

Commit

Permalink
Raise error if any key in a mapping is null (#3073)
Browse files Browse the repository at this point in the history
* Null keys will raise an error
* Raise errors when a key in an object is None
  • Loading branch information
kddejong authored Feb 26, 2024
1 parent 4a3dd98 commit 6836533
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/cfnlint/decode/cfn_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ def construct_yaml_map(self, node):
key = self.construct_object(key_node, False)
value = self.construct_object(value_node, False)

if key is None:
raise CfnParseError(
self.filename,
[
build_match(
filename=self.filename,
message=f"Null key {key_node.value!r} not supported (line {key_node.start_mark.line + 1})",
line_number=key_node.start_mark.line,
column_number=key_node.start_mark.column,
key=key_node.value,
),
],
)
for key_dup in mapping:
if key_dup == key:
if not matches:
Expand Down
15 changes: 14 additions & 1 deletion test/unit/module/decode/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import yaml
from yaml.scanner import ScannerError

import cfnlint.decode.decode # pylint: disable=E0401
import cfnlint.decode.cfn_json
import cfnlint.decode.cfn_yaml
import cfnlint.decode.decode


class TestDecode(BaseTestCase):
Expand Down Expand Up @@ -125,3 +127,14 @@ def test_decode_yaml_error(self, mock_cfn_yaml):
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].rule.id, "E0000")
self.assertEqual(matches[0].message, err_msg)

def test_decode_yaml_null_key(self):
err_msg = "Null key 'null' not supported (line 3)"
with self.assertRaises(cfnlint.decode.cfn_yaml.CfnParseError) as e:
cfnlint.decode.cfn_yaml.loads(
"""
Parameters:
null: test
"""
)
self.assertEqual(str(e.exception), err_msg)
File renamed without changes.

0 comments on commit 6836533

Please sign in to comment.