Class: OpenApiImport

Inherits:
Object
  • Object
show all
Defined in:
lib/open_api_import.rb

Class Method Summary collapse

Class Method Details

.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path) ⇒ Object

Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses. The http methods that will be treated are: 'get','post','put','delete', 'patch'.

Parameters:

  • swagger_file (String)

    . Path and file name. Could be absolute or relative to project root folder.

  • include_responses (Boolean) (defaults to: true)

    . (default: true) if you want to add the examples of responses in the resultant file.

  • mock_response (Boolean) (defaults to: false)

    . (default:false) Add the first response on the request as mock_response to be used. In case using nice_http gem: if NiceHttp.use_mocks = true will use it instead of getting the real response from the WS.

  • create_method_name (Symbol) (defaults to: :operation_id)

    . (:path, :operation_id, :operationId) (default: operation_id). How the name of the methods will be generated. path: it will be used the path and http method, for example for a GET on path: /users/list, the method name will be get_users_list operation_id: it will be used the operationId field but using the snake_case version, for example for listUsers: list_users operationId: it will be used the operationId field like it is, for example: listUsers

  • name_for_module (Symbol) (defaults to: :path)

    . (:path, :path_file, :fixed, :tags, :tags_file) (default: :path). How the module names will be created. path: It will be used the first folder of the path to create the module name, for example the path /users/list will be in the module Users and all the requests from all modules in the same file. path_file: It will be used the first folder of the path to create the module name, for example the path /users/list will be in the module Users and each module will be in a new requests file. tags: It will be used the tags key to create the module name, for example the tags: [users,list] will create the module UsersList and all the requests from all modules in the same file. tags_file: It will be used the tags key to create the module name, for example the tags: [users,list] will create the module UsersList and and each module will be in a new requests file. fixed: all the requests will be under the module Requests



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/open_api_import.rb', line 27

