Skip to content

Commit

Permalink
Merge pull request #299 from Jefftree/def-optimize
Browse files Browse the repository at this point in the history
Optimize GetOpenAPIDefinitions to avoid generating the map for all definitions on every BuildOpenAPISpec invocation
  • Loading branch information
k8s-ci-robot authored Mar 28, 2022
2 parents 3a31a64 + ba6f675 commit 3ee0da9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
14 changes: 9 additions & 5 deletions pkg/builder3/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (

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

builderutil "k8s.io/kube-openapi/pkg/builder3/util"
"k8s.io/kube-openapi/pkg/common"
"k8s.io/kube-openapi/pkg/common/restfuladapter"
"k8s.io/kube-openapi/pkg/spec3"
builderutil "k8s.io/kube-openapi/pkg/builder3/util"
"k8s.io/kube-openapi/pkg/util"
"k8s.io/kube-openapi/pkg/validation/spec"
)
Expand Down Expand Up @@ -226,10 +226,14 @@ func newOpenAPI(config *common.Config) openAPI {
}
}

o.definitions = o.config.GetDefinitions(func(name string) spec.Ref {
defName, _ := o.config.GetDefinitionName(name)
return spec.MustCreateRef("#/components/schemas/" + common.EscapeJsonPointer(defName))
})
if o.config.Definitions != nil {
o.definitions = o.config.Definitions
} else {
o.definitions = o.config.GetDefinitions(func(name string) spec.Ref {
defName, _ := o.config.GetDefinitionName(name)
return spec.MustCreateRef("#/components/schemas/" + common.EscapeJsonPointer(defName))
})
}

return o
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ type Config struct {
// or any of the models will result in spec generation failure.
GetDefinitions GetOpenAPIDefinitions

// Provides the definition for all models used by routes. One of GetDefinitions or Definitions must be defined to generate a spec.
// This takes precedent over the GetDefinitions function
Definitions map[string]OpenAPIDefinition

// GetOperationIDAndTags returns operation id and tags for a restful route. It is an optional function to customize operation IDs.
//
// Deprecated: GetOperationIDAndTagsFromRoute should be used instead. This cannot be specified if using the new Route
Expand Down Expand Up @@ -141,8 +145,13 @@ type OpenAPIV3Config struct {

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

// Provides the definition for all models used by routes. One of GetDefinitions or Definitions must be defined to generate a spec.
// This takes precedent over the GetDefinitions function
Definitions map[string]OpenAPIDefinition

// GetOperationIDAndTags returns operation id and tags for a restful route. It is an optional function to customize operation IDs.
//
// Deprecated: GetOperationIDAndTagsFromRoute should be used instead. This cannot be specified if using the new Route
Expand Down Expand Up @@ -176,12 +185,13 @@ func ConvertConfigToV3(config *Config) *OpenAPIV3Config {
GetOperationIDAndTags: config.GetOperationIDAndTags,
GetOperationIDAndTagsFromRoute: config.GetOperationIDAndTagsFromRoute,
GetDefinitionName: config.GetDefinitionName,
Definitions: config.Definitions,
SecuritySchemes: make(spec3.SecuritySchemes),
DefaultSecurity: config.DefaultSecurity,
DefaultResponse: openapiconv.ConvertResponse(config.DefaultResponse, []string{"application/json"}),

CommonResponses: make(map[int]*spec3.Response),
ResponseDefinitions: make(map[string]*spec3.Response),
CommonResponses: make(map[int]*spec3.Response),
ResponseDefinitions: make(map[string]*spec3.Response),
}

if config.SecurityDefinitions != nil {
Expand Down

0 comments on commit 3ee0da9

Please sign in to comment.