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

Incorrect labels in generated markdown for certain deeply-nested schemas #380

Closed
1 task done
henryrecker-pingidentity opened this issue May 29, 2024 · 3 comments · Fixed by #382
Closed
1 task done
Assignees
Labels
bug Something isn't working
Milestone

Comments

@henryrecker-pingidentity

Terraform CLI and terraform-plugin-docs Versions

$ terraform version
Terraform v1.8.3
on darwin_amd64
github.com/hashicorp/terraform-plugin-docs v0.19.3

Provider Code

func (r *exampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
	resp.Schema = schema.Schema{
		Description: "Example resource.",
		Attributes: map[string]schema.Attribute{
			"level_one": schema.SingleNestedAttribute{
				Required: true,
				Attributes: map[string]schema.Attribute{
					"level_two": schema.SingleNestedAttribute{
						Optional: true,
						Attributes: map[string]schema.Attribute{
							"level_three": schema.SingleNestedAttribute{
								Optional: true,
								Attributes: map[string]schema.Attribute{
									"level_four_primary": schema.SingleNestedAttribute{
										Attributes: map[string]schema.Attribute{
											"level_four_primary_string": schema.StringAttribute{
												Optional:    true,
												Description: "Parent should be level_one.level_two.level_three.level_four_primary.",
											},
											"level_five": schema.SingleNestedAttribute{
												Attributes: map[string]schema.Attribute{
													"level_five_string": schema.StringAttribute{
														Optional:    true,
														Description: "Parent should be level_one.level_two.level_three.level_four_primary.level_five.",
													},
												},
												Optional:    true,
												Description: "Parent should be level_one.level_two.level_three.level_four_primary.",
											},
										},
										Optional: true,
									},
									"level_four_secondary": schema.StringAttribute{
										Optional: true,
									},
								},
							},
						},
					},
				},
			},
		},
	}
}

Expected Behavior

Labels in generated documentation should be correct, including names of parent attributes up the chain

Actual Behavior

Links in the generated example.md file go to the right place, but the labels of the sections can be wrong. With this schema, one label treats an adjacent StringAttribute as if it was the parent SingleNestedAttribute.

<!-- schema generated by tfplugindocs -->
## Schema

### Required

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

<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_secondary--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_secondary--level_five"></a>
### Nested Schema for `level_one.level_two.level_three.level_four_secondary.level_five`

Optional:

- `level_five_string` (String) Parent should be level_one.level_two.level_three.level_four_primary.level_five.

Note the "Nested Schema for level_one.level_two.level_three.level_four_secondary.level_five": that level_four_secondary attribute is just a string, it can't have child attributes, yet the generated doc includes it as a parent in the label for the nested schema.

I'm not sure whether or not this is the simplest schema to reproduce this issue. I tried several variations. It seems that this can happen under ListNested attributes as well at minimum. I tried reducing the nesting from the amount in my example here but then the bug stopped occurring, so it may somehow be related to very deep nesting.

Steps to Reproduce

  1. tfplugindocs generate with a provider with a resource with the given schema

The code I copied here as well as the generated markdown can also be viewed in the DocsBug branch linked here - https://github.com/henryrecker-pingidentity/terraform-provider-example/tree/DocsBug

How much impact is this issue causing?

Medium

Logs

No response

Additional Information

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@henryrecker-pingidentity henryrecker-pingidentity added the bug Something isn't working label May 29, 2024
@henryrecker-pingidentity
Copy link
Author

henryrecker-pingidentity commented May 29, 2024

It looks like this was supposed to be fixed in v0.19.0. If I run with v0.18.0, I see one additional incorrect label. At v0.19.0 or later, I only see the one incorrect label I described in the issue.

@bflad bflad self-assigned this May 31, 2024
bflad added a commit that referenced this issue Jun 3, 2024
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.",
              }, "")
```
@bflad bflad added this to the v0.20.0 milestone Jun 3, 2024
@bflad
Copy link
Contributor

bflad commented Jun 3, 2024

Hi @henryrecker-pingidentity 👋 Thank you for submitting this bug report and sorry for the frustrating behavior. Very much appreciate the full reproduction setup as that made tracking this down so much easier.

It looks like there was some errant reuse of attribute paths in the generator code, which should be resolved with #382. 👍

@bflad bflad modified the milestones: v0.20.0, v0.19.4 Jun 3, 2024
@bflad bflad closed this as completed in #382 Jun 3, 2024
bflad added a commit that referenced this issue Jun 3, 2024
…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.",
              }, "")
```
@bflad
Copy link
Contributor

bflad commented Jun 3, 2024

This will likely be released tomorrow in a terraform-plugin-docs v0.19.4 release. 👍

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
2 participants