hashicorp / terraform-plugin-codegen-openapi

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

Add support for maps based on `additionalProperties` field #27

Closed austinvalle closed 1 year ago

austinvalle commented 1 year ago

Context

RFC: https://docs.google.com/document/d/15PJokoO7mkMYsc2Z7DN1copykQgH5Kt7nnzb-mcNcTo/edit#heading=h.4syvri1n60bk

References:

This PR adds support for:

I also added documentation to the design doc and added unit/integration tests to cover since this there aren't any use-cases in existing OAS tests.

Details

additionalProperties indicates that key names in a schema can be any string, with a value defined by the schema defined in additionalProperties.type.

Examples

This works for simple maps that have primitive element types (MapAttribute)

// Keys can be any string, value can be any string (map of strings)
{
  "type": "object",
  "additionalProperties": { 
    "type": "string" 
  }
}

This also works for more complex maps that have objects (MapNestedAttribute)

// Keys can be any string, value can be an object that matches the schema
{
  "type": "object",
  "additionalProperties": { 
    "type": "object",
    "properties": {
        // ... define object for value of map
    }
  }
}

Finally, it can also exist as a MapType in an array:

// Array with items, keys can be any string, value can be any string (map of strings)
{
  "type": "array",
  "items": {
    "type": "object"
    "additionalProperties": {
      "type": "string"
    }
  }
}

I wrote integration tests to cover this specific functionality as the existing OAS do not cover this use-case 👍🏻

Notes:

github-actions[bot] commented 3 months ago

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.