def self.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path)
  begin
    f = File.new("#{swagger_file}_open_api_import.log", "w")
    f.sync = true
    @logger = Logger.new f
  rescue StandardError => e
    warn "Not possible to create the Logger file"
    warn e
    @logger = Logger.new nil
  end

  begin
    @logger.info "swagger_file: #{swagger_file}, include_responses: #{include_responses}, mock_response: #{mock_response}\n"
    @logger.info "create_method_name: #{create_method_name}, name_for_module: #{name_for_module}\n"

    file_to_convert = if swagger_file["./"].nil?
                        swagger_file
                      else
                        Dir.pwd.to_s + "/" + swagger_file.gsub("./", "")
                      end
    unless File.exist?(file_to_convert)
      raise "The file #{file_to_convert} doesn't exist"
    end

    file_errors = file_to_convert + ".errors.log"
    File.delete(file_errors) if File.exist?(file_errors)
    import_errors = ""

    definition = OasParser::Definition.resolve(swagger_file)
    raw = definition.raw.deep_symbolize_keys

    if raw.key?(:openapi) && (raw[:openapi].to_f > 0)
      raw[:swagger] = raw[:openapi]
    end
    if raw[:swagger].to_f < 2.0
      raise "Unsupported Swagger version. Only versions >= 2.0 are valid."
    end

    base_host = ""
    base_path = ""

    base_host = raw[:host] if raw.key?(:host)
    base_path = raw[:basePath] if raw.key?(:basePath)
    module_name = raw[:info][:title].camel_case
    module_version = "V#{raw[:info][:version].to_s.snake_case}"

    output = []
    output_header = []
    output_header << "#" * 50
    output_header << "# #{raw[:info][:title]}"
    output_header << "# version: #{raw[:info][:version]}"
    output_header << "# description: "
    raw[:info][:description].to_s.split("\n").each do |d|
      output_header << "#     #{d}" unless d == ""
    end
    output_header << "#" * 50

    output_header << "module Swagger"
    output_header << "module #{module_name}"
    output_header << "module #{module_version}"
    output_header << "module Requests" if name_for_module == :fixed

    files = {}

    module_requests = ""

    definition.paths.each do |path|

      raw = path.raw.deep_symbolize_keys

      if raw.key?(:parameters)
        raw.each do |met, cont|
          if met != :parameters
            if raw[met].key?(:parameters)
              #todo: check if in some cases the parameters on the method can be added to the ones in the path
              #raw[met][:parameters] = raw[met][:parameters] & raw[:parameters]
            else
              raw[met][:parameters] = raw[:parameters]
            end
          end
        end
        raw.delete(:parameters)
      end

      raw.each do |met, cont|

        if %w[get post put delete patch].include?(met.to_s.downcase)
          params = []
          params_path = []
          params_query = []
          params_required = []
          description_parameters = []
          data_required = []
          data_read_only = []
          data_default = []
          data_examples = []
          data_pattern = []
          responses = []

          # for the case operationId is missing
          cont[:operationId] = "unknown" unless cont.key?(:operationId)

          if create_method_name == :path
            method_name = (met.to_s + "_" + path.path.to_s).snake_case
            method_name.chop! if method_name[-1] == "_"
          elsif create_method_name == :operation_id
            if (name_for_module == :tags or name_for_module == :tags_file) and cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
              metnametmp = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, '')
            else
              metnametmp = cont[:operationId]
            end
            method_name = metnametmp.to_s.snake_case
          else
            if (name_for_module == :tags or name_for_module == :tags_file) and cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
              method_name = cont[:operationId].gsub(/^#{cont[:tags].join}[\s_]*/, '')
            else
              method_name = cont[:operationId]
            end
          end

          path_txt = path.path.dup.to_s
          if [:path, :path_file, :tags, :tags_file].include?(name_for_module)
            old_module_requests = module_requests
            if [:path, :path_file].include?(name_for_module)
              # to remove version from path fex: /v1/Customer
              path_requests = path_txt.gsub(/^\/v[\d\.]*\//i, "")
              # to remove version from path fex: /1.0/Customer
              path_requests = path_requests.gsub(/^\/[\d\.]*\//i, "")
              if (path_requests == path_txt) && (path_txt.scan("/").size == 1)
                # no folder in path
                module_requests = "Root"
              else
                res_path = path_requests.scan(/(\w+)/)
                module_requests = res_path[0][0].camel_case
              end
            else
              if cont.key?(:tags) and cont[:tags].is_a?(Array) and cont[:tags].size>0
                module_requests = cont[:tags].join(" ").camel_case
              else
                module_requests = "Unknown"
              end
            end
            if old_module_requests != module_requests
              output << "end" unless old_module_requests == "" or name_for_module == :path_file or name_for_module == :tags_file
              if name_for_module == :path or name_for_module == :tags
                # to add the end for the previous module unless is the first one
                output << "module #{module_requests}"
              else #:path_file, :tags_file
                if old_module_requests != ""
                  unless files.key?(old_module_requests)
                    files[old_module_requests] = Array.new
                  end
                  files[old_module_requests].concat(output)
                  output = Array.new
                end
                output << "module #{module_requests}" unless files.key?(module_requests) # don't add in case already existed
              end
            end
          end

          output << ""
          output << "# operationId: #{cont[:operationId]}, method: #{met}"
          output << "# summary: #{cont[:summary]}"
          if !cont[:description].to_s.split("\n").empty?
            output << "# description: "
            cont[:description].to_s.split("\n").each do |d|
              output << "#     #{d}" unless d == ""
            end
          else
            output << "# description: #{cont[:description]}"
          end

          mock_example = []

          if include_responses && cont.key?(:responses) && cont[:responses].is_a?(Hash)
            cont[:responses].each do |k, v|
              response_example = []

              response_example = get_response_examples(v)
              
              if !response_example.empty?
                responses << "'#{k}': { "
                responses << "message: '#{v[:description]}', "
                responses << "data: "
                responses << response_example
                responses << "},"
                
                if mock_response and mock_example.size==0
                  mock_example << "code: '#{k}',"
                  mock_example << "message: '#{v[:description]}',"
                  mock_example << "data: "
                  mock_example << response_example
                end
                
              else
                responses << "'#{k}': { message: '#{v[:description]}'}, "
              end

            end
          end
          # todo: for open api 3.0 add the new Link feature: https://swagger.io/docs/specification/links/
          # todo: for open api 3.0 is not getting the required params in all cases

          # for the case open api 3 with cont.requestBody.content.'applicatin/json'.schema
          # example: petstore-expanded.yaml operationId=addPet
          if cont.key?(:requestBody) and cont[:requestBody].key?(:content) and 
            cont[:requestBody][:content].key?(:'application/json') and cont[:requestBody][:content][:'application/json'].key?(:schema)
            cont[:parameters] = [] unless cont.key?(:parameters)
            cont[:parameters] << {in: 'body', schema: cont[:requestBody][:content][:'application/json'][:schema] }
          end

          if cont.key?(:parameters) && cont[:parameters].is_a?(Array)
            cont[:parameters].each do |p|
              if p.keys.include?(:schema) and p[:schema].include?(:type)
                type = p[:schema][:type]
              elsif p.keys.include?(:type)
                type = p[:type]
              else
                type = ""
              end
              if p[:in] == "path"
                if create_method_name == :operationId
                  param_name = p[:name]
                  path_txt.gsub!("{#{param_name}}", "\#{#{param_name}}")
                else
                  param_name = p[:name].to_s.snake_case
                  path_txt.gsub!("{#{p[:name]}}", "\#{#{param_name}}")
                end
                params_path << param_name
                #params_required << param_name if p[:required].to_s=="true"
                description_parameters << "#    #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}"
              elsif p[:in] == "query"
                params_query << p[:name]
                params_required << p[:name] if p[:required].to_s=="true"
                description_parameters << "#    #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}"
              elsif p[:in] == "body"
                if p.keys.include?(:schema)
                  if p[:schema].key?(:oneOf)
                    bodies = p[:schema][:oneOf]
                  elsif p[:schema].key?(:anyOf)
                    bodies = p[:schema][:anyOf]
                  elsif p[:schema].key?(:allOf)
                    bodies = p[:schema][:allOf]
                  else
                    bodies = [p[:schema]]
                  end

                  params_data = []

                  bodies.each do |body|
                    if body.keys.include?(:required) and body[:required].size > 0
                      output << "# required data: #{body[:required].join(", ")}"
                      data_required += body[:required]
                    end

                    if body.keys.include?(:properties) and body[:properties].size > 0
                      body[:properties].each { |dpk, dpv|
                        if dpv.keys.include?(:example)
                          valv = dpv[:example]
                        else
                          if dpv.type == "object"
                            valv = "{}"
                          else  
                            valv = ""
                          end
                        end
                        if dpv.keys.include?(:description)
                          description_parameters << "#    #{dpk}: (#{dpv[:type]}) #{dpv[:description]}"
                        end
                        if dpv.keys.include?(:pattern)
                          data_pattern << "#{dpk}: /#{dpv[:pattern].to_s.gsub("\\/", "\/")}/"
                        end
                        if dpv.keys.include?(:readOnly) and dpv[:readOnly] == true
                          data_read_only << dpk
                        end
                        if dpv.keys.include?(:default)
                          if dpv.type != "string"
                            data_default << "#{dpk}: #{dpv[:default]}"
                          else
                            data_default << "#{dpk}: '#{dpv[:default]}'"
                          end
                        end

                        if dpv[:type].downcase == "string"
                          valv = '"' + valv + '"'
                        else
                          #todo: consider check default and insert it
                          if valv.to_s == ""
                            valv = '"' + valv + '"'
                          end
                        end
                        params_data << "#{dpk}: #{valv}"
                      }
                      if params_data.size > 0
                        data_examples << params_data
                        params_data = []
                      end
                    end
                  end
                end
              end
            end

            params = params_path

            unless params_query.empty?
              path_txt += "?"
              params_required.each do |pr|
                if params_query.include?(pr)
                  if create_method_name == :operationId
                    path_txt += "#{pr}=\#{#{pr}}&"
                    params << "#{pr}"
                  else
                    path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&"
                    params << "#{pr.to_s.snake_case}"
                  end
                    
                end
              end
              params_query.each do |pq|
                unless params_required.include?(pq)
                  if create_method_name == :operationId
                    path_txt += "#{pq}=\#{#{pq}}&"
                    params << "#{pq}: ''"
                  else
                    path_txt += "#{pq}=\#{#{pq.to_s.snake_case}}&"
                    params << "#{pq.to_s.snake_case}: ''"
                  end
                end
              end
            end
          end

          if description_parameters.size > 0
            output << "# parameters description: "
            output << description_parameters
          end

          #for the case we still have some parameters on path that were not in 'parameters'
          if path_txt.scan(/[^#]{\w+}/).size > 0
            paramst = []
            prms = path_txt.scan(/[^#]{(\w+)}/)
            prms.each do |p|
              paramst<<p[0].to_s.snake_case
              path_txt.gsub!("{#{p[0]}}", "\#{#{p[0].to_s.snake_case}}")
            end
            paramst.concat params
            params = paramst
          end
          
          output << "def self.#{method_name} (#{params.join(", ")})"

          output << "{"

          output << "path: \"#{base_path}#{path_txt}\","

          output << "method: :#{met}," if met.to_s != ""

          unless data_required.empty?
            output << "data_required: ["
            output << ":#{data_required.uniq.join(", :")}"
            output << "],"
          end
          unless data_read_only.empty?
            output << "data_read_only: ["
            output << ":#{data_read_only.uniq.join(", :")}"
            output << "],"
          end
          unless data_default.empty?
            output << "data_default: {"
            output << data_default.join(", \n")
            output << "},"
          end

          unless data_pattern.empty?
            output << "data_pattern: {"
            output << data_pattern.uniq.join(", \n")
            output << "},"
          end

          unless data_examples.empty?
            output << "data_examples: ["
            data_examples.each do |data|
              output << "{"
              output << data.join(", \n")
              output << "}, "
            end
            output << "],"
          end

          unless mock_example.empty?
            output << "mock_response: {"
            output << mock_example
            output << "},"
          end

          unless responses.empty?
            output << "responses: {"
            output << responses
            output << "},"
          end

          output << "}"
          output << "end"
        else
          @logger.warn "Not imported method: #{met} for path: #{path.path} since it is not supported by OpenApiImport"
        end
      end
    end

    output_footer = []

    output_footer << "end" unless (module_requests == "") && ([:path, :path_file, :tags, :tags_file].include?(name_for_module))
    output_footer << "end" << "end" << "end"

    if files.size == 0
      output = output_header + output + output_footer
      output_txt = output.join("\n")
      requests_file_path = file_to_convert + ".rb"
      File.open(requests_file_path, "w") { |file| file.write(output_txt) }
      `rufo #{requests_file_path}`
      message = "** Requests file: #{swagger_file}.rb that contains the code of the requests after importing the Swagger file"
      puts message
      @logger.info message
    else
      unless files.key?(module_requests)
        files[module_requests] = Array.new
      end
      files[module_requests].concat(output) #for the last one

      requires_txt = ""
      message = "** Generated files that contain the code of the requests after importing the Swagger file: "
      puts message
      @logger.info message
      files.each do |mod, out_mod|
        output = output_header + out_mod + output_footer
        output_txt = output.join("\n")
        requests_file_path = file_to_convert + "_" + mod + ".rb"
        requires_txt += "require_relative '#{File.basename(swagger_file)}_#{mod}'\n"
        File.open(requests_file_path, "w") { |file| file.write(output_txt) }
        `rufo #{requests_file_path}`
        message = "  - #{requests_file_path}"
        puts message
        @logger.info message
      end

      requests_file_path = file_to_convert + ".rb"
      File.open(requests_file_path, "w") { |file| file.write(requires_txt) }
      `rufo #{requests_file_path}`
      message = "** File that contains all the requires for all Request files: \n"
      message += "   - #{requests_file_path} "
      puts message
      @logger.info message
    end

    begin
      res = eval(output_txt)
    rescue Exception => stack
      import_errors += "\n\nResult evaluating the ruby file generated: \n" + stack.to_s
    end

    if import_errors.to_s != ""
      File.open(file_errors, "w") { |file| file.write(import_errors) }
      message = "* It seems there was a problem importing the Swagger file #{file_to_convert}\n"
      message += "* Take a look at the detected errors at #{file_errors}\n"
      warn message
      @logger.fatal message
      return false
    else
      return true
    end
  rescue StandardError => stack
    puts stack.message
    @logger.fatal stack.message
    @logger.fatal stack.backtrace
    puts stack.backtrace
  end
end