Skip to content

Commit 232f4c0

Browse files
committed
Address comments
1 parent 0c9bc77 commit 232f4c0

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

pkg/openapiconv/convert.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ import (
2020
"strings"
2121

2222
klog "k8s.io/klog/v2"
23+
builderutil "k8s.io/kube-openapi/pkg/builder3/util"
2324
"k8s.io/kube-openapi/pkg/spec3"
2425
"k8s.io/kube-openapi/pkg/validation/spec"
25-
builderutil "k8s.io/kube-openapi/pkg/builder3/util"
2626
)
2727

2828
var OpenAPIV2DefPrefix = "#/definitions/"
2929
var OpenAPIV3DefPrefix = "#/components/schemas/"
3030

3131
// ConvertV2ToV3 converts an OpenAPI V2 object into V3.
3232
// Certain references may be shared between the V2 and V3 objects in the conversion.
33-
func ConvertV2ToV3(v2Spec *spec.Swagger) (*spec3.OpenAPI) {
33+
func ConvertV2ToV3(v2Spec *spec.Swagger) *spec3.OpenAPI {
3434
v3Spec := &spec3.OpenAPI{
3535
Version: "3.0.0",
3636
Info: v2Spec.Info,
@@ -84,10 +84,10 @@ func ConvertSchema(v2Schema *spec.Schema) *spec.Schema {
8484
return nil
8585
}
8686
v3Schema := spec.Schema{
87-
VendorExtensible: v2Schema.VendorExtensible,
88-
SchemaProps: v2Schema.SchemaProps,
87+
VendorExtensible: v2Schema.VendorExtensible,
88+
SchemaProps: v2Schema.SchemaProps,
8989
SwaggerSchemaProps: v2Schema.SwaggerSchemaProps,
90-
ExtraProps: v2Schema.ExtraProps,
90+
ExtraProps: v2Schema.ExtraProps,
9191
}
9292

9393
if refString := v2Schema.Ref.String(); refString != "" {
@@ -105,30 +105,39 @@ func ConvertSchema(v2Schema *spec.Schema) *spec.Schema {
105105
}
106106
}
107107
if v2Schema.Items != nil {
108-
v3Schema.Items = new(spec.SchemaOrArray)
109-
v3Schema.Items.Schema = ConvertSchema(v2Schema.Items.Schema)
110-
if v2Schema.Items.Schemas != nil {
111-
v3Schema.Items.Schemas = []spec.Schema{}
112-
for _, s := range v2Schema.Items.Schemas {
113-
v3Schema.Items.Schemas = append(v3Schema.Items.Schemas, *ConvertSchema(&s))
114-
}
108+
v3Schema.Items = &spec.SchemaOrArray{
109+
Schema: ConvertSchema(v2Schema.Items.Schema),
110+
Schemas: ConvertSchemaList(v2Schema.Items.Schemas),
115111
}
116112
}
117113

118114
if v2Schema.AdditionalProperties != nil {
119-
v3Schema.AdditionalProperties = new(spec.SchemaOrBool)
120-
v3Schema.AdditionalProperties.Schema = ConvertSchema(v2Schema.AdditionalProperties.Schema)
121-
v3Schema.AdditionalProperties.Allows = v2Schema.AdditionalProperties.Allows
115+
v3Schema.AdditionalProperties = &spec.SchemaOrBool{
116+
Schema: ConvertSchema(v2Schema.AdditionalProperties.Schema),
117+
Allows: v2Schema.AdditionalProperties.Allows,
118+
}
122119
}
123120
if v2Schema.AdditionalItems != nil {
124-
v3Schema.AdditionalItems = new(spec.SchemaOrBool)
125-
v3Schema.AdditionalItems.Schema = ConvertSchema(v2Schema.AdditionalItems.Schema)
126-
v3Schema.AdditionalItems.Allows = v2Schema.AdditionalItems.Allows
121+
v3Schema.AdditionalItems = &spec.SchemaOrBool{
122+
Schema: ConvertSchema(v2Schema.AdditionalItems.Schema),
123+
Allows: v2Schema.AdditionalItems.Allows,
124+
}
127125
}
128126

129127
return builderutil.WrapRefs(&v3Schema)
130128
}
131129

130+
func ConvertSchemaList(v2SchemaList []spec.Schema) []spec.Schema {
131+
if v2SchemaList == nil {
132+
return nil
133+
}
134+
v3SchemaList := []spec.Schema{}
135+
for _, s := range v2SchemaList {
136+
v3SchemaList = append(v3SchemaList, *ConvertSchema(&s))
137+
}
138+
return v3SchemaList
139+
}
140+
132141
func ConvertSecurityScheme(v2securityScheme *spec.SecurityScheme) *spec3.SecurityScheme {
133142
if v2securityScheme == nil {
134143
return nil

0 commit comments

Comments
 (0)