diff --git a/docs/how_to.md b/docs/how_to.md index 11edb61ef..105b63ae7 100644 --- a/docs/how_to.md +++ b/docs/how_to.md @@ -1079,6 +1079,9 @@ if input is not given, that's the purpose of ```Optional computed``` properties. **NOTE**: - Object properties containing optional computed child properties will also need to include the extension ```x-terraform-computed```. Otherwise the Terraform schema for the object will not be marked as computed and any non expected value change in the child properties will result into diffs. + - Optional properties that are of type `array` that contain a default value are not supported at the moment and the provider + will ignore the Default value when creating the schema for the property. This is due to Terraform not supporting at the moment + [default values to be set in the schema's Default field for TypeList properties](https://github.com/hashicorp/terraform-plugin-sdk/blob/v2.5.0/helper/schema/schema.go#L763). - Computed properties: These properties must contain the ```readOnly``` attribute set. These properties are included in responses but not in requests, and the value is automatically assigned by the API. See example below **computed** diff --git a/openapi/openapi_spec_resource_schema_definition_property.go b/openapi/openapi_spec_resource_schema_definition_property.go index 485897dac..dc808fa08 100644 --- a/openapi/openapi_spec_resource_schema_definition_property.go +++ b/openapi/openapi_spec_resource_schema_definition_property.go @@ -282,7 +282,10 @@ func (s *SpecSchemaDefinitionProperty) terraformSchema() (*schema.Schema, error) // not allow properties with Computed = true having the Default field populated, otherwise the following error will be // thrown at runtime: Default must be nil if computed if !s.isComputed() { - terraformSchema.Default = s.Default + // Terraform does not allow defaults to be set on type list properties, an error (Default is not valid for lists) would be thrown otherwise (https://www.terraform.io/docs/extend/schemas/schema-behaviors.html#default) + if !s.isArrayProperty() { + terraformSchema.Default = s.Default + } } return terraformSchema, nil diff --git a/openapi/openapi_spec_resource_schema_definition_property_test.go b/openapi/openapi_spec_resource_schema_definition_property_test.go index c9bb5943a..4bfded0cc 100644 --- a/openapi/openapi_spec_resource_schema_definition_property_test.go +++ b/openapi/openapi_spec_resource_schema_definition_property_test.go @@ -1457,6 +1457,24 @@ func TestTerraformSchema(t *testing.T) { }) }) + Convey("Given a list type schemaDefinitionProperty that is optional and does have a default value (meaning the value is known at plan time)", t, func() { + s := newListSchemaDefinitionPropertyWithDefaults("slice_property", "", false, false, false, []interface{}{"value1"}, TypeString, nil) + Convey("When terraformSchema is called with a schema definition property that is optional computed", func() { + terraformPropertySchema, err := s.terraformSchema() + Convey("Then the result returned should be the expected one", func() { + So(err, ShouldBeNil) + So(terraformPropertySchema.Optional, ShouldBeTrue) + So(terraformPropertySchema.Required, ShouldBeFalse) + So(terraformPropertySchema.Computed, ShouldBeFalse) + // the schema returned should be configured with no default value since Terraform does not allow defaults to be set on type list properties, an error (Default is not valid for lists) would be thrown otherwise: + // Ref: https://www.terraform.io/docs/extend/schemas/schema-behaviors.html#default + So(terraformPropertySchema.Default, ShouldBeNil) + // ValidateFunc is not yet supported on lists or sets + So(terraformPropertySchema.ValidateDiagFunc, ShouldBeNil) + }) + }) + }) + Convey("Given a schemaDefinitionProperty that is forceNew and immutable ", t, func() { s := newStringSchemaDefinitionProperty("propertyName", "", false, false, false, true, false, true, false, false, "") Convey("When terraformSchema is called with a schema definition property that validation fails due to immutable and forceNew set", func() {