Skip to content

jsonencode is not deserialized correctly #250

@mcasperson

Description

@mcasperson

Describe the bug

The strings in an array passed to jsonencode are deserialized incorrectly.

Software:

  • OS: Linux
  • Python version 3.11
  • python-hcl2 version 7.3.1

Snippet of HCL2 code causing the unexpected behaviour:

resource "octopusdeploy_process_templated_step" "process_step_octopub_scan_for_vulnerabilities" {
  parameters            = {
      "Octopus.Action.Aws.IamCapabilities" = jsonencode([
        "CAPABILITY_AUTO_EXPAND",
        "CAPABILITY_IAM",
        "CAPABILITY_NAMED_IAM",
        ])
      }
}

Expected behavior

This script should retain the values of the array as it is deserialized and serialized:

import hcl2

hcl = """resource "octopusdeploy_process_templated_step" "process_step_octopub_scan_for_vulnerabilities" {
  parameters            = {
    "Octopus.Action.Aws.IamCapabilities" = jsonencode([
        "CAPABILITY_AUTO_EXPAND",
        "CAPABILITY_IAM",
        "CAPABILITY_NAMED_IAM",
        ])
      }
}
"""

parsed_config = hcl2.loads(hcl, with_meta=True)
example_ast = hcl2.reverse_transform(parsed_config)
converted_hcl = hcl2.writes(example_ast)

print(converted_hcl)

parsed_config2 = hcl2.loads(converted_hcl, with_meta=True)

print(parsed_config2)

This is the output of the script above;

resource "octopusdeploy_process_templated_step" "process_step_octopub_scan_for_vulnerabilities" {
  parameters = {
    Octopus.Action.Aws.IamCapabilities = jsonencode([CAPABILITY_AUTO_EXPAND, CAPABILITY_IAM, CAPABILITY_NAMED_IAM])
  }
}

{'resource': [{'octopusdeploy_process_templated_step': {'process_step_octopub_scan_for_vulnerabilities': {'parameters': {'Octopus.Action.Aws.IamCapabilities': '${jsonencode([${CAPABILITY_AUTO_EXPAND}, ${CAPABILITY_IAM}, ${CAPABILITY_NAMED_IAM}])}'}, '__start_line__': 1, '__end_line__': 5}}}]}

You can see from the output that the strings in the array have been wrapped up in the ${} syntax.

Metadata

Metadata

Labels

bugSomething isn't workingfixed-in-8.0this issue has been fixed in `transformer-overhaul` feature branch, to be released as v8.0

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions