Skip to content

fix(variable): stop double-escaping JSON string values in variable resource#642

Merged
mitchnielsen merged 1 commit intomainfrom
variable-resource-escapes
Feb 24, 2026
Merged

fix(variable): stop double-escaping JSON string values in variable resource#642
mitchnielsen merged 1 commit intomainfrom
variable-resource-escapes

Conversation

@mitchnielsen
Copy link
Member

@mitchnielsen mitchnielsen commented Feb 23, 2026

Summary

getUnderlyingValue() was converting Terraform types to Go types incorrectly before sending them to the API. For tuples, it called .String() on each element, which wraps strings in escaped quotes (e.g., "foo" becomes "\"foo\""). For objects, it tried to json.Unmarshal the .String() output, which isn't valid JSON. Both paths caused values to get double-escaped when the HTTP client JSON-serialized the request body.

This replaces the conversion logic with a recursive convertAttrValueToNative() that uses the correct SDK accessors (ValueString(), ValueBool(), Attributes(), Elements()) to extract actual values. Also removes a strconv.Unquote workaround in convertAPIValueToDynamic() that was compensating for the double-quoting on the read path.

Closes #641

Upgrade note

Users who have prefect_variable resources with tuple or object values may see a one-time plan diff after upgrading. This is expected. The previously stored values in the API contain extra escape characters from the bug, and the provider will now send the correct unescaped values. Running terraform apply will fix the stored values. No manual intervention is needed.

Session context

Traced the full data flow from Terraform schema through the resource handler to the HTTP client. The .String() method on Terraform SDK types returns a debug/display representation with JSON quoting, not the raw value. The object case was even more broken since Terraform's .String() output isn't valid JSON at all. The strconv.Unquote workaround on the read path was a clue that something was off on the write path.

🤖 Generated with Claude Code

…source

getUnderlyingValue() was using .String() on tuple elements and
json.Unmarshal on object .String() output, both of which produce
escaped representations that get re-escaped during HTTP serialization.
Replace with a recursive convertAttrValueToNative() that extracts
actual values via ValueString()/ValueBool()/Attributes()/Elements().

Also removes the strconv.Unquote workaround in convertAPIValueToDynamic
that was compensating for the double-quoting on the read path.

Closes #641

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mitchnielsen mitchnielsen added the bug Something isn't working label Feb 23, 2026
@mitchnielsen mitchnielsen self-assigned this Feb 23, 2026
@mitchnielsen mitchnielsen added the bug Something isn't working label Feb 23, 2026
@mitchnielsen mitchnielsen marked this pull request as ready for review February 24, 2026 02:30
@mitchnielsen mitchnielsen requested a review from a team as a code owner February 24, 2026 02:30
Copy link
Member

@parkedwards parkedwards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can't help but feel like some of this is my fault

@mitchnielsen
Copy link
Member Author

I think this was mostly me, but we got there in the end hah

@mitchnielsen mitchnielsen merged commit 6ca0f5c into main Feb 24, 2026
8 checks passed
@mitchnielsen mitchnielsen deleted the variable-resource-escapes branch February 24, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

prefect_variable resource escapes JSON string values with backslashes

2 participants