Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20261124-160000.yaml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 6 additions & 2 deletions helper/schema/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions helper/schema/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down