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

Optional attributes fixes #88

Merged
merged 3 commits into from Mar 8, 2021

Conversation

alisdair
Copy link
Contributor

@alisdair alisdair commented Mar 4, 2021

Two commits addressing issues related to objects with optional attributes. These commits address these downstream bugs in Terraform:

Fix for null objects with optional attrs

Conversions targeting types which are or include objects with optional attributes always simplify those types, removing the optional attributes from the type, and inserting null values for any that are missing. Objects with optional attributes are not intended to be used to instantiate values.

One accidental exception to this was the null value of dynamic type. These values are passed through by the getConversion wrapper, assigning the desired type. This conflicts with the type simplification rules for non-null values, and can result in a panic when a collection type contains null values.

This commit fixes this at the wrapper level by performing the same type simplification when converting null into an object type.

Fix unification of collection values

To correctly unify deep nested types, we need to unify all collection types (maps, lists, sets), not just maps. This ensures that all cousin types are unified correctly.

This problem was first addressed for maps in #47 a year ago, but we didn't realize at the time that the same bug could manifest in other collection types.

Conversions targeting types which are or include objects with optional
attributes always simplify those types, removing the optional attributes
from the type, and inserting null values for any that are missing.
Objects with optional attributes are not intended to be used to
instantiate values.

One accidental exception to this was the null value of dynamic type.
These values are passed through by the getConversion wrapper, assigning
the desired type. This conflicts with the type simplification rules for
non-null values, and can result in a panic when a collection type
contains null values.

This commit fixes this at the wrapper level by performing the same type
simplification when converting null into an object type.
To correctly unify deep nested types, we need to unify all collection
types (maps, lists, sets), not just maps. This ensures that all cousin
types are unified correctly.

This problem was first addressed for maps in zclconf#47 a year ago, but we
didn't realize at the time that the same bug could manifest in other
collection types.
@codecov
Copy link

codecov bot commented Mar 4, 2021

Codecov Report

Merging #88 (dfcbf96) into main (fcc7075) will increase coverage by 0.02%.
The diff coverage is 84.61%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #88      +/-   ##
==========================================
+ Coverage   70.67%   70.69%   +0.02%     
==========================================
  Files          79       79              
  Lines        6547     6580      +33     
==========================================
+ Hits         4627     4652      +25     
- Misses       1476     1483       +7     
- Partials      444      445       +1     
Impacted Files Coverage Δ
cty/convert/unify.go 80.64% <66.66%> (-1.43%) ⬇️
cty/type.go 82.69% <90.47%> (+5.27%) ⬆️
cty/convert/conversion.go 82.10% <100.00%> (+0.38%) ⬆️
cty/convert/mismatch_msg.go 69.38% <0.00%> (-2.05%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fcc7075...dfcbf96. Read the comment docs.

Copy link
Collaborator

@apparentlymart apparentlymart left a comment

Choose a reason for hiding this comment

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

Thanks for digging into these, @alisdair!

The overall approach here looks good to me, though please see my inline comment below about a trickier edge that I think this isn't covering.

// conversion function which drops the optional attributes from the
// type. Null pass through values must do the same to ensure that
// homogeneous collections have a single element type.
if out.IsObjectType() && len(out.OptionalAttributes()) > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seeing this check made me wonder if the shallow check here would be sufficient to deal with a case where we have a type constraint that only indirectly contains an object type with optional attributes. In order to try to test that I added the following test case in public_test.go, adapting one of the ones you added in this PR:

		{
			Value: cty.TupleVal([]cty.Value{
				cty.NullVal(cty.DynamicPseudoType),
				cty.ObjectVal(map[string]cty.Value{
					"foo": cty.NullVal(cty.DynamicPseudoType),
				}),
			}),
			Type: cty.List(cty.Object(
				map[string]cty.Type{
					"foo": cty.ObjectWithOptionalAttrs(
						map[string]cty.Type{
							"bar": cty.String,
						},
						[]string{"bar"},
					),
				},
			)),
			Want: cty.ListVal([]cty.Value{
				cty.NullVal(cty.Object(map[string]cty.Type{
					"foo": cty.Object(map[string]cty.Type{
						"bar": cty.String,
					}),
				})),
				cty.ObjectVal(map[string]cty.Value{
					"foo": cty.NullVal(cty.Object(map[string]cty.Type{
						"bar": cty.String,
					})),
				}),
			}),
		},

Unfortunately this seems to fall into the same panic trap as the original upstream report 😖

panic: inconsistent list element types (cty.Object(map[string]cty.Type{"foo":cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String}, []string{"bar"})}) then cty.Object(map[string]cty.Type{"foo":cty.Object(map[string]cty.Type{"bar":cty.String})})) [recovered]
        panic: inconsistent list element types (cty.Object(map[string]cty.Type{"foo":cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String}, []string{"bar"})}) then cty.Object(map[string]cty.Type{"foo":cty.Object(map[string]cty.Type{"bar":cty.String})}))

goroutine 156 [running]:
testing.tRunner.func1.2(0x5ad520, 0xc0001be020)
        /home/me/.goenv/versions/1.16.0/src/testing/testing.go:1144 +0x332
testing.tRunner.func1(0xc0001abb00)
        /home/me/.goenv/versions/1.16.0/src/testing/testing.go:1147 +0x4b6
panic(0x5ad520, 0xc0001be020)
        /home/me/.goenv/versions/1.16.0/src/runtime/panic.go:965 +0x1b9
github.com/zclconf/go-cty/cty.ListVal(0xc000160f80, 0x2, 0x2, 0xc0001483c0, 0xc00017ff00, 0x1, 0x1)
        /home/me/Devel/go-cty/cty/value_init.go:166 +0x57e
