Skip to content

Commit d1018bf

Browse files
authored
Avoid panic on attribute NilType (nested attributes) (#44)
* add panicking test * avoid panic on attribute NilType
1 parent 1b370c6 commit d1018bf

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

schemas.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,36 @@ type SchemaAttribute struct {
223223
Sensitive bool `json:"sensitive,omitempty"`
224224
}
225225

226+
type jsonSchemaAttribute struct {
227+
AttributeType json.RawMessage `json:"type,omitempty"`
228+
AttributeNestedType *SchemaNestedAttributeType `json:"nested_type,omitempty"`
229+
Description string `json:"description,omitempty"`
230+
DescriptionKind SchemaDescriptionKind `json:"description_kind,omitempty"`
231+
Deprecated bool `json:"deprecated,omitempty"`
232+
Required bool `json:"required,omitempty"`
233+
Optional bool `json:"optional,omitempty"`
234+
Computed bool `json:"computed,omitempty"`
235+
Sensitive bool `json:"sensitive,omitempty"`
236+
}
237+
238+
func (as *SchemaAttribute) MarshalJSON() ([]byte, error) {
239+
jsonSa := &jsonSchemaAttribute{
240+
AttributeNestedType: as.AttributeNestedType,
241+
Description: as.Description,
242+
DescriptionKind: as.DescriptionKind,
243+
Deprecated: as.Deprecated,
244+
Required: as.Required,
245+
Optional: as.Optional,
246+
Computed: as.Computed,
247+
Sensitive: as.Sensitive,
248+
}
249+
if as.AttributeType != cty.NilType {
250+
attrTy, _ := as.AttributeType.MarshalJSON()
251+
jsonSa.AttributeType = attrTy
252+
}
253+
return json.Marshal(jsonSa)
254+
}
255+
226256
// SchemaNestedAttributeType describes a nested attribute
227257
// which could also be just expressed simply as cty.Object(...),
228258
// cty.List(cty.Object(...)) etc. but this allows tracking additional

schemas_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,20 @@ func TestProviderSchemasValidate(t *testing.T) {
2222
t.Fatal(err)
2323
}
2424
}
25+
26+
func TestProviderSchemasValidate_nestedAttributes(t *testing.T) {
27+
f, err := os.Open("testdata/nested_attributes/schemas.json")
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
defer f.Close()
32+
33+
var schemas *ProviderSchemas
34+
if err := json.NewDecoder(f).Decode(&schemas); err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
if err := schemas.Validate(); err != nil {
39+
t.Fatal(err)
40+
}
41+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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 commit comments

Comments
 (0)