Skip to content

Commit ed6d4bf

Browse files
wata727apparentlymart
authored andcommitted
ext/typeexpr: Avoid refinements on dynamic values
1 parent a9f8d65 commit ed6d4bf

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

ext/typeexpr/defaults.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (d *Defaults) apply(v cty.Value) cty.Value {
9595
}
9696
values[key] = defaultValue
9797
}
98-
if defaultRng := defaultValue.Range(); defaultRng.DefinitelyNotNull() {
98+
if defaultRng := defaultValue.Range(); defaultRng.DefinitelyNotNull() && values[key].Type() != cty.DynamicPseudoType {
9999
values[key] = values[key].RefineNotNull()
100100
}
101101
}

ext/typeexpr/defaults_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,23 @@ func TestDefaults_Apply(t *testing.T) {
898898
"foo": cty.UnknownVal(cty.String).RefineNotNull(),
899899
}),
900900
},
901+
"optional attribute with dynamic value can be null": {
902+
defaults: &Defaults{
903+
Type: cty.ObjectWithOptionalAttrs(map[string]cty.Type{
904+
"foo": cty.String,
905+
}, []string{"foo"}),
906+
DefaultValues: map[string]cty.Value{
907+
"foo": cty.StringVal("bar"), // Important: default is non-null
908+
},
909+
},
910+
value: cty.ObjectVal(map[string]cty.Value{
911+
"foo": cty.DynamicVal,
912+
}),
913+
want: cty.ObjectVal(map[string]cty.Value{
914+
// The default value is itself non-null, but dynamic value cannot be refined.
915+
"foo": cty.DynamicVal,
916+
}),
917+
},
901918
}
902919

903920
for name, tc := range testCases {

0 commit comments

Comments
 (0)