hashicorp / terraform-plugin-go

A low-level Go binding for the Terraform protocol for integrations to be built on top of.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unmarshal should ignore elements in JSON that do not have a corresponding attribute

bendbennett opened this issue · comments

terraform-plugin-go version

0.12

Relevant provider source code

func TestValueFromJSON(t *testing.T) {
	t.Parallel()
	type testCase struct {
		value Value
		typ   Type
		json  string
	}
	tests := map[string]testCase{
		"object-with-missing-attribute": {
			value: NewValue(Object{
				AttributeTypes: map[string]Type{
					"bool":   Bool,
					"number": Number,
				},
			}, map[string]Value{
				"bool":   NewValue(Bool, true),
				"number": NewValue(Number, big.NewFloat(0)),
			}),
			typ: Object{
				AttributeTypes: map[string]Type{
					"bool":   Bool,
					"number": Number,
				},
			},
			json: `{"bool":true,"number":0,"unknown":"whatever"}`,
		},
	}
	for name, test := range tests {
		name, test := name, test
		t.Run(name, func(t *testing.T) {
			t.Parallel()
			val, err := ValueFromJSON([]byte(test.json), test.typ)
			if err != nil {
				t.Fatalf("unexpected error unmarshaling: %s", err)
			}
			if diff := cmp.Diff(test.value, val); diff != "" {
				t.Errorf("Unexpected results (-wanted +got): %s", diff)
			}
		})
	}
}

Expected Behavior

The test should pass.

Actual Behavior

The test fails with the following error:

unexpected error unmarshaling: ElementKeyValue(tftypes.String<unknown>): unsupported attribute "unknown"

Proposals

  • Modify the behaviour of Unmarshal so that elements that do not have a corresponding attribute can optionally be silently ignored.

References

This behaviour was discovered during the investigation of Unable to Read Previously Saved State for UpgradeResourceState TF 0.12.