Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid panic on attribute NilType (nested attributes) #44

Merged
merged 2 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"}}}}}