Skip to content

Commit f3da388

Browse files
authored
[cli/import] Strip InputType after union reduction in structural typing (#23115)
## Summary `valueStructurallyTypedAs` (`pkg/importer/hcl2.go`) strips `*schema.InputType` from its `schemaType` argument **before** calling `reduceUnionType`, but `reduceUnionType` returns whichever union element fits as-is. Schema-bound unions almost always have `*schema.InputType`-wrapped members (e.g. `Input<Obj0>`), so the post-union type switch never matches and the function returns `false` even when the value structurally matches the underlying type. The visible symptom is that `GenerateHCL2Definition` logs `"dropped non-conforming value from object type"` and silently drops the entry: an input like `{"k": {}}` against `Map<Union<Input<Obj0>, Input<Archive>>>` round-trips to `{}` when `Obj0` has no required properties. This is the root cause of the `TestRapidGenerateHCL2Definition` failure on https://github.com/pulumi/pulumi/actions/runs/25798683952/job/75783074303?pr=23112 (the rapid shrinker minimised it to a value with `b = {"":{}}` whose element type was `Input<Union<Input<a-:index:Obj0•Input>, Input<pulumi:pulumi:Archive>>>`). Fix: reapply the `*schema.InputType`-strip loop after the union reduction so the switch sees the unwrapped element type. ## Test plan - [x] `go test -count=1 -tags all -timeout 5m ./importer -run TestRapidGenerateHCL2Definition -rapid.seed=10040562407060112324` — the exact seed from the linked CI run, now passes. - [x] `go test -count=1 -tags all ./importer` — the existing `valueStructurallyTypedAs` unit tests still pass; the fix is strictly additive (it never flips `true` to `false`). - [ ] Note: this test still flakes from at least one other unrelated importer bug (`valueStructurallyTypedAs` doesn't handle `*schema.EnumType` in its primitive-value cases). That is out of scope here and will be addressed separately. Fixes #23105
1 parent 11a5257 commit f3da388

3 files changed

Lines changed: 113 additions & 1 deletion

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
changes:
2+
- type: fix
3+
scope: cli/import
4+
description: Stop dropping map values whose element type is a union of Input-wrapped types during HCL2 import

‎pkg/importer/hcl2.go‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,17 +608,43 @@ func valueStructurallyTypedAs(value property.Value, schemaType schema.Type) bool
608608
break
609609
}
610610
}
611+
// An enum value is represented as a value of the enum's underlying primitive
612+
// type at the wire level (e.g. a string for a string-typed enum). Substitute
613+
// the underlying type so the primitive-value branches below match.
614+
if enum, ok := schemaType.(*schema.EnumType); ok {
615+
schemaType = enum.ElementType
616+
}
611617
if union, ok := schemaType.(*schema.UnionType); ok {
612618
schemaType = reduceUnionType(union, value)
613619
}
620+
// reduceUnionType returns a member of the union as-is, which is often an
621+
// *schema.InputType wrapping the real type. Strip those wrappers again so
622+
// the type switch below can match on the underlying type.
623+
for {
624+
if input, ok := schemaType.(*schema.InputType); ok {
625+
schemaType = input.ElementType
626+
} else {
627+
break
628+
}
629+
}
614630

