Skip to content

Commit 3870756

Browse files
authored
generate: Prevent incorrect attribute association with very deep nesting (#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.",   }, "") ```
1 parent c656571 commit 3870756

File tree

5 files changed

+141
-2
lines changed

5 files changed

+141
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: 'generate: Prevented incorrect attribute paths with nested attributes that contain multiple attributes'
3+
time: 2024-06-03T09:29:11.314279-07:00
4+
custom:
5+
Issue: "380"

internal/schemamd/render.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ func writeObjectChildren(w io.Writer, parents []string, ty cty.Type, group group
466466

467467
for _, name := range sortedNames {
468468
att := atts[name]
469-
path := append(parents, name)
469+
path := make([]string, len(parents), len(parents)+1)
470+
copy(path, parents)
471+
path = append(path, name)
470472

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

523525
for _, name := range names {
524526
att := nestedAttributes.Attributes[name]
525-
path := append(parents, name)
527+
path := make([]string, len(parents), len(parents)+1)
528+
copy(path, parents)
529+
path = append(path, name)
526530

527531
nt, err := writeAttribute(w, path, att, group)
528532
if err != nil {

internal/schemamd/render_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ func TestRender(t *testing.T) {
4848
"testdata/framework_types.schema.json",
4949
"testdata/framework_types.md",
5050
},
51+
{
52+
// Reference: https://github.com/hashicorp/terraform-plugin-docs/issues/380
53+
"deep_nested_attributes",
54+
"testdata/deep_nested_attributes.schema.json",
55+
"testdata/deep_nested_attributes.md",
56+
},
5157
} {
5258
c := c
5359
t.Run(c.name, func(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Schema
2+
3+
### Required
4+
5+
- `level_one` (Attributes) (see [below for nested schema](#nestedatt--level_one))
6+
7+
### Read-Only
8+
9+
- `id` (String) Example identifier
10+
11+
<a id="nestedatt--level_one"></a>
12+
### Nested Schema for `level_one`
13+
14+
Optional:
15+
16+
- `level_two` (Attributes) (see [below for nested schema](#nestedatt--level_one--level_two))
17+
18+
<a id="nestedatt--level_one--level_two"></a>
19+
### Nested Schema for `level_one.level_two`
20+
21+
Optional:
22+
23+
- `level_three` (Attributes) (see [below for nested schema](#nestedatt--level_one--level_two--level_three))
24+
25+
<a id="nestedatt--level_one--level_two--level_three"></a>
26+
### Nested Schema for `level_one.level_two.level_three`
27+
28+
Optional:
29+
30+
- `level_four_primary` (Attributes) (see [below for nested schema](#nestedatt--level_one--level_two--level_three--level_four_primary))
31+
- `level_four_secondary` (String)
32+
33+
<a id="nestedatt--level_one--level_two--level_three--level_four_primary"></a>
34+
### Nested Schema for `level_one.level_two.level_three.level_four_primary`
35+
36+
Optional:
37+
38+
- `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))
39+
- `level_four_primary_string` (String) Parent should be level_one.level_two.level_three.level_four_primary.
40+
41+
<a id="nestedatt--level_one--level_two--level_three--level_four_primary--level_five"></a>
42+
### Nested Schema for `level_one.level_two.level_three.level_four_primary.level_five`
43+
44+
Optional:
45+
46+
- `level_five_string` (String) Parent should be level_one.level_two.level_three.level_four_primary.level_five.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"version": 0,
3+
"block": {
4+
"attributes": {
5+
"id": {
6+
"type": "string",
7+
"description": "Example identifier",
8+
"description_kind": "markdown",
9+
"computed": true
10+
},
11+
"level_one": {
12+
"nested_type": {
13+
"attributes": {
14+
"level_two": {
15+
"nested_type": {
16+
"attributes": {
17+
"level_three": {
18+
"nested_type": {
19+
"attributes": {
20+
"level_four_primary": {
21+
"nested_type": {
22+
"attributes": {
23+
"level_five": {
24+
"nested_type": {
25+
"attributes": {
26+
"level_five_string": {
27+
"type": "string",
28+
"description": "Parent should be level_one.level_two.level_three.level_four_primary.level_five.",
29+
"description_kind": "plain",
30+
"optional": true
31+
}
32+
},
33+
"nesting_mode": "single"
34+
},
35+
"description": "Parent should be level_one.level_two.level_three.level_four_primary.",
36+
"description_kind": "plain",
37+
"optional": true
38+
},
39+
"level_four_primary_string": {
40+
"type": "string",
41+
"description": "Parent should be level_one.level_two.level_three.level_four_primary.",
42+
"description_kind": "plain",
43+
"optional": true
44+
}
45+
},
46+
"nesting_mode": "single"
47+
},
48+
"description_kind": "plain",
49+
"optional": true
50+
},
51+
"level_four_secondary": {
52+
"type": "string",
53+
"description_kind": "plain",
54+
"optional": true
55+
}
56+
},
57+
"nesting_mode": "single"
58+
},
59+
"description_kind": "plain",
60+
"optional": true
61+
}
62+
},
63+
"nesting_mode": "single"
64+
},
65+
"description_kind": "plain",
66+
"optional": true
67+
}
68+
},
69+
"nesting_mode": "single"
70+
},
71+
"description_kind": "plain",
72+
"required": true
73+
}
74+
},
75+
"description": "Example resource",
76+
"description_kind": "markdown"
77+
}
78+
}

0 commit comments

Comments
 (0)