Ah I see. We still shouldn’t be getting a plan difference from Terraform but at least you are not getting a Terraform Core error.
I’m trying to write a test to replicate the behavior that you’re seeing and I haven’t been able to get a non-empty plan error yet. I’ve defined an object attribute with a number attribute in my schema:
"object_attribute": schema.ObjectAttribute{
Optional: true,
Computed: true,
AttributeTypes: map[string]attr.Type{
"number_attribute": types.NumberType,
},
},
And in the Read()
method, I set the state manually to 10.4
:
func (r NumberPrecisionResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data NumberPrecisionResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
data.ObjectAttribute, _ = types.ObjectValue(map[string]attr.Type{
"number_attribute": types.NumberType,
}, map[string]attr.Value{
"number_attribute": types.NumberValue(big.NewFloat(10.4)),
})
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
And run the following test (Note: provider developers do not normally need to run this comprehensive of a test, this is just to look for bugs in the framework and testing module):
func TestSchemaResource_NumberAttribute_test(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"framework": providerserver.NewProtocol5WithError(New()),
},
Steps: []resource.TestStep{
{
Config: `resource "framework_number_precision" "test" {
object_attribute = {
number_attribute = 10.4
}
}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("framework_number_precision.test", plancheck.ResourceActionCreate),
},
PostApplyPreRefresh: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("framework_number_precision.test", plancheck.ResourceActionNoop),
},
PostApplyPostRefresh: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("framework_number_precision.test", plancheck.ResourceActionNoop),
},
},
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue("framework_number_precision.test", tfjsonpath.New("object_attribute").AtMapKey("number_attribute"), knownvalue.Float32Exact(10.4)),
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("framework_number_precision.test", "object_attribute.number_attribute", "10.4"),
),
},
},
})
}
And the above test still passes. Is there something in my test or schema that doesn’t match what you have?