Skip to content

[Fix] StructToData: allow empty values through for Optional+Computed fields#2

Open
diraol wants to merge 1 commit into
mainfrom
worktree-swift-fox-fngp
Open

[Fix] StructToData: allow empty values through for Optional+Computed fields#2
diraol wants to merge 1 commit into
mainfrom
worktree-swift-fox-fngp

Conversation

@diraol
Copy link
Copy Markdown
Collaborator

@diraol diraol commented May 22, 2026

Problem

StructToData in common/reflect_resource.go has a gate that skips writing empty/nil API values to state for Optional fields:

// Before
if fieldSchema.Optional && isValueNilOrEmpty(valueField, fieldPath) {
    return nil
}

This gate is correct for Optional-only fields: if the API returns a server default the user never configured, we should not pollute state with it.

However, the same gate was also firing for Optional+Computed fields — ones where the server is explicitly allowed to set the value. When the API returned an empty map or nil for such a field (e.g. a cluster policy clearing spark_conf), the value was silently dropped, never reaching Terraform state. This caused two concrete problems:

  1. Perpetual (known after apply) diffs — state never reflected the empty server value, so Terraform kept re-planning it as unknown.
  2. lifecycle { ignore_changes } not working — because the field's current server value was never written to state, the provider could not track it, and on the next apply it would silently revert the externally-set value back to whatever was last in state.

Fix

Add !fieldSchema.Computed to the gate condition:

// After
if fieldSchema.Optional && !fieldSchema.Computed && isValueNilOrEmpty(valueField, fieldPath) {
    return nil
}

This preserves existing behaviour for Optional-only fields (server defaults are still filtered) while allowing Optional+Computed fields to write empty/nil values through to state.

Tests

  • TestStructToData_OptionalComputedEmptyMapWrittenToState — regression test: empty map for an Optional+Computed field must reach state.
  • TestStructToData_OptionalOnlyEmptyMapNotWrittenToState — guard test: empty map for an Optional-only field is still filtered out.

Notes

This is the prerequisite central fix. Per-resource PRs (marking policy-settable fields as Optional+Computed) will follow and depend on this change.

Related: databricks#1238

…uted fields

Without this guard, StructToData silently dropped nil/empty API values for
*all* Optional fields — including Optional+Computed ones. This prevented
externally-set values (cluster policies, API defaults) from reaching
Terraform state, causing perpetual "(known after apply)" diffs and
breaking `lifecycle { ignore_changes }` for those fields.

Adding `!fieldSchema.Computed` preserves the existing behaviour for
Optional-only fields (server defaults are still filtered out) while
letting Optional+Computed fields write empty values to state so
Terraform can track and ignore them correctly.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@diraol diraol force-pushed the worktree-swift-fox-fngp branch from 7a33342 to f8fb5f6 Compare May 22, 2026 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant