Skip to content

Commit

Permalink
fix array schema type
Browse files Browse the repository at this point in the history
  • Loading branch information
emil.gelman committed Oct 23, 2021
1 parent cb219e9 commit b3ea6c3
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 5 deletions.
10 changes: 10 additions & 0 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,12 @@ 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 {
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 @@ -73,6 +81,8 @@ func (m *Mapper) parseDefinition(rootName, name string, openapiSchema *spec.Sche
switch tType {
case "object":
newSchema.Type = schema.TypeMap
case array:
newSchema.Type = schema.TypeList
case "string":
newSchema.Type = schema.TypeString
}
Expand Down
73 changes: 73 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,75 @@ func TestMapper(t *testing.T) {
}
assert.Equal(t, expected, output)
}

func TestMapArrayDefinition(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 b3ea6c3

Please sign in to comment.