Recurisve schema using terraform plugin framework

Hi, I am trying to create a resource schema for a struct that looks like this

type Rule struct {
	Type       string      `json:"type"`
	Rules      []Rule      `json:"rules"`
	Conditions []Condition `json:"conditions"`

But I can’t find a way to do that;

Hi @gagan.trivedi :wave: Thank you for asking this.

Terraform’s schema system does not have “direct” support for recursive schemas – instead it must be statically defined for each level of nesting. Providers that have run into this situation have typically resorted to introducing a few levels of the recursion into the schema definition. The aws_wafv2_rule_group resource statement block comes to mind: Terraform Registry where it is limited to 3 levels of recursion in Terraform configurations. The code is setup to generate the schema for it here: terraform-provider-aws/rule_group.go at b4a9f93a2b7323202c8904e86cff03d3f2cb006b · hashicorp/terraform-provider-aws · GitHub

Another potentially less practitioner-friendly option is to treat that information as a JSON string value, where it is up to the API to handle validation, but the Terraform schema system is no longer in the way. Practitioners in this case could potentially be burdened with using the jsonencode() function to convert other Terraform data for this particular value.

Aside: It is also technically possible to use the low-level DynamicPsuedoType support available only in terraform-plugin-go to partially workaround the static definition issue, however it is very complex to setup a terraform-plugin-go based resource and using that type of schema means that many of the nicer features of the static schema system must be manually reimplemented. I only mention this if the lack of full recursion might be a blocking use case.