hashicorp / terraform-plugin-codegen-openapi

OpenAPI to Terraform Provider Code Generation Specification
Mozilla Public License 2.0
49 stars 9 forks source link

Normalize property names from OAS into valid Terraform attribute name #58

Closed austinvalle closed 11 months ago

austinvalle commented 11 months ago

Background

Currently, when an OAS is mapped to provider code specification, we use the property names directly from the JSON schema properties as the attribute name. Example:

OAS

"Pet": {
    "required": [
        "photoUrls"
    ],
    "type": "object",
    "properties": {
        // .. other properties
        "photoUrls": {
            "type": "array",
            "items": {
                "type": "string"
            }
        }
    }
},

Provider Code Spec

{
    "name": "pet",
    "schema": {
        "attributes": [
            // .. other properties
            {
                "name": "photoUrls",
                "list": {
                    "computed_optional_required": "required",
                    "element_type": {
                        "string": {}
                    }
                }
            }
         ]
    }
}

Here, photoUrls is not a valid Terraform attribute identifier and must match the regex ^[a-z_][a-z0-9_]*$:

Solution

It seems unrealistic to expect an OpenAPI spec to account for Terraform attribute naming, so we should attempt to normalize property names before serializing. This could prove tricky as the configuration has some options that reference OAS attributes, such as schema -> attributes -> aliases and schema -> attributes -> overrides.

As for how/what to normalize, we'll need to make pragmatic decisions on how far to go with the normalization, but an initial proposal:

We should also consider how to handle special characters that can't be normalized, like <extra_key>, and whether we want to remove those characters -> extra_key

We will also run into the problem of potential conflicts when normalizing a property name:

"Pet": {
    "required": [
        "photoUrls"
    ],
    "type": "object",
    "properties": {
        // .. other properties
        "photo_urls": {
            "type": "array",
            "items": {
                "type": "string"
            }
        },
        // this would be normalized to photo_urls, conflict!
        "photoUrls": {
            "type": "array",
            "items": {
                "type": "string"
            }
        }
    }
},
github-actions[bot] commented 3 months ago

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