Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefftree committed Oct 25, 2023
1 parent 2dd684a commit 71f1799
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
40 changes: 30 additions & 10 deletions pkg/builder/openapi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*d
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -35,10 +35,21 @@ 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
}
val, ok = o.additionalDefinitions[key]
return val, ok
}

// BuildOpenAPISpec builds OpenAPI spec given a list of route containers and common.Config to customize it.
Expand Down Expand Up @@ -127,10 +138,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 +180,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
23 changes: 19 additions & 4 deletions pkg/builder3/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,20 @@ 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
}
val, ok = o.additionalDefinitions[key]
return val, ok
}

func groupRoutesByPath(routes []common.Route) map[string][]common.Route {
Expand Down Expand Up @@ -237,6 +248,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 +448,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
4 changes: 4 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ type Config struct {
// This takes precedent over the GetDefinitions function
Definitions map[string]OpenAPIDefinition

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 +153,8 @@ type OpenAPIV3Config struct {
// This takes precedent over the GetDefinitions function
Definitions map[string]OpenAPIDefinition

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

0 comments on commit 71f1799

Please sign in to comment.