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 automatic id attribute behaviors under blocks #365

Merged
merged 2 commits into from
Apr 26, 2024

Conversation

bflad
Copy link
Contributor

@bflad bflad commented Apr 26, 2024

Closes #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.

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 bflad added the bug Something isn't working label Apr 26, 2024
@bflad bflad added this to the v0.19.2 milestone Apr 26, 2024
@bflad bflad requested a review from a team as a code owner April 26, 2024 15:12
Copy link
Member

@austinvalle austinvalle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@bflad bflad merged commit 14eebdd into main Apr 26, 2024
6 checks passed
@bflad bflad deleted the bflad/id-special-handling branch April 26, 2024 17:45
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 27, 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
Development

Successfully merging this pull request may close these issues.

The plugin has issue with id field. It marks all id fields as readonly
2 participants