@@ -36,7 +36,7 @@ const (
3636)
3737
3838type openAPI struct {
39- config * common.Config
39+ config * common.OpenAPIV3Config
4040 spec * spec3.OpenAPI
4141 definitions map [string ]common.OpenAPIDefinition
4242}
@@ -83,6 +83,15 @@ func (o *openAPI) buildOperations(route common.Route, inPathCommonParamsMap map[
8383 },
8484 },
8585 }
86+ for k , v := range route .Metadata () {
87+ if strings .HasPrefix (k , common .ExtensionPrefix ) {
88+ if ret .Extensions == nil {
89+ ret .Extensions = spec.Extensions {}
90+ }
91+ ret .Extensions .Add (k , v )
92+ }
93+ }
94+
8695 var err error
8796 if ret .OperationId , ret .Tags , err = o .config .GetOperationIDAndTagsFromRoute (route ); err != nil {
8897 return ret , err
@@ -104,9 +113,16 @@ func (o *openAPI) buildOperations(route common.Route, inPathCommonParamsMap map[
104113 }
105114 }
106115
107- // TODO: Default response if needed. Common Response config
116+ for code , resp := range o .config .CommonResponses {
117+ if _ , exists := ret .Responses .StatusCodeResponses [code ]; ! exists {
118+ ret .Responses .StatusCodeResponses [code ] = resp
119+ }
120+ }
121+
122+ if o .config .DefaultResponse != nil {
123+ ret .Responses .Default = o .config .DefaultResponse
124+ }
108125
109- ret .Parameters = make ([]* spec3.Parameter , 0 )
110126 params := route .Parameters ()
111127 for _ , param := range params {
112128 _ , isCommon := inPathCommonParamsMap [mapKeyFromParam (param )]
@@ -119,7 +135,7 @@ func (o *openAPI) buildOperations(route common.Route, inPathCommonParamsMap map[
119135 }
120136 }
121137
122- body , err := o .buildRequestBody (params , route .RequestPayloadSample ())
138+ body , err := o .buildRequestBody (params , route .Consumes (), route . RequestPayloadSample ())
123139 if err != nil {
124140 return nil , err
125141 }
@@ -130,7 +146,7 @@ func (o *openAPI) buildOperations(route common.Route, inPathCommonParamsMap map[
130146 return ret , nil
131147}
132148
133- func (o * openAPI ) buildRequestBody (parameters []common.Parameter , bodySample interface {}) (* spec3.RequestBody , error ) {
149+ func (o * openAPI ) buildRequestBody (parameters []common.Parameter , consumes [] string , bodySample interface {}) (* spec3.RequestBody , error ) {
134150 for _ , param := range parameters {
135151 if param .Kind () == common .BodyParameterKind && bodySample != nil {
136152 schema , err := o .toSchema (util .GetCanonicalTypeName (bodySample ))
@@ -139,15 +155,16 @@ func (o *openAPI) buildRequestBody(parameters []common.Parameter, bodySample int
139155 }
140156 r := & spec3.RequestBody {
141157 RequestBodyProps : spec3.RequestBodyProps {
142- Content : map [string ]* spec3.MediaType {
143- "application/json" : & spec3.MediaType {
144- MediaTypeProps : spec3.MediaTypeProps {
145- Schema : schema ,
146- },
147- },
148- },
158+ Content : map [string ]* spec3.MediaType {},
149159 },
150160 }
161+ for _ , consume := range consumes {
162+ r .Content [consume ] = & spec3.MediaType {
163+ MediaTypeProps : spec3.MediaTypeProps {
164+ Schema : schema ,
165+ },
166+ }
167+ }
151168 return r , nil
152169 }
153170 }
@@ -156,7 +173,7 @@ func (o *openAPI) buildRequestBody(parameters []common.Parameter, bodySample int
156173
157174func newOpenAPI (config * common.Config ) openAPI {
158175 o := openAPI {
159- config : config ,
176+ config : common . ConvertConfigToV3 ( config ) ,
160177 spec : & spec3.OpenAPI {
161178 Version : "3.0.0" ,
162179 Info : config .Info ,
@@ -168,6 +185,21 @@ func newOpenAPI(config *common.Config) openAPI {
168185 },
169186 },
170187 }
188+ if len (o .config .ResponseDefinitions ) > 0 {
189+ o .spec .Components .Responses = make (map [string ]* spec3.Response )
190+
191+ }
192+ for k , response := range o .config .ResponseDefinitions {
193+ o .spec .Components .Responses [k ] = response
194+ }
195+
196+ if len (o .config .SecuritySchemes ) > 0 {
197+ o .spec .Components .SecuritySchemes = make (spec3.SecuritySchemes )
198+
199+ }
200+ for k , securityScheme := range o .config .SecuritySchemes {
201+ o .spec .Components .SecuritySchemes [k ] = securityScheme
202+ }
171203
172204 if o .config .GetOperationIDAndTagsFromRoute == nil {
173205 // Map the deprecated handler to the common interface, if provided.
@@ -235,9 +267,7 @@ func (o *openAPI) buildOpenAPISpec(webServices []common.RouteContainer) error {
235267 }
236268
237269 pathItem = & spec3.Path {
238- PathProps : spec3.PathProps {
239- Parameters : make ([]* spec3.Parameter , 0 ),
240- },
270+ PathProps : spec3.PathProps {},
241271 }
242272
243273 // add web services's parameters as well as any parameters appears in all ops, as common parameters
@@ -249,6 +279,7 @@ func (o *openAPI) buildOpenAPISpec(webServices []common.RouteContainer) error {
249279
250280 for _ , route := range routes {
251281 op , _ := o .buildOperations (route , inPathCommonParamsMap )
282+ sortParameters (op .Parameters )
252283
253284 switch strings .ToUpper (route .Method ()) {
254285 case "GET" :
0 commit comments