From 32ae2d986b070e57611a5e19b5f4233457e575e9 Mon Sep 17 00:00:00 2001 From: Gabriel Ignat <6107088+IgnatG@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:03:52 +0000 Subject: [PATCH] fix(helper/schema): resolve SchemaFunc when generating resource configuration --- .../unreleased/BUG FIXES-20261124-160000.yaml | 5 ++ helper/schema/grpc_provider.go | 8 ++- helper/schema/grpc_provider_test.go | 67 +++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/BUG FIXES-20261124-160000.yaml diff --git a/.changes/unreleased/BUG FIXES-20261124-160000.yaml b/.changes/unreleased/BUG FIXES-20261124-160000.yaml new file mode 100644 index 00000000000..eb874397bc8 --- /dev/null +++ b/.changes/unreleased/BUG FIXES-20261124-160000.yaml @@ -0,0 +1,5 @@ +kind: BUG FIXES +body: 'helper/schema: Fixed `GenerateResourceConfig` returning the whole state, including computed-only attributes, as configuration for resources defined with `SchemaFunc`' +time: 2026-11-24T16:00:00.000000+00:00 +custom: + Issue: "1637" diff --git a/helper/schema/grpc_provider.go b/helper/schema/grpc_provider.go index bd165d47542..825fec927ad 100644 --- a/helper/schema/grpc_provider.go +++ b/helper/schema/grpc_provider.go @@ -1800,6 +1800,10 @@ func (s *GRPCProviderServer) GenerateResourceConfig(ctx context.Context, req *tf schemaBlock := s.getResourceSchemaBlock(req.TypeName) res := s.provider.ResourcesMap[req.TypeName] + // SchemaMap resolves a resource defined with SchemaFunc, whose Schema + // field is nil: reading res.Schema would find no attribute or block, + // and the whole state would be returned as configuration. + resSchema := schemaMap(res.SchemaMap()) stateVal, err := msgpack.Unmarshal(req.State.MsgPack, schemaBlock.ImpliedType()) if err != nil { @@ -1839,8 +1843,8 @@ func (s *GRPCProviderServer) GenerateResourceConfig(ctx context.Context, req *tf null := cty.NullVal(ty) // find the attribute or block schema representing the value - attr := schemaMap(res.Schema).AttributeByPath(path) - block := schemaMap(res.Schema).BlockByPath(path) + attr := resSchema.AttributeByPath(path) + block := resSchema.BlockByPath(path) switch { case attr != nil: // deprecated attributes diff --git a/helper/schema/grpc_provider_test.go b/helper/schema/grpc_provider_test.go index 50a122122de..80f3e208a7f 100644 --- a/helper/schema/grpc_provider_test.go +++ b/helper/schema/grpc_provider_test.go @@ -11873,6 +11873,73 @@ func TestGenerateResourceConfig(t *testing.T) { }, }, }, + "schema-func-resource": { + server: NewGRPCProviderServer(&Provider{ + ResourcesMap: map[string]*Resource{ + "test": { + SchemaVersion: 1, + SchemaFunc: func() map[string]*Schema { + return map[string]*Schema{ + "id": { + Type: TypeString, + Computed: true, + Optional: true, + }, + "test_computed": { + Type: TypeString, + Computed: true, + }, + "test_optional": { + Type: TypeString, + Optional: true, + }, + "test_empty_string": { + Type: TypeString, + Optional: true, + }, + } + }, + }, + }, + }), + req: &tfprotov5.GenerateResourceConfigRequest{ + TypeName: "test", + State: &tfprotov5.DynamicValue{ + MsgPack: mustMsgpackMarshal( + cty.Object(map[string]cty.Type{ + "id": cty.String, + "test_computed": cty.String, + "test_optional": cty.String, + "test_empty_string": cty.String, + }), + cty.ObjectVal(map[string]cty.Value{ + "id": cty.StringVal("id-val"), + "test_computed": cty.StringVal("computed-val"), + "test_optional": cty.StringVal("optional-val"), + "test_empty_string": cty.StringVal(""), + }), + ), + }, + }, + expected: &tfprotov5.GenerateResourceConfigResponse{ + Config: &tfprotov5.DynamicValue{ + MsgPack: mustMsgpackMarshal( + cty.Object(map[string]cty.Type{ + "id": cty.String, + "test_computed": cty.String, + "test_optional": cty.String, + "test_empty_string": cty.String, + }), + cty.ObjectVal(map[string]cty.Value{ + "id": cty.NullVal(cty.String), + "test_computed": cty.NullVal(cty.String), + "test_optional": cty.StringVal("optional-val"), + "test_empty_string": cty.NullVal(cty.String), + }), + ), + }, + }, + }, "schema-behaviors": { server: NewGRPCProviderServer(&Provider{ ResourcesMap: map[string]*Resource{