Skip to content

Commit

Permalink
Merge pull request #14240 from krauselukas/speed_up_api_docs
Browse files Browse the repository at this point in the history
Create ruby script to resolve references in swagger yaml
  • Loading branch information
hennevogel committed Apr 27, 2023
2 parents 6fb44c3 + 8c733bc commit 48cf6d1
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions dist/resolve_swagger_yaml.rb
@@ -0,0 +1,67 @@
#!/usr/bin/ruby
# frozen_string_literal: true

require 'optparse'
require 'logger'
require 'yaml'
require 'json'
require 'json_refs'

class ResolveSwaggerYAML
def initialize
@log = Logger.new($stderr)
parse_arguments
check_for_file_existence
resolved_yaml = resolve_swagger_yaml
output_yaml_file(yaml_content: resolved_yaml)
end

private

# rubocop:disable Metrics/MethodLength
def parse_arguments
opt_parser = OptionParser.new do |parser|
parser.banner = 'Usage: resolve_swagger_yaml.rb [options]'
parser.on('-i', '--input PATH/TO/SWAGGER.YAML', 'specify input swagger yaml file location') { |i| @input_file = i }
parser.on('-o', '--output PATH/TO/SWAGGER.YAML', 'specify output location of resolved swagger yaml file') { |o| @output_file = o }
parser.on('-f', '--force', 'allow overwrite of an existing file') { |f| @force = f }
parser.on('-h', '--help', 'Print this help') do
puts parser
exit
end
end
opt_parser.parse!(ARGV)
end
# rubocop:enable Metrics/MethodLength

def check_for_file_existence
return if File.file?(@input_file)

@log.error("The specified input file does not exist: #{@input_file}")
exit 1
end

def resolve_swagger_yaml
yaml = YAML.load_file(@input_file)
resolved_in_json_format = nil

Dir.chdir(File.dirname(@input_file)) do
# JsonRefs can handle the yaml format too, but returns a json formated version
resolved_in_json_format = JsonRefs.call(yaml)
end

# Convert output back to yaml
resolved_in_json_format.to_yaml
end

def output_yaml_file(yaml_content:)
if File.file?(@output_file) && !@force
@log.error("The file already exists, use '-f' to force overwrite: #{@output_file}")
exit 1
end

File.write(@output_file, yaml_content)
end
end

ResolveSwaggerYAML.new

0 comments on commit 48cf6d1

Please sign in to comment.