hashicorp / terraform-plugin-codegen-openapi

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

Support for Any Type schema #82

Open pauly4it opened 9 months ago

pauly4it commented 9 months ago

Our OpenAPI spec contains quite a few Any Type/arbitrary-type schemas.

Currently, the codegen tool skips a resource in the generator config because one of the child schemas is an Any Type, returning the following message: time=2023-10-20T16:27:09.841-06:00 level=WARN msg="skipping resource schema mapping" resource=system oas_path=decision_mappings.allowed.expected oas_line_number=5019 err="no 'type' array or supported allOf, oneOf, anyOf constraint - attribute cannot be created"

Example:

systems.v1.AllowedMapping:
  properties:
    expected:
      $ref: '#/components/schemas/systems.v1.AllowedMapping.expected'
    negated:
      default: false
      description: when set to true, decision is Allowed when the mapped property IS NOT equal to the expected value
      type: boolean
    path:
      description: dot-separated decision property path
      type: string
  required:
    - path
systems.v1.AllowedMapping.expected: {}

In the above example from our spec, expected can by any type of value based on the user's custom configuration (e.g, true, 200, "allowed", {"allowed": true, "errors": null}, etc.).

austinvalle commented 8 months ago

Hey there @pauly4it 👋🏻 ,

To provide some initial expectations, there are a couple dependencies that we need to resolve before this functionality can be added to codegen-openapi:

Once those are in-place, adding this functionality to codegen-openapi will be possible and we can consider this enhancement. I'll add this issue to our backlog to track 👍🏻

pauly4it commented 8 months ago

@austinvalle could we potentially treat an Any Type as a string?

I was thinking more about how a user may configure that expected attribute from my example in a TF HCL file, and it could be something like:

resource "system" "example" {
  # ... other system resource config params

          expected = <<EOF
{"allowed": true, "errors": null}
EOF

  # ... rest of system resource config
}
austinvalle commented 8 months ago

Hmmm 🤔,

While it may be possible to represent this use-case of Any as a string, I'm not sure that'd be expected for all cases. I'm wondering if maybe in the future we could extend the overrides config block to allow explicitly setting the type.

data_sources:
  system:
    read:
      path: /system
      method: GET
    schema:
      attributes:
        overrides:
          expected:
            type: string

For what you described above, a string type would work, but you might even want to use the jsontypes.Normalized type, which is built on top of the string type: https://github.com/hashicorp/terraform-plugin-framework-jsontypes.

pauly4it commented 8 months ago

Overrides of attribute type would definitely help solve the problem in these types of cases. Combined with #81, it could add more flexibility into the configuration.

bflad commented 2 months ago

Good news here is that the first part of this, terraform-plugin-framework support for dynamic types and attributes is available. The other code generation projects will need to be updated with that support then hopefully this can be supported.