@@ -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,17 @@ 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 there is still no response, use default response provided.
123+ if len (ret .Responses .StatusCodeResponses ) == 0 {
124+ ret .Responses .Default = o .config .DefaultResponse
125+ }
108126
109- ret .Parameters = make ([]* spec3.Parameter , 0 )
110127 params := route .Parameters ()
111128 for _ , param := range params {
112129 _ , isCommon := inPathCommonParamsMap [mapKeyFromParam (param )]
@@ -119,7 +136,7 @@ func (o *openAPI) buildOperations(route common.Route, inPathCommonParamsMap map[
119136 }
120137 }
121138
122- body , err := o .buildRequestBody (params , route .RequestPayloadSample ())
139+ body , err := o .buildRequestBody (params , route .Consumes (), route . RequestPayloadSample ())
123140 if err != nil {
124141 return nil , err
125142 }
@@ -130,7 +147,7 @@ func (o *openAPI) buildOperations(route common.Route, inPathCommonParamsMap map[
130147 return ret , nil
131148}
132149
133- func (o * openAPI ) buildRequestBody (parameters []common.Parameter , bodySample interface {}) (* spec3.RequestBody , error ) {
150+ func (o * openAPI ) buildRequestBody (parameters []common.Parameter , consumes [] string , bodySample interface {}) (* spec3.RequestBody , error ) {
134151 for _ , param := range parameters {
135152 if param .Kind () == common .BodyParameterKind && bodySample != nil {
136153 schema , err := o .toSchema (util .GetCanonicalTypeName (bodySample ))
@@ -139,15 +156,16 @@ func (o *openAPI) buildRequestBody(parameters []common.Parameter, bodySample int
139156 }
140157 r := & spec3.RequestBody {
141158 RequestBodyProps : spec3.RequestBodyProps {
142- Content : map [string ]* spec3.MediaType {
143- "application/json" : & spec3.MediaType {
144- MediaTypeProps : spec3.MediaTypeProps {
145- Schema : schema ,
146- },
147- },
148- },
159+ Content : map [string ]* spec3.MediaType {},
149160 },
150161 }
162+ for _ , consume := range consumes {
163+ r .Content [consume ] = & spec3.MediaType {
164+ MediaTypeProps : spec3.MediaTypeProps {
165+ Schema : schema ,
166+ },
167+ }
168+ }
151169 return r , nil
152170 }
153171 }
@@ -156,7 +174,7 @@ func (o *openAPI) buildRequestBody(parameters []common.Parameter, bodySample int
156174
157175func newOpenAPI (config * common.Config ) openAPI {
158176 o := openAPI {
159- config : config ,
177+ config : common . ConvertConfigToV3 ( config ) ,
160178 spec : & spec3.OpenAPI {
161179 Version : "3.0.0" ,
162180 Info : config .Info ,
@@ -168,6 +186,21 @@ func newOpenAPI(config *common.Config) openAPI {
168186 },
169187 },
170188 }
189+ if len (o .config .ResponseDefinitions ) > 0 {
190+ o .spec .Components .Responses = make (map [string ]* spec3.Response )
191+
192+ }
193+ for k , response := range o .config .ResponseDefinitions {
194+ o .spec .Components .Responses [k ] = response
195+ }
196+
197+ if len (o .config .SecuritySchemes ) > 0 {
198+ o .spec .Components .SecuritySchemes = make (spec3.SecuritySchemes )
199+
200+ }
201+ for k , securityScheme := range o .config .SecuritySchemes {
202+ o .spec .Components .SecuritySchemes [k ] = securityScheme
203+ }
171204
172205 if o .config .GetOperationIDAndTagsFromRoute == nil {
173206 // Map the deprecated handler to the common interface, if provided.
@@ -235,9 +268,7 @@ func (o *openAPI) buildOpenAPISpec(webServices []common.RouteContainer) error {
235268 }
236269
237270 pathItem = & spec3.Path {
238- PathProps : spec3.PathProps {
239- Parameters : make ([]* spec3.Parameter , 0 ),
240- },
271+ PathProps : spec3.PathProps {},
241272 }
242273
243274 // add web services's parameters as well as any parameters appears in all ops, as common parameters
@@ -249,6 +280,7 @@ func (o *openAPI) buildOpenAPISpec(webServices []common.RouteContainer) error {
249280
250281 for _ , route := range routes {
251282 op , _ := o .buildOperations (route , inPathCommonParamsMap )
283+ sortParameters (op .Parameters )
252284
253285 switch strings .ToUpper (route .Method ()) {
254286 case "GET" :
0 commit comments