Skip to content

Commit 6ebf5ca

Browse files
committed
Fixes for CI
1 parent dcd6870 commit 6ebf5ca

File tree

4 files changed

+257
-73
lines changed

4 files changed

+257
-73
lines changed

pkg/builder3/openapi.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"k8s.io/kube-openapi/pkg/spec3"
2626
"k8s.io/kube-openapi/pkg/util"
2727
"k8s.io/kube-openapi/pkg/validation/spec"
28-
"k8s.io/kube-openapi/pkg/openapiconv"
2928
"net/http"
3029
"strings"
3130
)
@@ -35,11 +34,12 @@ const (
3534
)
3635

3736
type openAPI struct {
38-
config *common.Config
37+
config *common.OpenAPIV3Config
3938
spec *spec3.OpenAPI
4039
definitions map[string]common.OpenAPIDefinition
4140
}
4241

42+
4343
func groupRoutesByPath(routes []common.Route) map[string][]common.Route {
4444
pathToRoutes := make(map[string][]common.Route)
4545
for _, r := range routes {
@@ -106,16 +106,15 @@ func (o *openAPI) buildOperations(route common.Route, inPathCommonParamsMap map[
106106

107107
for code, resp := range o.config.CommonResponses {
108108
if _, exists := ret.Responses.StatusCodeResponses[code]; !exists {
109-
ret.Responses.StatusCodeResponses[code] = openapiconv.ConvertResponse(&resp, route.Produces())
109+
ret.Responses.StatusCodeResponses[code] = resp
110110
}
111111
}
112112

113113
// If there is still no response, use default response provided.
114114
if len(ret.Responses.StatusCodeResponses) == 0 {
115-
ret.Responses.Default = openapiconv.ConvertResponse(o.config.DefaultResponse, route.Produces())
115+
ret.Responses.Default = o.config.DefaultResponse
116116
}
117117

118-
119118
ret.Parameters = make([]*spec3.Parameter, 0)
120119
params := route.Parameters()
121120
for _, param := range params {
@@ -166,19 +165,23 @@ func (o *openAPI) buildRequestBody(parameters []common.Parameter, bodySample int
166165

167166
func newOpenAPI(config *common.Config) openAPI {
168167
o := openAPI{
169-
config: config,
168+
config: common.ConvertConfigToV3(config),
170169
spec: &spec3.OpenAPI{
171170
Version: "3.0.0",
172171
Info: config.Info,
173172
Paths: &spec3.Paths{
174173
Paths: map[string]*spec3.Path{},
175174
},
176175
Components: &spec3.Components{
177-
Schemas: map[string]*spec.Schema{},
176+
Schemas: map[string]*spec.Schema{},
178177
SecuritySchemes: make(spec3.SecuritySchemes),
178+
Responses: make(map[string]*spec3.Response),
179179
},
180180
},
181181
}
182+
for k, response := range o.config.ResponseDefinitions {
183+
o.spec.Components.Responses[k] = response
184+
}
182185

183186
if o.config.GetOperationIDAndTagsFromRoute == nil {
184187
// Map the deprecated handler to the common interface, if provided.

pkg/common/common.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222

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

25+
"k8s.io/kube-openapi/pkg/openapiconv"
26+
"k8s.io/kube-openapi/pkg/spec3"
2527
"k8s.io/kube-openapi/pkg/validation/spec"
2628
)
2729

@@ -117,6 +119,89 @@ type Config struct {
117119
DefaultSecurity []map[string][]string
118120
}
119121

122+
type OpenAPIV3Config struct {
123+
// Info is general information about the API.
124+
Info *spec.Info
125+
126+
// DefaultResponse will be used if an operation does not have any responses listed. It
127+
// will show up as ... "responses" : {"default" : $DefaultResponse} in the spec.
128+
DefaultResponse *spec3.Response
129+
130+
// ResponseDefinitions will be added to "responses" under the top-level swagger object. This is an object
131+
// that holds responses definitions that can be used across operations. This property does not define
132+
// global responses for all operations. For more info please refer:
133+
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#fixed-fields
134+
ResponseDefinitions map[string]*spec3.Response
135+
136+
// CommonResponses will be added as a response to all operation specs. This is a good place to add common
137+
// responses such as authorization failed.
138+
CommonResponses map[int]*spec3.Response
139+
140+
// List of webservice's path prefixes to ignore
141+
IgnorePrefixes []string
142+
143+
// OpenAPIDefinitions should provide definition for all models used by routes. Failure to provide this map
144+
// or any of the models will result in spec generation failure.
145+
GetDefinitions GetOpenAPIDefinitions
146+
147+
// GetOperationIDAndTags returns operation id and tags for a restful route. It is an optional function to customize operation IDs.
148+
//
149+
// Deprecated: GetOperationIDAndTagsFromRoute should be used instead. This cannot be specified if using the new Route
150+
// interface set of funcs.
151+
GetOperationIDAndTags func(r *restful.Route) (string, []string, error)
152+
153+
// GetOperationIDAndTagsFromRoute returns operation id and tags for a Route. It is an optional function to customize operation IDs.
154+
GetOperationIDAndTagsFromRoute func(r Route) (string, []string, error)
155+
156+
// GetDefinitionName returns a friendly name for a definition base on the serving path. parameter `name` is the full name of the definition.
157+
// It is an optional function to customize model names.
158+
GetDefinitionName func(name string) (string, spec.Extensions)
159+
160+
// SecurityDefinitions is list of all security definitions for OpenAPI service. If this is not nil, the user of config
161+
// is responsible to provide DefaultSecurity and (maybe) add unauthorized response to CommonResponses.
162+
SecuritySchemes spec3.SecuritySchemes
163+
164+
// DefaultSecurity for all operations. This will pass as spec.SwaggerProps.Security to OpenAPI.
165+
// For most cases, this will be list of acceptable definitions in SecurityDefinitions.
166+
DefaultSecurity []map[string][]string
167+
}
168+
169+
// ConvertConfigToV3 converts a Config object to an OpenAPIV3Config object
170+
func ConvertConfigToV3(config *Config) *OpenAPIV3Config {
171+
if config == nil {
172+
return nil
173+
}
174+
175+
v3Config := &OpenAPIV3Config{
176+
Info: config.Info,
177+
IgnorePrefixes: config.IgnorePrefixes,
178+
GetDefinitions: config.GetDefinitions,
179+
GetOperationIDAndTags: config.GetOperationIDAndTags,
180+
GetOperationIDAndTagsFromRoute: config.GetOperationIDAndTagsFromRoute,
181+
GetDefinitionName: config.GetDefinitionName,
182+
SecuritySchemes: make(spec3.SecuritySchemes),
183+
DefaultSecurity: config.DefaultSecurity,
184+
DefaultResponse: openapiconv.ConvertResponse(config.DefaultResponse, []string{"application/json"}),
185+
186+
CommonResponses: make(map[int]*spec3.Response),
187+
ResponseDefinitions: make(map[string]*spec3.Response),
188+
}
189+
190+
if config.SecurityDefinitions != nil {
191+
for s, securityScheme := range *config.SecurityDefinitions {
192+
v3Config.SecuritySchemes[s] = openapiconv.ConvertSecurityScheme(securityScheme)
193+
}
194+
}
195+
for k, commonResponse := range config.CommonResponses {
196+
v3Config.CommonResponses[k] = openapiconv.ConvertResponse(&commonResponse, []string{"application/json"})
197+
}
198+
199+
for k, responseDefinition := range config.ResponseDefinitions {
200+
v3Config.ResponseDefinitions[k] = openapiconv.ConvertResponse(&responseDefinition, []string{"application/json"})
201+
}
202+
return v3Config
203+
}
204+
120205
type typeInfo struct {
121206
name string
122207
format string

0 commit comments

Comments
 (0)