Skip to content

Commit 39b765c

Browse files
authored
Add support for string-able multi-types, allOf overrides, and improve error messaging (#63)
* upgrade to go 1.21 * throw error when method or path not found in OAS * add slog warning for circular references * upgrade spec and add validate warning * update version/validate for spec * remove stdout as default option for output * hook up logging plumbing * add top level logging * warn * add docs for config package * add explorer docs * doc strings * comment cleanup * kubernetes testdata * implement new error handling for properties * fix allOf handling + add stringified multi-type * add license header * update schema composition logic to always error if we can't support it * Revert "update schema composition logic to always error if we can't support it" This reverts commit 553d4ec. * add unit tests for allOf * add doc strings to property error * add docs * add more logic for parent nodes * added helper function for logging
1 parent e9df56c commit 39b765c

24 files changed

+25492
-172
lines changed

DESIGN.md

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,15 @@ As OpenAPI is designed to describe HTTP APIs in general, it doesn't always fully
185185

186186
### Multi-type Support
187187

188-
Generally, [multi-types](https://cswr.github.io/JsonSchema/spec/multiple_types/) are not supported by the generator as the Terraform Plugin Framework does not support multi-types. There is one specific scenario that is supported by the generator and that is any type that is combined with the `null` OAS type, as any Terraform data type can hold a [null](https://developer.hashicorp.com/terraform/plugin/framework/handling-data/terraform-concepts#null-values) value.
188+
Generally, [multi-types](https://cswr.github.io/JsonSchema/spec/multiple_types/) are not supported by the generator as the Terraform Plugin Framework does not support multi-types. There are two specific scenarios that are supported by the generator.
189+
190+
> **Note:** with multi-type support described below, the `description` will be populated from the root-level schema, see examples.
189191

190192
### Nullable Multi-type support
191-
> **Note:** with nullable multi-types, the `description` will be populated from the root-level schema, as shown below.
192193

193-
In an OAS schema, the following keywords defining nullable multi-types are supported (nullable types will follow the same mapping rules [defined above](#oas-types-to-provider-attributes) for the type that is not the `null` type):
194+
If a multi-type is detected where one of the types is `null`, the other type will be used for schema mapping using the same rules [defined above](#oas-types-to-provider-attributes).
194195

195-
#### OAS `type` keyword array
196+
#### Examples with `type` array
196197
```json
197198
// Maps to StringAttribute
198199
{
@@ -217,7 +218,7 @@ In an OAS schema, the following keywords defining nullable multi-types are suppo
217218
}
218219
```
219220

220-
#### OAS `anyOf` and `oneOf` keywords
221+
#### Examples with `anyOf` and `oneOf`
221222
```json
222223
// Maps to SingleNestedAttribute
223224
{
@@ -249,3 +250,57 @@ In an OAS schema, the following keywords defining nullable multi-types are suppo
249250
}
250251
}
251252
```
253+
254+
### String-able Multi-type support
255+
256+
If a multi-type is detected where one of the types is a `string` and the other type is a `primitive`, then the resulting attribute will be a `StringAttribute`.
257+
258+
Supported `primitive` types that can be represented as `string`:
259+
- `number`
260+
- `integer`
261+
- `boolean`
262+
263+
264+
#### Examples with `type` array, `oneOf`, and `anyOf`
265+
```json
266+
// Maps to StringAttribute
267+
{
268+
"stringable_number_example": {
269+
"description": "this is the description that's used!",
270+
"type": [
271+
"string",
272+
"number"
273+
]
274+
}
275+
}
276+
277+
// Maps to StringAttribute
278+
{
279+
"stringable_integer_example": {
280+
"description": "this is the description that's used!",
281+
"anyOf": [
282+
{
283+
"type": "integer"
284+
},
285+
{
286+
"type": "string"
287+
}
288+
]
289+
}
290+
}
291+
292+
// Maps to StringAttribute
293+
{
294+
"stringable_boolean_example": {
295+
"description": "this is the description that's used!",
296+
"oneOf": [
297+
{
298+
"type": "string"
299+
},
300+
{
301+
"type": "boolean"
302+
}
303+
]
304+
}
305+
}
306+
```

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ testdata:
3636
--output ./internal/cmd/testdata/edgecase/provider_code_spec.json \
3737
./internal/cmd/testdata/edgecase/openapi_spec.yml
3838

39+
go run ./cmd/tfplugingen-openapi generate \
40+
--config ./internal/cmd/testdata/kubernetes/generator_config.yml \
41+
--output ./internal/cmd/testdata/kubernetes/provider_code_spec.json \
42+
./internal/cmd/testdata/kubernetes/openapi_spec.json
43+
3944
.PHONY: lint fmt test
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Ref: https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/deployment_v1
2+
provider:
3+
name: kubernetes
4+
5+
resources:
6+
deployment_v1:
7+
create:
8+
path: /apis/apps/v1/namespaces/{namespace}/deployments
9+
method: POST
10+
read:
11+
path: /apis/apps/v1/namespaces/{namespace}/deployments/{name}
12+
method: GET
13+
update:
14+
path: /apis/apps/v1/namespaces/{namespace}/deployments/{name}
15+
method: PUT
16+
delete:
17+
path: /apis/apps/v1/namespaces/{namespace}/deployments/{name}
18+
method: DELETE

0 commit comments

Comments
 (0)