Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overly-eager jsonencode #35984

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions internal/genconfig/generate_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ func writeConfigAttributesFromExisting(addr addrs.AbsResourceInstance, buf *stri
// If the value is a string storing a JSON value we want to represent it in a terraform native way
// and encapsulate it in `jsonencode` as it is the idiomatic representation
if val.IsKnown() && !val.IsNull() && val.Type() == cty.String && json.Valid([]byte(val.AsString())) {
buf.WriteString("jsonencode(")

var ctyValue ctyjson.SimpleJSONValue
err := ctyValue.UnmarshalJSON([]byte(val.AsString()))
if err != nil {
Expand All @@ -183,27 +181,25 @@ func writeConfigAttributesFromExisting(addr addrs.AbsResourceInstance, buf *stri
continue
}

tok := hclwrite.TokensForValue(ctyValue.Value)
if _, err := tok.WriteTo(buf); err != nil {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Skipped part of config generation",
Detail: fmt.Sprintf("Could not create attribute %s in %s when generating import configuration. The plan will likely report the missing attribute as being deleted.", name, addr),
Extra: err,
})
continue
// Lone deserializable primitive types are valid json, but should be treated as strings
if ctyValue.Type().IsPrimitiveType() {
if d := writeTokens(val, buf); d != nil {
diags = diags.Append(d)
continue
}
} else {
buf.WriteString("jsonencode(")

if d := writeTokens(ctyValue.Value, buf); d != nil {
diags = diags.Append(d)
continue
}

buf.WriteString(")")
}

buf.WriteString(")")
} else {
tok := hclwrite.TokensForValue(val)
if _, err := tok.WriteTo(buf); err != nil {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Skipped part of config generation",
Detail: fmt.Sprintf("Could not create attribute %s in %s when generating import configuration. The plan will likely report the missing attribute as being deleted.", name, addr),
Extra: err,
})
if d := writeTokens(val, buf); d != nil {
diags = diags.Append(d)
continue
}
}
Expand All @@ -215,6 +211,20 @@ func writeConfigAttributesFromExisting(addr addrs.AbsResourceInstance, buf *stri
return diags
}

func writeTokens(val cty.Value, buf *strings.Builder) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
tok := hclwrite.TokensForValue(val)
if _, err := tok.WriteTo(buf); err != nil {
return diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Skipped part of config generation",
Detail: "Could not create attribute in import configuration. The plan will likely report the missing attribute as being deleted.",
Extra: err,
})
}
return diags
}

func writeConfigBlocks(addr addrs.AbsResourceInstance, buf *strings.Builder, blocks map[string]*configschema.NestedBlock, indent int) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics

Expand Down
43 changes: 43 additions & 0 deletions internal/genconfig/generate_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,49 @@ resource "tfcoremock_simple_resource" "empty" {
expected: `
resource "tfcoremock_simple_resource" "empty" {
value = jsonencode(["Hello", "World"])
}`,
},
"simple_resource_with_json_primitive_strings": {
schema: &configschema.Block{
// BlockTypes: map[string]*configschema.NestedBlock{},
radeksimko marked this conversation as resolved.
Show resolved Hide resolved
Attributes: map[string]*configschema.Attribute{
"id": {
Type: cty.String,
Computed: true,
},
"value_string_number": {
Type: cty.String,
Optional: true,
},
"value_string_bool": {
Type: cty.String,
Optional: true,
},
},
},
addr: addrs.AbsResourceInstance{
Module: nil,
Resource: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "tfcoremock_simple_resource",
Name: "empty",
},
Key: nil,
},
},
provider: addrs.LocalProviderConfig{
LocalName: "tfcoremock",
},
value: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("D2320658"),
"value_string_number": cty.StringVal("42"),
"value_string_bool": cty.StringVal("true"),
}),
expected: `
resource "tfcoremock_simple_resource" "empty" {
value_string_bool = "true"
value_string_number = "42"
}`,
},
"simple_resource_with_malformed_json": {
Expand Down
Loading