github.com/zclconf/go-cty/cty/convert.conversionCollectionToList.func1(0x6b97d0, 0xc000133540, 0x5a40e0, 0xc0000bd2f0, 0x0, 0x0, 0x0, 0x557e14, 0xc00009ad68, 0x40da5b, ...)
        /home/me/Devel/go-cty/cty/convert/conversion_collection.go:54 +0x793
github.com/zclconf/go-cty/cty/convert.getConversion.func1(0x6b97d0, 0xc000133540, 0x5a40e0, 0xc0000bd2f0, 0x0, 0x0, 0x0, 0xc0001a3ac0, 0xc00017fef0, 0x6b97d0, ...)
        /home/me/Devel/go-cty/cty/convert/conversion.go:56 +0x52c
github.com/zclconf/go-cty/cty/convert.retConversion.func1(0x6b97d0, 0xc000133540, 0x5a40e0, 0xc0000bd2f0, 0xc00017fef0, 0x4d33a4, 0x76c940, 0x6d17d2, 0xf, 0x6e0f68)
        /home/me/Devel/go-cty/cty/convert/conversion.go:198 +0x6b
github.com/zclconf/go-cty/cty/convert.Convert(0x6b97d0, 0xc000133540, 0x5a40e0, 0xc0000bd2f0, 0x6b97d0, 0xc000133570, 0xf, 0x6e0f68, 0x38, 0x439, ...)
        /home/me/Devel/go-cty/cty/convert/public.go:51 +0x1c5
github.com/zclconf/go-cty/cty/convert.TestConvert.func1(0xc0001abb00)
        /home/me/Devel/go-cty/cty/convert/public_test.go:986 +0x97
testing.tRunner(0xc0001abb00, 0xc00017fe80)
        /home/me/.goenv/versions/1.16.0/src/testing/testing.go:1194 +0xef
created by testing.(*T).Run
        /home/me/.goenv/versions/1.16.0/src/testing/testing.go:1239 +0x2b3

I think unfortunately getting correct behavior here is going to require a recursive walk of the type constraint for any null value (and possibly also the same for ths !in.IsKnown() case above?) to concrete-ize any of the object types with optional attributes inside. The method cty.Type.HasDynamicTypes shows a pattern for implementing a recursive walk of that sort, so I guess my first idea is to add cty.Type.WithoutOptionalAttributesDeep that will return a type with all of the optional attributes inside removed. That will unfortunately be a bit expensive for new memory allocation, but I think in a way that will have similar impact to the "normal" (non-null, known) type conversion codepaths.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@apparentlymart I'm unable to reproduce this panic when both of the commits on this branch are in place. The extra unification step introduced for lists (and sets) handles the disjoint types of the null and object tuple members, and the result is that the test passes. If I revert the unification commit, the panic occurs.

With unification in place, I can't see any situation where the WithoutOptionalAttributesDeep function is needed. I'm happy to add a commit to add and use it, but I'm not sure it's necessary until I can find a test case which fails without it.

Am I missing something, or did you test the panic against the first commit only? Any suggestions for an extended test which would fail in this way?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hi @alisdair,

Curiously, I went back to my dev environment where I'd previously tested this and re-ran tests without any modifications and it didn't panic for me anymore either, so I have to assume I'd somehow got a stale result out of the test suite, although I have no concrete explanation as to why that would be.

Your explanation of how the unification step should get expected result makes sense to me now I re-read the changeset with that idea in mind, and I can no longer reproduce this panic, so instead of trying to explain this oddity I'm going to just assume I made a human error while I was testing and inadvertently tested something other than what I thought I was testing.

With that said, I made some adjustments to the test to take type unification out of the process and focus only on the type conversion aspect, with this new test case:

		{
			Value: cty.NullVal(cty.DynamicPseudoType),
			Type: cty.Object(
				map[string]cty.Type{
					"foo": cty.ObjectWithOptionalAttrs(
						map[string]cty.Type{
							"bar": cty.String,
						},
						[]string{"bar"},
					),
				},
			),
			Want: cty.NullVal(cty.Object(map[string]cty.Type{
				"foo": cty.Object(map[string]cty.Type{
					"bar": cty.String,
				}),
			})),
		},

This failed for me because the resulting null value had a type still containing optional attributes:

    --- FAIL: TestConvert/cty.NullVal(cty.DynamicPseudoType)_to_cty.Object(map[string]cty.Type{"foo":cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String},_[]string{"bar"})}) (0.00s)
        public_test.go:987: wrong result
            value: cty.NullVal(cty.DynamicPseudoType)
            type:  cty.Object(map[string]cty.Type{"foo":cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String}, []string{"bar"})})
            got:   cty.NullVal(cty.Object(map[string]cty.Type{"foo":cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String}, []string{"bar"})}))
            want:  cty.NullVal(cty.Object(map[string]cty.Type{"foo":cty.Object(map[string]cty.Type{"bar":cty.String})}))

Of course, we could decide that the Want value I wrote here is what's wrong, rather than the actual result, but given that in other cases we've decided that the result should have all of the optional attribute annotations removed, I wanted to raise this to see what you think about the inconsistency of this result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the extra clarification! I think the most recent commit addresses this now, for both unknown and null values.

When converting unknown and null values to a given type, we want to
avoid constructing instances of types which include optional object
attributes, even deeply nested. To do this we add a new method,
cty.Type.WithoutOptionalAttributesDeep, which recursively processes its
receiver to return a type which has no instances of objects with
optional attributes.
Copy link
Collaborator

@apparentlymart apparentlymart left a comment

Choose a reason for hiding this comment

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

Great! Thanks for working through these extra cases with me, and sorry for the weird diversion into a panic that actually wasn't.

I'm going to merge this now.

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.

None yet

2 participants