Skip to content

Commit 3ee0da9

Browse files
authored
Merge pull request #299 from Jefftree/def-optimize
Optimize GetOpenAPIDefinitions to avoid generating the map for all definitions on every BuildOpenAPISpec invocation
2 parents 3a31a64 + ba6f675 commit 3ee0da9

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

pkg/builder3/openapi.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424

2525
restful "github.com/emicklei/go-restful"
2626

27+
builderutil "k8s.io/kube-openapi/pkg/builder3/util"
2728
"k8s.io/kube-openapi/pkg/common"
2829
"k8s.io/kube-openapi/pkg/common/restfuladapter"
2930
"k8s.io/kube-openapi/pkg/spec3"
30-
builderutil "k8s.io/kube-openapi/pkg/builder3/util"
3131
"k8s.io/kube-openapi/pkg/util"
3232
"k8s.io/kube-openapi/pkg/validation/spec"
3333
)
@@ -226,10 +226,14 @@ func newOpenAPI(config *common.Config) openAPI {
226226
}
227227
}
228228

229-
o.definitions = o.config.GetDefinitions(func(name string) spec.Ref {
230-
defName, _ := o.config.GetDefinitionName(name)
231-
return spec.MustCreateRef("#/components/schemas/" + common.EscapeJsonPointer(defName))
232-
})
229+
if o.config.Definitions != nil {
230+
o.definitions = o.config.Definitions
231+
} else {
232+
o.definitions = o.config.GetDefinitions(func(name string) spec.Ref {
233+
defName, _ := o.config.GetDefinitionName(name)
234+
return spec.MustCreateRef("#/components/schemas/" + common.EscapeJsonPointer(defName))
235+
})
236+
}
233237

234238
return o
235239
}

pkg/common/common.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ type Config struct {
9494
// or any of the models will result in spec generation failure.
9595
GetDefinitions GetOpenAPIDefinitions
9696

97+
// Provides the definition for all models used by routes. One of GetDefinitions or Definitions must be defined to generate a spec.
98+
// This takes precedent over the GetDefinitions function
99+
Definitions map[string]OpenAPIDefinition
100+
97101
// GetOperationIDAndTags returns operation id and tags for a restful route. It is an optional function to customize operation IDs.
98102
//
99103
// Deprecated: GetOperationIDAndTagsFromRoute should be used instead. This cannot be specified if using the new Route
@@ -141,8 +145,13 @@ type OpenAPIV3Config struct {
141145

142146
// OpenAPIDefinitions should provide definition for all models used by routes. Failure to provide this map
143147
// or any of the models will result in spec generation failure.
148+
// One of GetDefinitions or Definitions must be defined to generate a spec.
144149
GetDefinitions GetOpenAPIDefinitions
145150

151+
// Provides the definition for all models used by routes. One of GetDefinitions or Definitions must be defined to generate a spec.
152+
// This takes precedent over the GetDefinitions function
153+
Definitions map[string]OpenAPIDefinition
154+
146155
// GetOperationIDAndTags returns operation id and tags for a restful route. It is an optional function to customize operation IDs.
147156
//
148157
// Deprecated: GetOperationIDAndTagsFromRoute should be used instead. This cannot be specified if using the new Route
@@ -176,12 +185,13 @@ func ConvertConfigToV3(config *Config) *OpenAPIV3Config {
176185
GetOperationIDAndTags: config.GetOperationIDAndTags,
177186
GetOperationIDAndTagsFromRoute: config.GetOperationIDAndTagsFromRoute,
178187
GetDefinitionName: config.GetDefinitionName,
188+
Definitions: config.Definitions,
179189
SecuritySchemes: make(spec3.SecuritySchemes),
180190
DefaultSecurity: config.DefaultSecurity,
181191
DefaultResponse: openapiconv.ConvertResponse(config.DefaultResponse, []string{"application/json"}),
182192

183-
CommonResponses: make(map[int]*spec3.Response),
184-
ResponseDefinitions: make(map[string]*spec3.Response),
193+
CommonResponses: make(map[int]*spec3.Response),
194+
ResponseDefinitions: make(map[string]*spec3.Response),
185195
}
186196

187197
if config.SecurityDefinitions != nil {

0 commit comments

Comments
 (0)