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 for TypeSet applying with unexpected empty element #1042

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Commits on Aug 30, 2022

  1. Fix for TypeSet applying with unexpected empty element

    Fixes hashicorp#895
    
    When calculating a diff, elements in sets are tracked as a delete of all
    old attributes with `NewRemoved = true`, and a create of all new
    attributes. When `DiffSuppressFunc` is used for an attribute, the
    `ResourceAttrDiff` is replaced with `New = Old` (no-op):
    https://github.com/hashicorp/terraform-plugin-sdk/blob/fa8d313665945816f6eb6c79182e4abdc1540504/helper/schema/schema.go#L1144
    
    However, this also drops all other metadata about the attr diff, such as
    `NewRemoved`. This ends up affecting the `InstanceDiff.applyBlockDiff`
    which expects to drop removed items:
    https://github.com/hashicorp/terraform-plugin-sdk/blob/e14d3b611f2e257d2a0862e5ea0f90ea96fd5bf8/terraform/diff.go#L207
    
    All attributes of the removed set element get removed here, except those
    with `DiffSuppressFunc` since the `NewRemoved` attribute was dropped.
    
    Instead of dropping the metadata about the attr diff, clone the original
    attr diff, and just set `New = Old`.
    
    The added test fails without this fix:
    ```
    === RUN   TestSchemaMap_Diff/30-Set_with_DiffSuppressFunc
        schema_test.go:3188: expected:
            *terraform.InstanceDiff{ [...]"rule.80.duration":*terraform.ResourceAttrDiff{Old:"", New:"", NewComputed:false, NewRemoved:true, [...]
    
            got:
            *terraform.InstanceDiff{ [...]"rule.80.duration":*terraform.ResourceAttrDiff{Old:"", New:"", NewComputed:false, [...] NewRemoved:false, [...]
    ```
    
    Previously, `NewRemoved` was set to false for the sustain field even though it belonged to an element being removed.
    prashantv committed Aug 30, 2022
    Configuration menu
    Copy the full SHA
    d7ff689 View commit details
    Browse the repository at this point in the history