Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize CRD building by reusing Definitions #436

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions pkg/builder/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,24 @@ const (
)

type openAPI struct {
config *common.Config
swagger *spec.Swagger
protocolList []string
definitions map[string]common.OpenAPIDefinition
config *common.Config
swagger *spec.Swagger
protocolList []string
definitions map[string]common.OpenAPIDefinition
additionalDefinitions map[string]common.OpenAPIDefinition
}

// getDefinition is a getter that checks two maps for the existence of a key
func (o *openAPI) getDefinition(key string) (common.OpenAPIDefinition, bool) {
val, ok := o.definitions[key]
if ok {
return val, ok
}
if o.additionalDefinitions != nil {
val, ok = o.additionalDefinitions[key]
return val, ok
}
return common.OpenAPIDefinition{}, false
}

// BuildOpenAPISpec builds OpenAPI spec given a list of route containers and common.Config to customize it.
Expand Down Expand Up @@ -127,10 +141,19 @@ func newOpenAPI(config *common.Config) openAPI {
return name[strings.LastIndex(name, "/")+1:], nil
}
}
o.definitions = o.config.GetDefinitions(func(name string) spec.Ref {
defName, _ := o.config.GetDefinitionName(name)
return spec.MustCreateRef("#/definitions/" + 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("#/definitions/" + common.EscapeJsonPointer(defName))
})
}

if o.config.AdditionalDefinitions != nil {
o.additionalDefinitions = o.config.AdditionalDefinitions
}

if o.config.CommonResponses == nil {
o.config.CommonResponses = map[int]spec.Response{}
}
Expand Down Expand Up @@ -160,7 +183,7 @@ func (o *openAPI) buildDefinitionRecursively(name string) error {
if _, ok := o.swagger.Definitions[uniqueName]; ok {
return nil
}
if item, ok := o.definitions[name]; ok {
if item, ok := o.getDefinition(name); ok {
schema := spec.Schema{
VendorExtensible: item.Schema.VendorExtensible,
SchemaProps: item.Schema.SchemaProps,
Expand Down
26 changes: 22 additions & 4 deletions pkg/builder3/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,23 @@ const (
)

type openAPI struct {
config *common.OpenAPIV3Config
spec *spec3.OpenAPI
definitions map[string]common.OpenAPIDefinition
config *common.OpenAPIV3Config
spec *spec3.OpenAPI
definitions map[string]common.OpenAPIDefinition
additionalDefinitions map[string]common.OpenAPIDefinition
}

// getDefinition is a getter that checks two maps for the existence of a key
func (o *openAPI) getDefinition(key string) (common.OpenAPIDefinition, bool) {
val, ok := o.definitions[key]
if ok {
return val, ok
}
if o.additionalDefinitions != nil {
val, ok = o.additionalDefinitions[key]
return val, ok
}
return common.OpenAPIDefinition{}, false
}

func groupRoutesByPath(routes []common.Route) map[string][]common.Route {
Expand Down Expand Up @@ -237,6 +251,10 @@ func newOpenAPI(config *common.OpenAPIV3Config) openAPI {
})
}

if o.config.AdditionalDefinitions != nil {
o.additionalDefinitions = o.config.AdditionalDefinitions
}

return o
}

Expand Down Expand Up @@ -433,7 +451,7 @@ func (o *openAPI) buildDefinitionRecursively(name string) error {
if _, ok := o.spec.Components.Schemas[uniqueName]; ok {
return nil
}
if item, ok := o.definitions[name]; ok {
if item, ok := o.getDefinition(name); ok {
schema := &spec.Schema{
VendorExtensible: item.Schema.VendorExtensible,
SchemaProps: item.Schema.SchemaProps,
Expand Down
10 changes: 10 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ type Config struct {
// This takes precedent over the GetDefinitions function
Definitions map[string]OpenAPIDefinition

// Provides an additional set of definitions for models used by routes.
// This is unioned with Definitions when building the spec, and allows
// the Definitions map to be reused across different builders.
AdditionalDefinitions 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 @@ -151,6 +156,11 @@ type OpenAPIV3Config struct {
// This takes precedent over the GetDefinitions function
Definitions map[string]OpenAPIDefinition

// Provides an additional set of definitions for models used by routes.
// This is unioned with Definitions when building the spec, and allows
// the Definitions map to be reused across different builders.
AdditionalDefinitions 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