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

The plugin has issue with id field. It marks all id fields as readonly #335

Closed
1 task done
komalkadam-dc opened this issue Jan 31, 2024 · 1 comment · Fixed by #365
Closed
1 task done

The plugin has issue with id field. It marks all id fields as readonly #335

komalkadam-dc opened this issue Jan 31, 2024 · 1 comment · Fixed by #365
Assignees
Labels
bug Something isn't working
Milestone

Comments

@komalkadam-dc
Copy link

Terraform CLI and terraform-plugin-docs Versions

terraform version Terraform v1.6.6 on darwin_arm64

github.com/hashicorp/terraform-plugin-docs v0.18.0

Provider Code

https://registry.terraform.io/providers/duplocloud/duplocloud/latest/docs/resources/plan_certificates#certificate

Expected Behavior

Marking the fields id as read only even if the are nested
https://registry.terraform.io/providers/duplocloud/duplocloud/latest/docs/resources/plan_certificates#id

Actual Behavior

https://registry.terraform.io/providers/duplocloud/duplocloud/latest/docs/resources/plan_certificates#id

Steps to Reproduce

  1. tfplugindocs generate --flags

How much impact is this issue causing?

Low

Logs

No response

Additional Information

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@komalkadam-dc komalkadam-dc added the bug Something isn't working label Jan 31, 2024
@bflad
Copy link
Member

bflad commented Apr 26, 2024

Hi @komalkadam-dc 👋 Thank you for reporting this issue and sorry for the trouble here.

Looking at the internal logic, there is some special handling for id attributes and it seems like it does not take into account whether that id attribute is at the root of the schema or not. Root level id attributes were special in terraform-plugin-sdk and Terraform versions before 0.12, which is likely why this code was in here. Hopefully adding a quick root level check should resolve this. 👍

@bflad bflad added this to the v0.19.2 milestone Apr 26, 2024
@bflad bflad self-assigned this Apr 26, 2024
bflad added a commit that referenced this issue Apr 26, 2024
Reference: #335

Previously, the automatic `id` attribute handling intended for root level schema blocks was applied to all blocks, causing the generator to errantly set the grouping to `Read-Only` and the description to `The ID of this resource`. This was captured via a new integration test:

```
--- FAIL: Test_SchemaJson_GenerateAcceptanceTests (0.00s)
    --- FAIL: Test_SchemaJson_GenerateAcceptanceTests/nested_id_attribute (0.25s)
        testscript.go:558: # Copyright (c) HashiCorp, Inc.
            # SPDX-License-Identifier: MPL-2.0
            # Ensure only root level id attribute receives automatic description (0.236s)
            > [!unix] skip
            > exec tfplugindocs --provider-name=terraform-provider-scaffolding --providers-schema=schema.json
            [stdout]
            rendering website for provider "terraform-provider-scaffolding" (as "terraform-provider-scaffolding")
            exporting schema from JSON file
            getting provider schema
            generating missing templates
            generating missing resource content
            generating new template for "scaffolding_example"
            generating missing data source content
            generating missing function content
            generating missing provider content
            generating new template for "terraform-provider-scaffolding"
            rendering static website
            cleaning rendered website dir
            rendering templated website to static markdown
            rendering "index.md.tmpl"
            rendering "resources/example.md.tmpl"
            > cmp stdout expected-output.txt
            > cmp docs/index.md expected-index.md
            > cmp docs/resources/example.md expected-resource.md
            diff docs/resources/example.md expected-resource.md
            --- docs/resources/example.md
            +++ expected-resource.md
            @@ -48,17 +48,17 @@
             <a id="nestedblock--list_nested_block_optional_id"></a>
             ### Nested Schema for `list_nested_block_optional_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Optional:
            +
            +- `id` (String)

             <a id="nestedblock--list_nested_block_required_id"></a>
             ### Nested Schema for `list_nested_block_required_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Required:
            +
            +- `id` (String)

             <a id="nestedatt--optional_object_attribute"></a>
            @@ -72,33 +72,33 @@
             <a id="nestedblock--set_nested_block_optional_id"></a>
             ### Nested Schema for `set_nested_block_optional_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Optional:
            +
            +- `id` (String)

             <a id="nestedblock--set_nested_block_required_id"></a>
             ### Nested Schema for `set_nested_block_required_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Required:
            +
            +- `id` (String)

             <a id="nestedblock--single_nested_block_optional_id"></a>
             ### Nested Schema for `single_nested_block_optional_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Optional:
            +
            +- `id` (String)

             <a id="nestedblock--single_nested_block_required_id"></a>
             ### Nested Schema for `single_nested_block_required_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Required:
            +
            +- `id` (String)

             <a id="nestedatt--computed_object_attribute"></a>
            @@ -114,7 +114,7 @@

             Read-Only:

            -- `id` (String) The ID of this resource.
            +- `id` (String)

             <a id="nestedblock--set_nested_block_computed_id"></a>
            @@ -122,7 +122,7 @@

             Read-Only:

            -- `id` (String) The ID of this resource.
            +- `id` (String)

             <a id="nestedblock--single_nested_block_computed_id"></a>
            @@ -130,4 +130,4 @@

             Read-Only:

            -- `id` (String) The ID of this resource.
            +- `id` (String)

            FAIL: testdata/scripts/schema-json/generate/nested_id_attribute.txtar:9: docs/resources/example.md and expected-resource.md differ
