Skip to content

PreferWriteOnlyAttribute validator is not making use of the client capabilities #282

@yottta

Description

@yottta

Terraform CLI and Framework Versions

Terraform v1.11.3 on darwin_arm64

github.com/hashicorp/terraform-plugin-framework-validators from the main branch

Terraform Plugin Framework Code

func (r *writeOnlyResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
	resp.Schema = schema.Schema{
		Attributes: map[string]schema.Attribute{
			...
			"attr": schema.StringAttribute{
				Optional: true,
				Validators: []validator.String{
					stringvalidator.PreferWriteOnlyAttribute(path.MatchRoot("attr_wo")),
					stringvalidator.ExactlyOneOf(path.MatchRoot("attr_wo")),
				},
			},
			"attr_wo": schema.StringAttribute{
				Optional:  true,
				WriteOnly: true,
				Validators: []validator.String{
					stringvalidator.ExactlyOneOf(path.MatchRoot("attr")),
					stringvalidator.AlsoRequires(path.MatchRoot("attr_wo_version")),
				},
			},
			"has_attr_wo": schema.BoolAttribute{
				Computed: true,
			},
			"attr_wo_version": schema.Int32Attribute{
				Optional: true,
			},
		},
	}
}

Terraform Configuration

terraform {
  required_providers {
    provs = {
      source = "edu/provs"
    }
  }
}

provider "provs" {
  path = "/var/tmp/custom_tf_provider"
}

resource "provs_write_only_test" "test" {
  attr = "attr val"
}

Expected Behavior

Have this warning printed:
Image

Actual Behavior

The warning is not shown.

Image

Steps to Reproduce

  1. terraform plan

Logs

No response

Additional Information

This is due to the configuration of the validator. The client capabilities are not passed into the validator struct and the writeOnly capability is always false. Due to this, the validator is always skipped.

I already have a fix for this, but as suggested in the contribution guide, before opening a PR, I opened an issue first to understand if this behavior is actually intended for the moment or it is truly a bug.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions