diff --git a/pkg/builder3/openapi.go b/pkg/builder3/openapi.go index 8871c37ce..abe5a331e 100644 --- a/pkg/builder3/openapi.go +++ b/pkg/builder3/openapi.go @@ -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" ) @@ -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 } diff --git a/pkg/common/common.go b/pkg/common/common.go index d7aae2927..c84ca7031 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -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 @@ -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 @@ -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 {