Skip to content

Commit

Permalink
Avoid panic on attribute NilType (nested attributes) (#44)
Browse files Browse the repository at this point in the history
* add panicking test

* avoid panic on attribute NilType
  • Loading branch information
radeksimko committed Oct 1, 2021
1 parent 1b370c6 commit d1018bf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
30 changes: 30 additions & 0 deletions schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,36 @@ type SchemaAttribute struct {
Sensitive bool `json:"sensitive,omitempty"`
}

type jsonSchemaAttribute struct {
AttributeType json.RawMessage `json:"type,omitempty"`
AttributeNestedType *SchemaNestedAttributeType `json:"nested_type,omitempty"`
Description string `json:"description,omitempty"`
DescriptionKind SchemaDescriptionKind `json:"description_kind,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Required bool `json:"required,omitempty"`
Optional bool `json:"optional,omitempty"`
Computed bool `json:"computed,omitempty"`
Sensitive bool `json:"sensitive,omitempty"`
}

func (as *SchemaAttribute) MarshalJSON() ([]byte, error) {
jsonSa := &jsonSchemaAttribute{
AttributeNestedType: as.AttributeNestedType,
Description: as.Description,
DescriptionKind: as.DescriptionKind,
Deprecated: as.Deprecated,
Required: as.Required,
Optional: as.Optional,
Computed: as.Computed,
Sensitive: as.Sensitive,
}
if as.AttributeType != cty.NilType {
attrTy, _ := as.AttributeType.MarshalJSON()
jsonSa.AttributeType = attrTy
}
return json.Marshal(jsonSa)
}

// SchemaNestedAttributeType describes a nested attribute
// which could also be just expressed simply as cty.Object(...),
// cty.List(cty.Object(...)) etc. but this allows tracking additional
Expand Down
17 changes: 17 additions & 0 deletions schemas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ func TestProviderSchemasValidate(t *testing.T) {
t.Fatal(err)
}
}

func TestProviderSchemasValidate_nestedAttributes(t *testing.T) {
f, err := os.Open("testdata/nested_attributes/schemas.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()

var schemas *ProviderSchemas
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
t.Fatal(err)
}

if err := schemas.Validate(); err != nil {
t.Fatal(err)
}
}
1 change: 1 addition & 0 deletions testdata/nested_attributes/schemas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format_version":"0.2","provider_schemas":{"registry.terraform.io/hashicorp/awscc":{"provider":{"version":0,"block":{"attributes":{"access_key":{"type":"string","description_kind":"plain","optional":true},"assume_role":{"nested_type":{"attributes":{"duration":{"type":"string","description_kind":"plain","optional":true},"external_id":{"type":"string","description_kind":"plain","optional":true}},"nesting_mode":"single"},"description_kind":"plain","optional":true}},"description_kind":"plain"}}}}}

0 comments on commit d1018bf

Please sign in to comment.