From 078e63ebe7fe5b1366b622e4da54bfef0387740c Mon Sep 17 00:00:00 2001 From: guineveresaenger Date: Mon, 28 Oct 2024 14:46:04 -0700 Subject: [PATCH 1/3] Allow for a Type: 'boolean' to validate when property value is a stringified bool --- pkg/tfbridge/typechecker/typechecker.go | 3 ++- pkg/tfbridge/typechecker/typechecker_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/tfbridge/typechecker/typechecker.go b/pkg/tfbridge/typechecker/typechecker.go index c84cbb7a7..3a96a4101 100644 --- a/pkg/tfbridge/typechecker/typechecker.go +++ b/pkg/tfbridge/typechecker/typechecker.go @@ -161,7 +161,8 @@ func (v *TypeChecker) validatePropertyValue( switch typeSpec.Type { case "boolean": - if !propertyValue.IsBool() { + // The bridge permits the strings "true" and "false" to read as boolean, so allow strings. + if !propertyValue.IsBool() && !propertyValue.IsString() { return []Failure{newTypeFailure(propertyPath, typeSpec.Type, propertyValue)} } return nil diff --git a/pkg/tfbridge/typechecker/typechecker_test.go b/pkg/tfbridge/typechecker/typechecker_test.go index a8007a3ad..f9f6a5ebb 100644 --- a/pkg/tfbridge/typechecker/typechecker_test.go +++ b/pkg/tfbridge/typechecker/typechecker_test.go @@ -1631,6 +1631,11 @@ func TestValidateConfigType(t *testing.T) { })), }), }, + { + name: "allows_bool_strings", + inputName: "skipMetadataApiCheck", + input: resource.PropertyValue{V: "true"}, + }, } for _, tc := range testCases { tc := tc @@ -1661,6 +1666,11 @@ func TestValidateConfigType(t *testing.T) { }, }, }, + "skipMetadataApiCheck": { + TypeSpec: pschema.TypeSpec{ + Type: "boolean", + }, + }, }, }, } From 2ee1a9ad22d7ba42197817243b4fb90f355154c2 Mon Sep 17 00:00:00 2001 From: guineveresaenger Date: Mon, 28 Oct 2024 15:33:07 -0700 Subject: [PATCH 2/3] Only allow the strings 'true' and 'false' to pass the type checker --- pkg/tfbridge/typechecker/typechecker.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/tfbridge/typechecker/typechecker.go b/pkg/tfbridge/typechecker/typechecker.go index 3a96a4101..e61e013e4 100644 --- a/pkg/tfbridge/typechecker/typechecker.go +++ b/pkg/tfbridge/typechecker/typechecker.go @@ -161,8 +161,15 @@ func (v *TypeChecker) validatePropertyValue( switch typeSpec.Type { case "boolean": - // The bridge permits the strings "true" and "false" to read as boolean, so allow strings. - if !propertyValue.IsBool() && !propertyValue.IsString() { + // Check for strings that are values "true" or "false". + // These are handled as booleans in the bridge, so they should be skipped by the type checker. + var boolString bool + if propertyValue.IsString() { + if propertyValue.StringValue() == "true" || propertyValue.StringValue() == "false" { + boolString = true + } + } + if !propertyValue.IsBool() && !boolString { return []Failure{newTypeFailure(propertyPath, typeSpec.Type, propertyValue)} } return nil From 05c83689134d3a4d1701c1a90fdb3db60760a8d5 Mon Sep 17 00:00:00 2001 From: guineveresaenger Date: Tue, 29 Oct 2024 10:18:23 -0700 Subject: [PATCH 3/3] Clarify workaround conditions --- pkg/tfbridge/typechecker/typechecker.go | 3 ++- pkg/tfbridge/typechecker/typechecker_test.go | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/tfbridge/typechecker/typechecker.go b/pkg/tfbridge/typechecker/typechecker.go index e61e013e4..2f8b47d78 100644 --- a/pkg/tfbridge/typechecker/typechecker.go +++ b/pkg/tfbridge/typechecker/typechecker.go @@ -162,7 +162,8 @@ func (v *TypeChecker) validatePropertyValue( switch typeSpec.Type { case "boolean": // Check for strings that are values "true" or "false". - // These are handled as booleans in the bridge, so they should be skipped by the type checker. + //TODO: Remove the boolString condition when https://github.com/pulumi/pulumi-terraform-bridge/issues/2520 + // is resolved. This is a workaround for the config encoding not honoring type overrides. var boolString bool if propertyValue.IsString() { if propertyValue.StringValue() == "true" || propertyValue.StringValue() == "false" { diff --git a/pkg/tfbridge/typechecker/typechecker_test.go b/pkg/tfbridge/typechecker/typechecker_test.go index f9f6a5ebb..e2bfbff0f 100644 --- a/pkg/tfbridge/typechecker/typechecker_test.go +++ b/pkg/tfbridge/typechecker/typechecker_test.go @@ -1632,6 +1632,9 @@ func TestValidateConfigType(t *testing.T) { }), }, { + //TODO: Remove this test when https://github.com/pulumi/pulumi-terraform-bridge/issues/2520 is resolved. + // This tests a workaround path to keep the type checker from tripping on missing functionality in the + // config encoding and will fail once that is fixed. name: "allows_bool_strings", inputName: "skipMetadataApiCheck", input: resource.PropertyValue{V: "true"},