```

The schema rendering logic could likely use a full refactoring as it has very high code complexity, but this change is only a very targeted fix for the acute and errant `id` attribute handling behavior.
bflad added a commit that referenced this issue Apr 26, 2024
Reference: #335

Previously, the automatic `id` attribute handling intended for root level schema blocks was applied to all blocks, causing the generator to errantly set the grouping to `Read-Only` and the description to `The ID of this resource`. This was captured via a new integration test:

```
--- FAIL: Test_SchemaJson_GenerateAcceptanceTests (0.00s)
    --- FAIL: Test_SchemaJson_GenerateAcceptanceTests/nested_id_attribute (0.25s)
        testscript.go:558: # Copyright (c) HashiCorp, Inc.
            # SPDX-License-Identifier: MPL-2.0
            # Ensure only root level id attribute receives automatic description (0.236s)
            > [!unix] skip
            > exec tfplugindocs --provider-name=terraform-provider-scaffolding --providers-schema=schema.json
            [stdout]
            rendering website for provider "terraform-provider-scaffolding" (as "terraform-provider-scaffolding")
            exporting schema from JSON file
            getting provider schema
            generating missing templates
            generating missing resource content
            generating new template for "scaffolding_example"
            generating missing data source content
            generating missing function content
            generating missing provider content
            generating new template for "terraform-provider-scaffolding"
            rendering static website
            cleaning rendered website dir
            rendering templated website to static markdown
            rendering "index.md.tmpl"
            rendering "resources/example.md.tmpl"
            > cmp stdout expected-output.txt
            > cmp docs/index.md expected-index.md
            > cmp docs/resources/example.md expected-resource.md
            diff docs/resources/example.md expected-resource.md
            --- docs/resources/example.md
            +++ expected-resource.md
            @@ -48,17 +48,17 @@
             <a id="nestedblock--list_nested_block_optional_id"></a>
             ### Nested Schema for `list_nested_block_optional_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Optional:
            +
            +- `id` (String)

             <a id="nestedblock--list_nested_block_required_id"></a>
             ### Nested Schema for `list_nested_block_required_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Required:
            +
            +- `id` (String)

             <a id="nestedatt--optional_object_attribute"></a>
            @@ -72,33 +72,33 @@
             <a id="nestedblock--set_nested_block_optional_id"></a>
             ### Nested Schema for `set_nested_block_optional_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Optional:
            +
            +- `id` (String)

             <a id="nestedblock--set_nested_block_required_id"></a>
             ### Nested Schema for `set_nested_block_required_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Required:
            +
            +- `id` (String)

             <a id="nestedblock--single_nested_block_optional_id"></a>
             ### Nested Schema for `single_nested_block_optional_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Optional:
            +
            +- `id` (String)

             <a id="nestedblock--single_nested_block_required_id"></a>
             ### Nested Schema for `single_nested_block_required_id`

            -Read-Only:
            -
            -- `id` (String) The ID of this resource.
            +Required:
            +
            +- `id` (String)

             <a id="nestedatt--computed_object_attribute"></a>
            @@ -114,7 +114,7 @@

             Read-Only:

            -- `id` (String) The ID of this resource.
            +- `id` (String)

             <a id="nestedblock--set_nested_block_computed_id"></a>
            @@ -122,7 +122,7 @@

             Read-Only:

            -- `id` (String) The ID of this resource.
            +- `id` (String)

             <a id="nestedblock--single_nested_block_computed_id"></a>
            @@ -130,4 +130,4 @@

             Read-Only:

            -- `id` (String) The ID of this resource.
            +- `id` (String)

            FAIL: testdata/scripts/schema-json/generate/nested_id_attribute.txtar:9: docs/resources/example.md and expected-resource.md differ
```

The schema rendering logic could likely use a full refactoring as it has very high code complexity, but this change is only a very targeted fix for the acute and errant `id` attribute handling behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants