Skip to content

Add support for sets with custom OAS format field #25

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

Merged
merged 8 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ The response body schema found will be deep merged with the query/path `paramete

For a given [OAS type](https://spec.openapis.org/oas/v3.1.0#data-types) and format combination, the following rules will be applied for mapping to Framework attribute types. Not all Framework types are represented natively with OAS, those types are noted below in [Unsupported Attribute Types](#unsupported-attribute-types).

> **NOTE:** All `Type` and `Format` fields below are native to OpenAPI Spec 3.x, with the exception of the format `set`, which is a custom field that only this generator tool is expected to support.

| Type (OAS) | Format (OAS) | Items type (OAS `array`) | Plugin Framework Attribute Type |
|------------|---------------------|--------------------------|---------------------------------------------------------------------------------------------|
| `integer` | - | - | `Int64Attribute` |
Expand All @@ -77,20 +79,22 @@ For a given [OAS type](https://spec.openapis.org/oas/v3.1.0#data-types) and form
| `string` | - | - | `StringAttribute` |
| `boolean` | - | - | `BoolAttribute` |
| `array` | - | `object` | `ListNestedAttribute` |
| `array` | - | (all others) | `ListAttribute` (nests with [element types](#oas-to-plugin-framework-element-types)) |
| `array` | - | (all others) | `ListAttribute` (nests with [element types](#oas-to-plugin-framework-element-types)) |
| `array` | `set` | `object` | `SetNestedAttribute` |
| `array` | `set` | (all others) | `SetAttribute` (nests with [element types](#oas-to-plugin-framework-element-types)) |
| `object` | - | - | `SingleNestedAttribute` |

#### Unsupported Attribute Types
- `ListNestedBlock`, `SetNestedBlock`, and `SingleNestedBlock`
- While the Plugin Framework supports blocks, the Plugin Framework team encourages provider developers to prefer `ListNestedAttribute`, `SetNestedAttribute`, and `SingleNestedAttribute` for new provider development.
- `ObjectAttribute`
- The generator will default to `SingleNestedAttribute` for object types to provide the additional schema information.
- `SetNestedAttribute`, `SetAttribute`, `MapNestedAttribute`, and `MapAttribute`
- `MapNestedAttribute`, and `MapAttribute`
- Mapping for these types is currently not supported, but will be considered in future versions.

### OAS to Plugin Framework Element Types

For attributes that dont have additional schema information (currently only `ListAttribute`), the following rules will be applied for mapping from OAS type and format combinations, into Framework element types.
For attributes that don't have additional schema information (currently only `ListAttribute` and `SetAttribute`), the following rules will be applied for mapping from OAS type and format combinations, into Framework element types.

| Type (OAS) | Format (OAS) | Items type (OAS `array`) | Plugin Framework Element Type |
|------------|---------------------|--------------------------|---------------------------------|
Expand All @@ -100,6 +104,7 @@ For attributes that don’t have additional schema information (currently only `
| `string` | - | - | `StringType` |
| `boolean` | - | - | `BoolType` |
| `array` | - | (all) | `ListType` |
| `array` | `set` | (all) | `SetType` |
| `object` | - | - | `ObjectType` |

### Required, Computed, and Optional
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/google/go-cmp v0.5.9
github.com/hashicorp/terraform-plugin-codegen-spec v0.0.0-20230622133831-8ed4d77d321e
github.com/hashicorp/terraform-plugin-codegen-spec v0.0.0-20230623193314-2a16a9d0b6f3
github.com/mattn/go-colorable v0.1.13
github.com/mitchellh/cli v1.1.5
github.com/pb33f/libopenapi v0.8.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/terraform-plugin-codegen-spec v0.0.0-20230622133831-8ed4d77d321e h1:+BPglv17monTwovwckVW/ZXbi3ZzGLanhQvBccSdWSQ=
github.com/hashicorp/terraform-plugin-codegen-spec v0.0.0-20230622133831-8ed4d77d321e/go.mod h1:4aZFKiOI2xiQFOpeQMMyDL8vRmKAZXHUY4kWol7Fdco=
github.com/hashicorp/terraform-plugin-codegen-spec v0.0.0-20230623193314-2a16a9d0b6f3 h1:6IiMR/31Wu/HvEGhLNzx1wSWvYCU2gkRv39sB24Cpis=
github.com/hashicorp/terraform-plugin-codegen-spec v0.0.0-20230623193314-2a16a9d0b6f3/go.mod h1:4aZFKiOI2xiQFOpeQMMyDL8vRmKAZXHUY4kWol7Fdco=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
Expand Down
90 changes: 89 additions & 1 deletion internal/cmd/testdata/edgecase/generated_framework_ir.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,97 @@
}
]
}
},
{
"name": "set_collections",
"schema": {
"attributes": [
{
"name": "set_prop",
"set": {
"computed_optional_required": "computed_optional",
"element_type": {
"string": {}
},
"description": "This is a set of strings"
}
},
{
"name": "setnested_prop",
"set_nested": {
"computed_optional_required": "computed_optional",
"nested_object": {
"attributes": [
{
"name": "bool_prop",
"bool": {
"computed_optional_required": "computed_optional",
"description": "Bool inside a set!"
}
},
{
"name": "string_prop",
"string": {
"computed_optional_required": "computed_optional",
"description": "String inside a set!",
"sensitive": false
}
}
]
},
"description": "This is a set with a nested object"
}
}
]
}
}
],
"provider": {
"name": "edgecase"
}
},
"resources": [
{
"name": "set_collections",
"schema": {
"attributes": [
{
"name": "set_prop",
"set": {
"computed_optional_required": "computed_optional",
"element_type": {
"string": {}
},
"description": "This is a set of strings"
}
},
{
"name": "setnested_prop",
"set_nested": {
"computed_optional_required": "required",
"nested_object": {
"attributes": [
{
"name": "bool_prop",
"bool": {
"computed_optional_required": "computed_optional",
"description": "Bool inside a set!"
}
},
{
"name": "string_prop",
"string": {
"computed_optional_required": "computed_optional",
"description": "String inside a set!",
"sensitive": false
}
}
]
},
"description": "This is a set with a nested object"
}
}
]
}
}
]
}
67 changes: 60 additions & 7 deletions internal/cmd/testdata/edgecase/openapi_spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,72 @@ paths:
get:
summary: Test for nested lists (list within a list)
responses:
'200':
"200":
description: OK
content:
application/json:
schema:
type: object
properties:
main_list:
description: This list has another list nested underneath!
$ref: "#/components/schemas/multidimensional_array_schema"
/set_collections:
get:
summary: Test for SetNested attributes and Set attributes in a data source
responses:
"200":
description: OK
content:
application/json:
schema:
type: object
properties:
setnested_prop:
$ref: "#/components/schemas/setnested_schema"
set_prop:
description: This is a set of strings
type: array
format: set
items:
type: array
items:
type: string
required:
- main_list
type: string
post:
summary: Test for SetNested attributes and Set attributes in a resource
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- setnested_prop
properties:
setnested_prop:
$ref: "#/components/schemas/setnested_schema"
set_prop:
description: This is a set of strings
type: array
format: set
items:
type: string
components:
schemas:
multidimensional_array_schema:
description: This list has another list nested underneath!
type: array
items:
type: array
items:
type: string
setnested_schema:
description: This is a set with a nested object
type: array
format: set
items:
type: object
properties:
string_prop:
description: String inside a set!
type: string
bool_prop:
description: Bool inside a set!
type: boolean
12 changes: 11 additions & 1 deletion internal/cmd/testdata/edgecase/tfopenapigen_config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
# Ref: https://registry.terraform.io/providers/integrations/github/latest/docs
provider:
name: edgecase

resources:
set_collections:
create:
path: /set_collections
method: POST
read:
path: /set_collections
method: GET

data_sources:
nested_lists:
read:
path: /nested_lists
method: GET
set_collections:
read:
path: /set_collections
method: GET
4 changes: 2 additions & 2 deletions internal/mapper/oas/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *OASSchema) BuildResourceAttribute(name string, computability schema.Com
case util.OAS_type_boolean:
return s.BuildBoolResource(name, computability)
case util.OAS_type_array:
return s.BuildListResource(name, computability)
return s.BuildCollectionResource(name, computability)
case util.OAS_type_object:
return s.BuildSingleNestedResource(name, computability)
default:
Expand Down Expand Up @@ -94,7 +94,7 @@ func (s *OASSchema) BuildDataSourceAttribute(name string, computability schema.C
case util.OAS_type_boolean:
return s.BuildBoolDataSource(name, computability)
case util.OAS_type_array:
return s.BuildListDataSource(name, computability)
return s.BuildCollectionDataSource(name, computability)
case util.OAS_type_object:
return s.BuildSingleNestedDataSource(name, computability)
default:
Expand Down
Loading