hashicorp / terraform-plugin-codegen-framework

Terraform Provider Code Generation Specification to Framework
Mozilla Public License 2.0
39 stars 16 forks source link

Custom types have little effect when specified for attributes of nested objects #147

Open cysp opened 4 months ago

cysp commented 4 months ago

Use Cases or Problem Statement

I'm implementing a provider containing a resource that has an attribute modelled as a list of nested objects, where an attribute of the nested object is a JSON value. Unfortunately the upstream API for this resource occasionally returns this JSON field with keys reordered, so I would like to model that nested field using jsontypes to apply semantic equality and avoid meaningless diffs.

I tried specifying a custom type in the specification for this nested attribute but that seems to only affect the generated schema, not the model structs generated for nested objects. This is evident in the lack of semantic equality behaviour for these nested attributes. The difference in generated code is visible in this commit: https://github.com/cysp/terraform-plugin-codegen-framework-nested-custom-types/commit/1a59ffe8946463d088f56bb34513d2fc6bb0e2f7 Note that the top-level json field's type in ResourceModel is affected by the custom type but the model fields for nested objects show no change.

Proposal

Hand-editing the generated model structs to replace basetypes.String{Type,Value} with the custom jsontypes representations gives the expected behaviour, and looking at the code generator it looks like there's a fairly simple (but widespread) change to make to respect custom type specifications. This change to the code generator implements custom types for string attributes in nested contexts: https://github.com/hashicorp/terraform-plugin-codegen-framework/compare/main...cysp:terraform-plugin-codegen-framework:custom-nested-object-attribute-types … which results in this change in the reproduction example linked above: https://github.com/cysp/terraform-plugin-codegen-framework-nested-custom-types/compare/main...custom-nested-object-attribute-types

Additional Information

No response

Code of Conduct