@@ -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+
120205type typeInfo struct {
121206 name string
122207 format string
0 commit comments