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

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

Closed
bendbennett opened this issue Jul 26, 2022 · 1 comment · Fixed by #213
Closed
Labels
bug Something isn't working

Comments

@bendbennett
Copy link
Contributor

bendbennett commented Jul 26, 2022

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.

@bendbennett bendbennett added the bug Something isn't working label Jul 26, 2022
bendbennett added a commit that referenced this issue Jul 27, 2022
of opts which can be used to modify the unmarshalling behaviour,
such as ignoring undefined attributes (#212)
bendbennett added a commit that referenced this issue Jul 27, 2022
of opts which can be used to modify the unmarshalling behaviour,
such as ignoring undefined attributes (#212)
bendbennett added a commit that referenced this issue Jul 27, 2022
bendbennett added a commit that referenced this issue Jul 27, 2022
bendbennett added a commit that referenced this issue Jul 28, 2022
* Adding alternative implementation of Unmarshal which allows supplying
of opts which can be used to modify the unmarshalling behaviour,
such as ignoring undefined attributes (#212)

* Upper case JSONOpts field (#212)

* Code review changes (#212)

* Apply suggestions from code review

Co-authored-by: Brian Flad <bflad417@gmail.com>

Co-authored-by: Brian Flad <bflad417@gmail.com>
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant