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

internal/fwserver: Skip automatic object state conversion and nested attributes plan modification if object is null or unknown #995

Merged
merged 2 commits into from Apr 30, 2024

Conversation

bflad
Copy link
Member

@bflad bflad commented Apr 29, 2024

Closes #993

By data definition, a null or unknown object has no underlying data where nested attributes should run plan modification. By Terraform data consistency rules, a null or unknown object should be preserved as-is (unless it is an unknown value being refined into a more known value), however this was not the case where a collection-based nested attribute had a null or unknown object, such as this example configuration:

resource "examplecloud_thing" "example" {
  nested_map = {
    examplekey = null # this was previously problematic
  }
}

Previously, a null or unknown object would be errantly transformed by the framework plan modification logic into a known object with null/unknown attributes. This could be seen in human-readable Terraform plans, such as ({} below):

  # examplecloud_thing.example will be created
  + resource "examplecloud_thing" "example" {
      + nested_map = {
          + "examplekey" = {},
        }
    }

Following new unit test creation, failures could be seen from the PlanResourceChange RPC:

--- FAIL: TestServerPlanResourceChange_AttributeRoundtrip (0.00s)
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-and-computed-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`...,
            + 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`...,
            + 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<null>>>`,
              		Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-and-computed-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`...,
            + 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-and-computed-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`...,
            + 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`...,
            + 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`...,
            + 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`...,
            + 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<null>>>`,
              		Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-and-computed-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`...,
            + 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`...,
            + 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<null>>>`,
              		Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-and-computed-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`...,
            + 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-and-computed-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`...,
            + 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`...,
            + 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }

The plan modification logic was updated to return a null/unknown object early, but only after the object plan modifiers have run. This ensures that provider developers still have an escape hatch to run logic before the framework skips the nested attributes.

If provider developers need to override this logic change, object-level plan modifiers can convert a null/unknown object into a known object will all null/unknown attributes, which then the framework will run nested attribute plan modification on those attributes. If this is a common use case, a feature request can be submitted to consider including this type of object plan modifier in the framework.

…attributes plan modification if object is null or unknown

Reference: #993

By data definition, a null or unknown object has no underlying data where nested attributes should run plan modification. By Terraform data consistency rules, a null or unknown object should be preserved as-is (unless it is an unknown value being refined into a more known value), however this was not the case where a collection-based nested attribute had a null or unknown object, such as this example configuration:

```
resource "examplecloud_thing" "example" {
  nested_map = {
    examplekey = null # this was previously problematic
  }
}
```

Previously, a null or unknown object would be errantly transformed by the framework plan modification logic into a known object with null/unknown attributes. This could be seen in human-readable Terraform plans, such as (`{}` below):

```
  # examplecloud_thing.example will be created
  + resource "examplecloud_thing" "example" {
      + nested_map = {
          + "examplekey" = {},
        }
    }
```

Following new unit test creation, failures could be seen from the PlanResourceChange RPC:

```
--- FAIL: TestServerPlanResourceChange_AttributeRoundtrip (0.00s)
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-and-computed-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`...,
            + 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`...,
            + 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<null>>>`,
              		Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-and-computed-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`...,
            + 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-and-computed-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`...,
            + 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`...,
            + 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`...,
            + 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`...,
            + 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<null>>>`,
              		Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-and-computed-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`...,
            + 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`...,
            + 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<null>>>`,
              		Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-and-computed-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attribute"`...,
            + 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-and-computed-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<"string_att`...,
            + 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-unknown-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<"string_attrib`...,
            + 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true}}},
              	},
              	RequiresReplace: s"[]",
              }
```

```
--- FAIL: TestServerPlanResourceChange_AttributeRoundtrip (0.00s)
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-list-nested-optional-and-computed-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<null>>>`,
            + 		Raw:    s`tftypes.Object["list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]]<"list_nested_attribute":tftypes.List[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"list_nested_attribute": schema.ListNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-set-nested-optional-and-computed-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<null>>>`,
            + 		Raw:    s`tftypes.Object["set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]]<"set_nested_attribute":tftypes.Set[tftypes.Object["string_attribute":tftypes.String]]<tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"set_nested_attribute": schema.SetNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
    --- FAIL: TestServerPlanResourceChange_AttributeRoundtrip/create-map-nested-optional-and-computed-null-element (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:15072: unexpected difference:   &fwserver.PlanResourceChangeResponse{
              	Diagnostics:    nil,
              	PlannedPrivate: &{Provider: &{data: {}}},
              	PlannedState: &tfsdk.State{
            - 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<null>>>`,
            + 		Raw:    s`tftypes.Object["map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]]<"map_nested_attribute":tftypes.Map[tftypes.Object["string_attribute":tftypes.String]]<"null":tftypes.Object["string_attribute":tftypes.String]<unknown>>>`,
              		Schema: schema.Schema{Attributes: {"map_nested_attribute": schema.MapNestedAttribute{NestedObject: {...}, Optional: true, Computed: true}}},
              	},
              	RequiresReplace: s"[]",
              }
```

The plan modification logic was updated to return a null/unknown object early, but only after the object plan modifiers have run. This ensures that provider developers still have an escape hatch to run logic before the framework skips the nested attributes.

If provider developers need to override this logic change, object-level plan modifiers can convert a null/unknown object into a known object will all null/unknown attributes, which then the framework will run nested attribute plan modification on those attributes. If this is a common use case, a feature request can be submitted to consider including this type of object plan modifier in the framework.
@bflad bflad added the bug Something isn't working label Apr 29, 2024
@bflad bflad requested a review from a team as a code owner April 29, 2024 20:57
Copy link
Contributor

@bendbennett bendbennett left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

Thank you for adding such thorough unit test coverage.

@bflad bflad added this to the v1.9.0 milestone Apr 30, 2024
@bflad bflad merged commit f200629 into main Apr 30, 2024
28 checks passed
@bflad bflad deleted the bflad/nestedattributeobject-skip-known-valuing branch April 30, 2024 12:02
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.

Nested object attributes are instantiated during plan when config is null
2 participants