@@ -54,18 +54,23 @@ func ConvertExternalDocumentation(v2ED *spec.ExternalDocumentation) *spec3.Exter
5454}
5555
5656func ConvertComponents (v2SecurityDefinitions spec.SecurityDefinitions , v2Definitions spec.Definitions , v2Responses map [string ]spec.Response , produces []string ) * spec3.Components {
57- components := & spec3.Components {
58- Schemas : make ( map [ string ] * spec. Schema ),
59- SecuritySchemes : make (spec3. SecuritySchemes ),
60- Responses : make (map [string ]* spec3. Response ),
57+ components := & spec3.Components {}
58+
59+ if len ( v2Definitions ) > 0 {
60+ components . Schemas = make (map [string ]* spec. Schema )
6161 }
6262 for s , schema := range v2Definitions {
6363 components .Schemas [s ] = ConvertSchema (& schema )
6464 }
65-
65+ if len (v2SecurityDefinitions ) > 0 {
66+ components .SecuritySchemes = make (spec3.SecuritySchemes )
67+ }
6668 for s , securityScheme := range v2SecurityDefinitions {
6769 components .SecuritySchemes [s ] = ConvertSecurityScheme (securityScheme )
6870 }
71+ if len (v2Responses ) > 0 {
72+ components .Responses = make (map [string ]* spec3.Response )
73+ }
6974 for r , response := range v2Responses {
7075 components .Responses [r ] = ConvertResponse (& response , produces )
7176 }
@@ -144,9 +149,11 @@ func ConvertPaths(v2Paths *spec.Paths) *spec3.Paths {
144149 }
145150 paths := & spec3.Paths {
146151 VendorExtensible : v2Paths .VendorExtensible ,
147- Paths : make (map [string ]* spec3.Path ),
148152 }
149153
154+ if len (v2Paths .Paths ) > 0 {
155+ paths .Paths = make (map [string ]* spec3.Path )
156+ }
150157 for k , v := range v2Paths .Paths {
151158 paths .Paths [k ] = ConvertPathItem (v )
152159 }
@@ -157,14 +164,13 @@ func ConvertPathItem(v2pathItem spec.PathItem) *spec3.Path {
157164 path := & spec3.Path {
158165 Refable : v2pathItem .Refable ,
159166 PathProps : spec3.PathProps {
160- Get : ConvertOperation (v2pathItem .Get ),
161- Put : ConvertOperation (v2pathItem .Put ),
162- Post : ConvertOperation (v2pathItem .Post ),
163- Delete : ConvertOperation (v2pathItem .Delete ),
164- Options : ConvertOperation (v2pathItem .Options ),
165- Head : ConvertOperation (v2pathItem .Head ),
166- Patch : ConvertOperation (v2pathItem .Patch ),
167- Parameters : []* spec3.Parameter {},
167+ Get : ConvertOperation (v2pathItem .Get ),
168+ Put : ConvertOperation (v2pathItem .Put ),
169+ Post : ConvertOperation (v2pathItem .Post ),
170+ Delete : ConvertOperation (v2pathItem .Delete ),
171+ Options : ConvertOperation (v2pathItem .Options ),
172+ Head : ConvertOperation (v2pathItem .Head ),
173+ Patch : ConvertOperation (v2pathItem .Patch ),
168174 },
169175 VendorExtensible : v2pathItem .VendorExtensible ,
170176 }
@@ -187,16 +193,16 @@ func ConvertOperation(v2Operation *spec.Operation) *spec3.Operation {
187193 Summary : v2Operation .Summary ,
188194 Deprecated : v2Operation .Deprecated ,
189195 OperationId : v2Operation .ID ,
190- Parameters : []* spec3.Parameter {},
191196 },
192197 }
193198
194199 for _ , param := range v2Operation .Parameters {
195200 if param .ParamProps .Name == "body" && param .ParamProps .Schema != nil {
196201 operation .OperationProps .RequestBody = & spec3.RequestBody {
197- RequestBodyProps : spec3.RequestBodyProps {
198- Content : make (map [string ]* spec3.MediaType ),
199- },
202+ RequestBodyProps : spec3.RequestBodyProps {},
203+ }
204+ if len (v2Operation .Consumes ) > 0 {
205+ operation .RequestBody .Content = make (map [string ]* spec3.MediaType )
200206 }
201207 for _ , consumer := range v2Operation .Consumes {
202208 operation .RequestBody .Content [consumer ] = & spec3.MediaType {
@@ -205,17 +211,20 @@ func ConvertOperation(v2Operation *spec.Operation) *spec3.Operation {
205211 },
206212 }
207213 }
214+ } else {
215+ operation .Parameters = append (operation .Parameters , ConvertParameter (param ))
208216 }
209- operation .Parameters = append (operation .Parameters , ConvertParameter (param ))
210217 }
211218
212219 operation .Responses = & spec3.Responses {ResponsesProps : spec3.ResponsesProps {
213220 Default : ConvertResponse (v2Operation .Responses .Default , v2Operation .Produces ),
214- StatusCodeResponses : make (map [int ]* spec3.Response ),
215221 },
216- VendorExtensible : v2Operation .VendorExtensible ,
222+ VendorExtensible : v2Operation .Responses . VendorExtensible ,
217223 }
218224
225+ if len (v2Operation .Responses .StatusCodeResponses ) > 0 {
226+ operation .Responses .StatusCodeResponses = make (map [int ]* spec3.Response )
227+ }
219228 for k , v := range v2Operation .Responses .StatusCodeResponses {
220229 operation .Responses .StatusCodeResponses [k ] = ConvertResponse (& v , v2Operation .Produces )
221230 }
@@ -231,12 +240,13 @@ func ConvertResponse(v2Response *spec.Response, produces []string) *spec3.Respon
231240 VendorExtensible : v2Response .VendorExtensible ,
232241 ResponseProps : spec3.ResponseProps {
233242 Description : v2Response .Description ,
234- Headers : make (map [string ]* spec3.Header ),
235- Content : make (map [string ]* spec3.MediaType ),
236243 },
237244 }
238245
239246 if v2Response .Schema != nil {
247+ if len (produces ) > 0 {
248+ response .Content = make (map [string ]* spec3.MediaType )
249+ }
240250 for _ , producer := range produces {
241251 response .ResponseProps .Content [producer ] = & spec3.MediaType {
242252 MediaTypeProps : spec3.MediaTypeProps {
@@ -261,6 +271,17 @@ func ConvertParameter(v2Param spec.Parameter) *spec3.Parameter {
261271 AllowEmptyValue : v2Param .AllowEmptyValue ,
262272 },
263273 }
274+ // Convert SimpleSchema into Schema
275+ if param .Schema == nil {
276+ param .Schema = & spec.Schema {
277+ SchemaProps : spec.SchemaProps {
278+ Type : []string {v2Param .Type },
279+ Format : v2Param .Format ,
280+ UniqueItems : v2Param .UniqueItems ,
281+ },
282+ }
283+ }
284+
264285 return param
265286}
266287
0 commit comments