Skip to content

Commit

Permalink
convert: Fix MismatchMessage for object attributes
Browse files Browse the repository at this point in the history
When determining which object attributes cause a type mismatch, we must
disregard equal attribute types. Previously we would attempt to find a
conversion between them, deciding that the attribute type was a mismatch
if no conversion exists. However, this is invalid if the two types are
equal, because GetConversion returns nil for equal primitive types.

Because of non-deterministic map iteration order, the test case added in
this commit failed approximately 2/3 of the time before the change. It
now passes consistently.
  • Loading branch information
alisdair authored and apparentlymart committed Nov 23, 2020
1 parent 72c7e3e commit 1ce709a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cty/convert/mismatch_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func mismatchMessageObjects(got, want cty.Type) string {
continue
}

if gotAty.Equals(wantAty) {
continue // exact match, so no problem
}

// We'll now try to convert these attributes in isolation and
// see if we have a nested conversion error to report.
// We'll try an unsafe conversion first, and then fall back on
Expand Down
18 changes: 18 additions & 0 deletions cty/convert/mismatch_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ func TestMismatchMessage(t *testing.T) {
cty.List(cty.DynamicPseudoType),
`all list elements must have the same type`,
},
{
cty.Object(map[string]cty.Type{
"foo": cty.Bool,
"bar": cty.String,
"baz": cty.Object(map[string]cty.Type{
"boop": cty.Number,
}),
}),
cty.Object(map[string]cty.Type{
"foo": cty.Bool,
"bar": cty.String,
"baz": cty.Object(map[string]cty.Type{
"boop": cty.Number,
"beep": cty.Bool,
}),
}),
`attribute "baz": attribute "beep" is required`,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 1ce709a

Please sign in to comment.