615-
if schemaType == schema.AnyType {
631+
// AnyType, JSONType, and AnyResourceType all share the "pulumi:pulumi:Any"
632+
// token but are distinct singletons. Each one accepts any value at the
633+
// structural level, so short-circuit them together.
634+
if schemaType == schema.AnyType || schemaType == schema.JSONType || schemaType == schema.AnyResourceType {
616635
return true
617636
}
618637

619638
switch {
620639
case value.IsMap():
621640
switch arg := schemaType.(type) {
641+
case *schema.MapType:
642+
for _, vv := range value.AsMap().All {
643+
if !valueStructurallyTypedAs(vv, arg.ElementType) {
644+
return false
645+
}
646+
}
647+
return true
622648
case *schema.ObjectType:
623649
schemaProperties := make(map[string]schema.Type)
624650
for _, schemaProperty := range arg.Properties {

‎pkg/importer/hcl2_test.go‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,88 @@ func TestStructuralTypeChecks(t *testing.T) {
11121112

11131113
assert.True(t, valueStructurallyTypedAs(complexFittingValue, complexUnionOfObjects))
11141114
})
1115+
1116+
t.Run("Enum", func(t *testing.T) {
1117+
t.Parallel()
1118+
1119+
// An enum is represented on the wire as a value of its underlying
1120+
// primitive type. valueStructurallyTypedAs must look through the enum
1121+
// to its ElementType so a primitive value can be accepted.
1122+
stringEnum := &schema.EnumType{
1123+
Token: "a:index:S",
1124+
ElementType: schema.StringType,
1125+
Elements: []*schema.Enum{{Value: "x"}, {Value: "y"}},
1126+
}
1127+
boolEnum := &schema.EnumType{
1128+
Token: "a:index:B",
1129+
ElementType: schema.BoolType,
1130+
Elements: []*schema.Enum{{Value: true}, {Value: false}},
1131+
}
1132+
1133+
assert.True(t, valueStructurallyTypedAs(property.New("x"), stringEnum))
1134+
assert.True(t, valueStructurallyTypedAs(property.New(false), boolEnum))
1135+
// A bool against a string-typed enum is still rejected.
1136+
assert.False(t, valueStructurallyTypedAs(property.New(true), stringEnum))
1137+
// Enum wrapped in InputType is unwrapped too.
1138+
assert.True(t, valueStructurallyTypedAs(
1139+
property.New(false),
1140+
&schema.InputType{ElementType: boolEnum}))
1141+
})
1142+
1143+
t.Run("MissingTypeCases", func(t *testing.T) {
1144+
t.Parallel()
1145+
1146+
// schema.JSONType and schema.AnyResourceType share the
1147+
// "pulumi:pulumi:Any" token with schema.AnyType but are distinct
1148+
// singletons. All three accept any value structurally.
1149+
assert.True(t, valueStructurallyTypedAs(property.New(""), schema.JSONType))
1150+
assert.True(t, valueStructurallyTypedAs(property.New(false), schema.JSONType))
1151+
assert.True(t, valueStructurallyTypedAs(property.New(""), schema.AnyResourceType))
1152+
assert.True(t, valueStructurallyTypedAs(property.New(false), schema.AnyResourceType))
1153+
1154+
// A property.Map value against *schema.MapType must accept entries
1155+
// whose values match the element type (the previous implementation
1156+
// only handled ObjectType and UnionType for IsMap values).
1157+
stringMap := &schema.MapType{ElementType: schema.StringType}
1158+
assert.True(t, valueStructurallyTypedAs(
1159+
makeObject(map[string]property.Value{"k": property.New("v")}),
1160+
stringMap))
1161+
// Empty maps trivially match.
1162+
assert.True(t, valueStructurallyTypedAs(makeObject(nil), stringMap))
1163+
// Mismatched element type is still rejected.
1164+
assert.False(t, valueStructurallyTypedAs(
1165+
makeObject(map[string]property.Value{"k": property.New(42.0)}),
1166+
stringMap))
1167+
})
1168+
1169+
t.Run("UnionOfInputWrappedObjects", func(t *testing.T) {
1170+
t.Parallel()
1171+
1172+
// Schema-bound unions wrap their members in *schema.InputType, e.g.
1173+
// Map<Union<Input<Obj>, Input<Archive>>>. valueStructurallyTypedAs
1174+
// strips *schema.InputType from its argument *before* the union is
1175+
// reduced, but reduceUnionType returns the chosen element as-is —
1176+
// typically Input<Obj>. The type switch below must still see the
1177+
// underlying type, so the function must strip the wrapper again
1178+
// after the reduction.
1179+
objectType := makeObjectType(
1180+
makeProperty("foo", schema.StringType),
1181+
)
1182+
union := makeUnionType(
1183+
&schema.InputType{ElementType: objectType},
1184+
&schema.InputType{ElementType: schema.ArchiveType},
1185+
)
1186+
1187+
// A map that fits Obj is accepted even though Obj is wrapped in
1188+
// *schema.InputType inside the union.
1189+
assert.True(t, valueStructurallyTypedAs(
1190+
makeObject(map[string]property.Value{"foo": property.New("x")}),
1191+
union))
1192+
// A map that does not fit Obj (unknown property) is still rejected.
1193+
assert.False(t, valueStructurallyTypedAs(
1194+
makeObject(map[string]property.Value{"unknown": property.New("x")}),
1195+
union))
1196+
})
11151197
}
11161198

11171199
func TestGenerateValuePreservesProviderDiscriminators(t *testing.T) {

0 commit comments

Comments
 (0)