Skip to content

Commit

Permalink
generate: Prevent incorrect attribute association with very deep nest…
Browse files Browse the repository at this point in the history
…ing (#382)

Reference: #380

After introducing the new unit test matching the given schema and expected output, prior to the logic change:

```
--- FAIL: TestRender (0.00s)
    --- FAIL: TestRender/deep_nested_attributes (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-docs/internal/schemamd/render_test.go:91: Unexpected diff (-wanted, +got):   strings.Join({
              	... // 1062 identical bytes
              	"el_two.level_three.level_four_primary. (see [below for nested sc",
              	"hema](#nestedatt--level_one--level_two--level_three--level_four_",
            - 	"prim",
            + 	"second",
              	"ary--level_five))\n- `level_four_primary_string` (String) Parent ",
              	"should be level_one.level_two.level_three.level_four_primary.\n\n<",
              	`a id="nestedatt--level_one--level_two--level_three--level_four_`,
            - 	"prim",
            + 	"second",
              	"ary--level_five\"></a>\n### Nested Schema for `level_one.level_two",
              	".level_three.level_four_",
            - 	"prim",
            + 	"second",
              	"ary.level_five`\n\nOptional:\n\n- `level_five_string` (String) Paren",
              	"t should be level_one.level_two.level_three.level_four_primary.l",
              	"evel_five.",
              }, "")
```
  • Loading branch information
bflad committed Jun 3, 2024
1 parent c656571 commit 3870756
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 2 deletions.
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"
}
}

0 comments on commit 3870756

Please sign in to comment.