Class: OpenApiImport
- Inherits:
-
Object
- Object
- OpenApiImport
- Defined in:
- lib/open_api_import/open_api_import.rb
Class Method Summary collapse
-
.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path, silent: false, create_constants: false) ⇒ void
Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses.
Class Method Details
.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path, silent: false, create_constants: false) ⇒ void
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'.
22 23 24 25 26 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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
# File 'lib/open_api_import/open_api_import.rb', line 22 def self.from(swagger_file, create_method_name: :operation_id, include_responses: true, mock_response: false, name_for_module: :path, silent: false, create_constants: false) begin f = File.new("#{swagger_file}_open_api_import.log", "w") f.sync = true @logger = Logger.new f puts "Logs file: #{swagger_file}_open_api_import.log" unless silent 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 = "" required_constants = [] begin definition = OasParser::Definition.resolve(swagger_file) rescue Exception => stack = "There was a problem parsing the Open Api document using the oas_parser gem. The execution was aborted.\n" += "Visit the github for oas_parser gem for bugs and more info: https://github.com/Nexmo/oas_parser\n" += "Error: #{stack.}" puts @logger.fatal @logger.fatal stack.backtrace exit! end 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) #in case parameters for all methods in path is present 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 = [] params_data = [] description_parameters = [] data_form = [] data_required = [] #todo: add nested one.true.three to data_read_only data_read_only = [] data_default = [] data_examples = [] data_pattern = [] responses = [] # for the case operationId is missing cont[:operationId] = "undefined" 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_]*/, "") cont[:tags].join.split(" ").each do |tag| metnametmp.gsub!(/^#{tag}[\s_]*/i, "") end metnametmp = met if metnametmp == "" 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_]*/, "") cont[:tags].join.split(" ").each do |tag| method_name.gsub!(/^#{tag}[\s_]*/i, "") end method_name = met if method_name == "" 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 = "Undefined" end end # to remove from method_name: v1_list_regions and add it to module if /^(?<vers>v\d+)/i =~ method_name method_name.gsub!(/^#{vers}_?/, "") module_requests = (vers.capitalize + module_requests).camel_case unless module_requests.start_with?(vers) 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].split("\n").join("\n# ")}" if cont.key?(: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) data_pattern += get_patterns("", v[:schema]) if v.key?(:schema) data_pattern.uniq! v[:description] = v[:description].to_s.gsub("'", %q(\\\')) 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 data_examples_all_of = false 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 unless params_path.include?(param_name) if create_constants params_path << "#{param_name}: #{param_name.upcase}" required_constants << param_name.upcase else params_path << param_name end #params_required << param_name if p[:required].to_s=="true" @logger.warn "Description key is missing for #{met} #{path.path} #{p[:name]}" if p[:description].nil? description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s == "true"} #{p[:description].to_s.split("\n").join("\n#\t\t\t")}" end elsif p[:in] == "query" params_query << p[:name] params_required << p[:name] if p[:required].to_s == "true" @logger.warn "Description key is missing for #{met} #{path.path} #{p[:name]}" if p[:description].nil? description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s == "true"} #{p[:description].to_s.split("\n").join("\n#\t\t\t")}" elsif p[:in] == "formData" or p[:in] == "formdata" #todo: take in consideration: default, required #todo: see if we should add the required as params to the method and not required as options #todo: set on data the required fields with the values from args description_parameters << "# #{p[:name]}: (#{p[:type]}) #{p[:description].split("\n").join("\n#\t\t\t")}" case p[:type] when /^string$/i data_form << "#{p[:name]}: ''" when /^boolean$/i data_form << "#{p[:name]}: true" when /^number$/i data_form << "#{p[:name]}: 0" when /^integer$/i data_form << "#{p[:name]}: 0" else puts "! on formData not supported type #{p[:type]}" end 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) data_examples_all_of, bodies = get_data_all_of_bodies(p) bodies.unshift(p[:schema]) if p[:schema].key?(:required) or p.key?(:required) #todo: check if this 'if' is necessary data_examples_all_of = true # because we are on data and allOf already else bodies = [p[:schema]] end params_data = [] bodies.each do |body| data_required += get_required_data(body) all_properties = [] all_properties << body[:properties] if body.keys.include?(:properties) and body[:properties].size > 0 if body.key?(:allOf) body[:allOf].each do |item| all_properties << item[:properties] if item.key?(:properties) end end all_properties.each do |props| props.each { |dpk, dpv| if dpv.keys.include?(:example) if dpv[:example].is_a?(Array) and dpv.type != "array" valv = dpv[:example][0] else valv = dpv[:example].to_s end else if dpv.type == "object" if dpv.key?(:properties) valv = get_examples(dpv[:properties], :key_value, true).join("\n") else valv = "{}" end elsif dpv.type == "array" if dpv.key?(:items) valv = get_examples({ dpk => dpv }, :only_value) valv = valv.join("\n") else valv = "[]" end else valv = "" end end if dpv.keys.include?(:description) description_parameters << "# #{dpk}: (#{dpv[:type]}) #{dpv[:description].split("\n").join("\n#\t\t\t")}" end data_pattern += get_patterns(dpk, dpv) data_pattern.uniq! dpkeys = [] data_pattern.reject! do |dp| dpkey = dp.scan(/^'[\w\.]+'/) if dpkeys.include?(dpkey) true else dpkeys << dpkey false end end if dpv.keys.include?(:readOnly) and dpv[:readOnly] == true data_read_only << dpk end if dpv.keys.include?(:default) if dpv[:default].nil? data_default << "#{dpk}: nil" elsif dpv.type != "string" data_default << "#{dpk}: #{dpv[:default]}" else data_default << "#{dpk}: '#{dpv[:default]}'" end end #todo: consider check default and insert it #todo: remove array from here and add the option to get_examples for the case thisisthekey: ['xxxx'] if dpv.key?(:type) and dpv[:type] != "array" params_data << get_examples({ dpk => dpv }, :only_value, true).join params_data[-1].chop!.chop! if params_data[-1].to_s[-2..-1] == ", " params_data.pop if params_data[-1].match?(/^\s*$/im) else if valv.to_s == "" valv = '""' elsif valv.include?('"') valv.gsub!('"', "'") unless valv.include?("'") end params_data << "#{dpk}: #{valv}" end } if params_data.size > 0 if data_examples_all_of == true and data_examples.size > 0 data_examples[0] += params_data else data_examples << params_data end params_data = [] end end end unless data_required.empty? data_required.uniq! output << "# required data: #{data_required.inspect}" end end elsif p[:in] == "header" #todo: see how we can treat those cases else puts "! not imported data with :in:#{p[:in]} => #{p.inspect}" end end params = params_path unless params_query.empty? path_txt += "?" params_required.each do |pr| if create_constants if params_query.include?(pr) if create_method_name == :operationId path_txt += "#{pr}=\#{#{pr}}&" params << "#{pr}: #{pr.upcase}" required_constants << pr.upcase else path_txt += "#{pr}=\#{#{pr.to_s.snake_case}}&" params << "#{pr.to_s.snake_case}: #{pr.to_s.snake_case.upcase}" required_constants << pr.to_s.snake_case.upcase end end else 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 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.uniq 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| #if create_constants # paramst<<"#{p[0].to_s.snake_case}: #{p[0].to_s.snake_case.upcase}" # required_constants << p[0].to_s.snake_case.upcase #else paramst << p[0].to_s.snake_case #end path_txt.gsub!("{#{p[0]}}", "\#{#{p[0].to_s.snake_case}}") end paramst.concat params params = paramst end params.uniq! output << "def self.#{method_name} (#{params.join(", ")})" output << "{" output << "name: \"#{module_requests}.#{method_name}\"," 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: {" data_default.uniq! output << data_default.join(", \n") output << "}," end unless data_pattern.empty? output << "data_pattern: {" output << data_pattern.uniq.join(", \n") output << "}," end unless data_form.empty? data_examples << data_form end unless data_examples.empty? unless data_required.empty? reqdata = [] begin data_examples[0].uniq! data_ex = eval("{#{data_examples[0].join(", ")}}") rescue SyntaxError data_ex = {} @logger.warn "Syntax error: #{met} for path: #{path.path} evaluating data_examples[0] => #{data_examples[0].inspect}" rescue data_ex = {} @logger.warn "Syntax error: #{met} for path: #{path.path} evaluating data_examples[0] => #{data_examples[0].inspect}" end if (data_required.grep(/\./)).empty? reqdata = filter(data_ex, data_required) #not nested else reqdata = filter(data_ex, data_required, true) #nested end unless reqdata.empty? reqdata.uniq! phsd = pretty_hash_symbolized(reqdata) phsd[0] = "data: {" output += phsd end end unless data_read_only.empty? or !data_required.empty? reqdata = [] #remove read only fields from :data data_examples[0].each do |edata| read_only = false data_read_only.each do |rdata| if edata.scan(/^#{rdata}:/).size > 0 read_only = true break elsif edata.scan(/:/).size == 0 break end end reqdata << edata unless read_only end unless reqdata.empty? reqdata.uniq! output << "data: {" output << reqdata.join(", \n") output << "}," end end output << "data_examples: [" data_examples.each do |data| output << "{" data.uniq! 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 = [] << "end" unless (module_requests == "") && ([:path, :path_file, :tags, :tags_file].include?(name_for_module)) << "end" << "end" << "end" if files.size == 0 output = output_header + output + output_txt = output.join("\n") requests_file_path = file_to_convert + ".rb" File.open(requests_file_path, "w") { |file| file.write(output_txt) } res_rufo = `rufo #{requests_file_path}` = "** Requests file: #{swagger_file}.rb that contains the code of the requests after importing the Swagger file" puts unless silent @logger.info @logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/) @logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK") else unless files.key?(module_requests) files[module_requests] = Array.new end files[module_requests].concat(output) #for the last one requires_txt = "" = "** Generated files that contain the code of the requests after importing the Swagger file: " puts unless silent @logger.info files.each do |mod, out_mod| output = output_header + out_mod + 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) } res_rufo = `rufo #{requests_file_path}` = " - #{requests_file_path}" puts unless silent @logger.info @logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/) @logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK") end requests_file_path = file_to_convert + ".rb" if required_constants.size > 0 rconsts = "# Required constants\n" required_constants.uniq! required_constants.each do |rq| rconsts += "#{rq} ||= ENV['#{rq}'] ||=''\n" end rconsts += "\n\n" else rconsts = "" end File.open(requests_file_path, "w") { |file| file.write(rconsts + requires_txt) } res_rufo = `rufo #{requests_file_path}` = "** File that contains all the requires for all Request files: \n" += " - #{requests_file_path} " puts unless silent @logger.info @logger.error " Error formating with rufo" unless res_rufo.to_s.match?(/\AFormat:.+$\s*\z/) @logger.error " Syntax Error: #{`ruby -c #{requests_file_path}`}" unless `ruby -c #{requests_file_path}`.include?("Syntax OK") 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) } = "* It seems there was a problem importing the Swagger file #{file_to_convert}\n" += "* Take a look at the detected errors at #{file_errors}\n" warn @logger.fatal return false else return true end rescue StandardError => stack puts stack. @logger.fatal stack. @logger.fatal stack.backtrace puts stack.backtrace end end |