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

generate: Prevent incorrect attribute association with very deep nesting #382

Merged
merged 1 commit into from
Jun 3, 2024
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
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20240603-092911.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'generate: Prevented incorrect attribute paths with nested attributes that contain multiple attributes'
time: 2024-06-03T09:29:11.314279-07:00
custom:
Issue: "380"
8 changes: 6 additions & 2 deletions internal/schemamd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,9 @@ func writeObjectChildren(w io.Writer, parents []string, ty cty.Type, group group

for _, name := range sortedNames {
att := atts[name]
path := append(parents, name)
path := make([]string, len(parents), len(parents)+1)
copy(path, parents)
path = append(path, name)

nt, err := writeObjectAttribute(w, path, att, group)
if err != nil {
Expand Down Expand Up @@ -522,7 +524,9 @@ func writeNestedAttributeChildren(w io.Writer, parents []string, nestedAttribute

for _, name := range names {
att := nestedAttributes.Attributes[name]
path := append(parents, name)
path := make([]string, len(parents), len(parents)+1)
copy(path, parents)
path = append(path, name)

nt, err := writeAttribute(w, path, att, group)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/schemamd/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func TestRender(t *testing.T) {
"testdata/framework_types.schema.json",
"testdata/framework_types.md",
},
{
// Reference: https://github.com/hashicorp/terraform-plugin-docs/issues/380
"deep_nested_attributes",
"testdata/deep_nested_attributes.schema.json",
"testdata/deep_nested_attributes.md",
},
} {
c := c
t.Run(c.name, func(t *testing.T) {
Expand Down
46 changes: 46 additions & 0 deletions internal/schemamd/testdata/deep_nested_attributes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Schema

### Required

- `level_one` (Attributes) (see [below for nested schema](#nestedatt--level_one))

### Read-Only

- `id` (String) Example identifier

<a id="nestedatt--level_one"></a>
### Nested Schema for `level_one`

Optional:

- `level_two` (Attributes) (see [below for nested schema](#nestedatt--level_one--level_two))

<a id="nestedatt--level_one--level_two"></a>
### Nested Schema for `level_one.level_two`

Optional:

- `level_three` (Attributes) (see [below for nested schema](#nestedatt--level_one--level_two--level_three))

<a id="nestedatt--level_one--level_two--level_three"></a>
### Nested Schema for `level_one.level_two.level_three`

Optional:

- `level_four_primary` (Attributes) (see [below for nested schema](#nestedatt--level_one--level_two--level_three--level_four_primary))
- `level_four_secondary` (String)

<a id="nestedatt--level_one--level_two--level_three--level_four_primary"></a>
### Nested Schema for `level_one.level_two.level_three.level_four_primary`

Optional:

- `level_five` (Attributes) Parent should be level_one.level_two.level_three.level_four_primary. (see [below for nested schema](#nestedatt--level_one--level_two--level_three--level_four_primary--level_five))
- `level_four_primary_string` (String) Parent should be level_one.level_two.level_three.level_four_primary.

<a id="nestedatt--level_one--level_two--level_three--level_four_primary--level_five"></a>
### Nested Schema for `level_one.level_two.level_three.level_four_primary.level_five`

Optional:

- `level_five_string` (String) Parent should be level_one.level_two.level_three.level_four_primary.level_five.
78 changes: 78 additions & 0 deletions internal/schemamd/testdata/deep_nested_attributes.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"version": 0,
"block": {
"attributes": {
"id": {
"type": "string",
"description": "Example identifier",
"description_kind": "markdown",
"computed": true
},
"level_one": {
"nested_type": {
"attributes": {
"level_two": {
"nested_type": {
"attributes": {
"level_three": {
"nested_type": {
"attributes": {
"level_four_primary": {
"nested_type": {
"attributes": {
"level_five": {
"nested_type": {
"attributes": {
"level_five_string": {
"type": "string",
"description": "Parent should be level_one.level_two.level_three.level_four_primary.level_five.",
"description_kind": "plain",
"optional": true
}
},
"nesting_mode": "single"
},
"description": "Parent should be level_one.level_two.level_three.level_four_primary.",
"description_kind": "plain",
"optional": true
},
"level_four_primary_string": {
"type": "string",
"description": "Parent should be level_one.level_two.level_three.level_four_primary.",
"description_kind": "plain",
"optional": true
}
},
"nesting_mode": "single"
},
"description_kind": "plain",
"optional": true
},
"level_four_secondary": {
"type": "string",
"description_kind": "plain",
"optional": true
}
},
"nesting_mode": "single"
},
"description_kind": "plain",
"optional": true
}
},
"nesting_mode": "single"
},
"description_kind": "plain",
"optional": true
}
},
"nesting_mode": "single"
},
"description_kind": "plain",
"required": true
}
},
"description": "Example resource",
"description_kind": "markdown"
}
}