Skip to content

Commit

Permalink
Merge pull request #2 from emilgelman/fix-array-types
Browse files Browse the repository at this point in the history
Fix array types
  • Loading branch information
emilgelman authored Oct 23, 2021
2 parents cb219e9 + 2b32249 commit fa47f4b
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 11 deletions.
30 changes: 24 additions & 6 deletions pkg/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"k8s.io/kube-openapi/pkg/validation/spec"
)

const array = "array"

type Mapper struct {
}

Expand Down Expand Up @@ -57,6 +59,17 @@ func (m *Mapper) parseDefinition(rootName, name string, openapiSchema *spec.Sche
tfSchema[i] = &schema.Schema{Type: schema.TypeList, Elem: &schema.Resource{Schema: ss}}
continue
}
if prop.SchemaProps.Type[0] == array {
if len(prop.SchemaProps.Items.Schema.Type) > 0 {
t := prop.SchemaProps.Items.Schema.Type[0]
tfSchema[i] = &schema.Schema{Type: schema.TypeList, Elem: &schema.Schema{Type: mapType(t)}}
continue
}
path := prop.SchemaProps.Items.Schema.Ref.Ref.GetURL().Path
ss := schemas[path]
tfSchema[i] = &schema.Schema{Type: schema.TypeList, Elem: &ss}
continue
}
m.parseDefinition(rootName, i, &prop, tfSchema, schemas)
}
if name == rootName {
Expand All @@ -70,12 +83,17 @@ func (m *Mapper) parseDefinition(rootName, name string, openapiSchema *spec.Sche
}
newSchema := &schema.Schema{}
tType := openapiSchema.Type[0]
switch tType {
case "object":
newSchema.Type = schema.TypeMap
case "string":
newSchema.Type = schema.TypeString
}
newSchema.Type = mapType(tType)
newSchema.Description = openapiSchema.Description
tfSchema[name] = newSchema
}

func mapType(t string) schema.ValueType {
switch t {
case "object":
return schema.TypeMap
case array:
return schema.TypeList
}
return schema.TypeString
}
115 changes: 115 additions & 0 deletions pkg/mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mapper
import (
"testing"

"github.com/go-openapi/jsonreference"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/assert"
"k8s.io/kube-openapi/pkg/common"
Expand Down Expand Up @@ -41,3 +42,117 @@ func TestMapper(t *testing.T) {
}
assert.Equal(t, expected, output)
}

func TestMapArrayOfPrimitive(t *testing.T) {
m := New()
definitions := map[string]common.OpenAPIDefinition{
"car": {
Schema: spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: map[string]spec.Schema{
"colors": {
SchemaProps: spec.SchemaProps{
Type: []string{"array"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
},
},
},
},
},
Required: []string{"colors"},
},
},
},
}
output := m.Map(definitions)
expected := map[string]map[string]*schema.Schema{
"car": {
"colors": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
assert.Equal(t, expected, output)

}

func TestMapArrayOfObject(t *testing.T) {
m := New()
ref := func(path string) spec.Ref {
return spec.Ref{Ref: jsonreference.MustCreateRef(path)}
}
definitions := map[string]common.OpenAPIDefinition{
"engineSpec": {
Schema: spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: map[string]spec.Schema{
"cylinders": {
SchemaProps: spec.SchemaProps{
Type: []string{"array"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: map[string]interface{}{},
Ref: ref("cylinder"),
},
},
},
},
},
},
Required: []string{"cylinders"},
},
},
Dependencies: []string{
"cylinder"},
},
"cylinder": {
Schema: spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: map[string]spec.Schema{
"Number": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
},
},
Required: []string{"Number"},
},
},
},
}
output := m.Map(definitions)
expected := map[string]map[string]*schema.Schema{
"engineSpec": {
"cylinders": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &map[string]*schema.Schema{
"Number": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"cylinder": {
"Number": &schema.Schema{
Type: schema.TypeString,
Required: true,
}},
}
assert.Equal(t, expected, output)
}
8 changes: 7 additions & 1 deletion test/input/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ type Car struct {

// +k8s:openapi-gen=true
type EngineSpec struct {
BHP string `json:"bhp"`
BHP string `json:"bhp"`
Cylinders []Cylinder `json:"cylinders"`
}

// +k8s:openapi-gen=true
type Cylinder struct {
Number string
}
32 changes: 28 additions & 4 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,45 @@ import (
)

var expectedSchemas = map[string]map[string]*schema.Schema{
"GetEngineSpecSchema": {"bhp": &schema.Schema{
Type: schema.TypeString,
Required: true,
}},
"GetEngineSpecSchema": {
"bhp": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"cylinders": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &map[string]*schema.Schema{"Number": {
Type: schema.TypeString,
Required: true,
}},
},
},
"GetCarSchema": {
"engineSpec": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{Schema: map[string]*schema.Schema{
"bhp": {Type: schema.TypeString, Required: true},
"cylinders": {
Type: schema.TypeList,
Required: true,
Elem: &map[string]*schema.Schema{"Number": {
Type: schema.TypeString,
Required: true,
}},
},
}},
},
"make": &schema.Schema{Type: schema.TypeString, Required: true},
"model": &schema.Schema{Type: schema.TypeString},
},
"GetCylinderSchema": {
"Number": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}

func TestGenerator(t *testing.T) {
Expand Down

0 comments on commit fa47f4b

Please sign in to comment.