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 b3ea6c3 commit 2b32249
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
22 changes: 15 additions & 7 deletions pkg/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func (m *Mapper) parseDefinition(rootName, name string, openapiSchema *spec.Sche
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}
Expand All @@ -78,14 +83,17 @@ func (m *Mapper) parseDefinition(rootName, name string, openapiSchema *spec.Sche
}
newSchema := &schema.Schema{}
tType := openapiSchema.Type[0]
switch tType {
newSchema.Type = mapType(tType)
newSchema.Description = openapiSchema.Description
tfSchema[name] = newSchema
}

func mapType(t string) schema.ValueType {
switch t {
case "object":
newSchema.Type = schema.TypeMap
return schema.TypeMap
case array:
newSchema.Type = schema.TypeList
case "string":
newSchema.Type = schema.TypeString
return schema.TypeList
}
newSchema.Description = openapiSchema.Description
tfSchema[name] = newSchema
return schema.TypeString
}
44 changes: 43 additions & 1 deletion pkg/mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,49 @@ func TestMapper(t *testing.T) {
assert.Equal(t, expected, output)
}

func TestMapArrayDefinition(t *testing.T) {
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)}
Expand Down

0 comments on commit 2b32249

Please sign in to comment.