Skip to content

Instantly share code, notes, and snippets.

@ngiyshhk
Last active October 18, 2021 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngiyshhk/4097b2fb20e4c75f347396dca1941146 to your computer and use it in GitHub Desktop.
Save ngiyshhk/4097b2fb20e4c75f347396dca1941146 to your computer and use it in GitHub Desktop.
OpenAPI ファイル分割スクリプト
################################################
# ■使い方
#
# ruby split_openapi.rb {{分割前OpenAPIファイル}}
#
# #分割対象指定のところを修正すれば分割対象を指定可能
# pathsはデフォルトで分割
################################################
require 'yaml'
require 'fileutils'
def main(filepath)
bundled = readOpenapi(filepath)
# 分割対象指定
targets = [
['components', 'responses'], # components/responses
['components', 'schemas'], # components/schemas
['components', 'parameters'] # components/parameters
]
# Refを変更後のPathに書き換え
changeRefToYamlAll(bundled, targets)
# 分割対象を別ファイルに書き出し
writeSeparatedYamlAll(bundled, targets)
# targetsをroot.yamlへの出力対象から除外
deleteTargetKeyAll(bundled, targets)
# pathsの出力
writePathYamlAll(bundled)
# root.yamlに出力するpathsの修正
bundled['paths'].each{|k, v|
bundled['paths'][k] = { '$ref': "./paths/#{changePathKeyToFilename(k)}.yaml" }
}
# root.yaml出力
open("root.yaml", 'w') {|f| YAML.dump(bundled, f)}
end
def readOpenapi(filepath)
open(filepath, 'r') {|f| YAML.load(f) }
end
def changeRefToYamlAll(obj, targets)
targets.each{|target| changeRefToYaml(obj, target)}
end
def changeRefToYaml(obj, target)
return obj.each {|o| changeRefToYaml(o, target)} if obj.is_a?(Array)
return if !obj.is_a?(Hash)
obj['$ref'].gsub!(/#\/#{target.join('/')}(.*)$/, "./#{target.join('/')}\\1.yaml") if obj.key?('$ref')
obj.filter{|k,v| k != '$ref'}.each {|k,v| changeRefToYaml(v, target)}
end
def deleteTargetKeyAll(obj, targets)
targets.each{|target| deleteTargetKey(obj, target)}
end
def deleteTargetKey(obj, target)
return obj.delete(target[0]) if target.length == 1
deleteTargetKey(obj[target[0]], target[1..-1])
end
def writeSeparatedYamlAll(bundled, targets)
targets.each{|target|
writeSeparatedYaml(bundled, target)
}
end
def writeSeparatedYaml(obj, origin_target, target = origin_target)
return writeSeparatedYaml(obj[target[0]], origin_target, target[1..-1]) if target.length > 1
FileUtils.mkdir_p(origin_target.join('/'))
obj[target[0]].each {|k,v|
changeRefsRoot(v, origin_target.length)
open("#{origin_target.join('/')}/#{k}.yaml", 'w') {|f| YAML.dump(v, f) }
}
end
def changeRefsRoot(obj, depth)
return obj.each {|o| changeRefsRoot(o, depth)} if obj.is_a?(Array)
return if !obj.is_a?(Hash)
obj['$ref'].gsub!(/^\./, Array.new(depth, "..").join('/')) if obj.key?('$ref')
obj.filter{|k,v| k != '$ref'}.each {|k,v| changeRefsRoot(v, depth)}
end
def writePathYamlAll(bundled)
FileUtils.mkdir_p('paths')
bundled['paths'].each{|k, v| writePathYaml(k, v)}
end
def writePathYaml(key, value)
yaml_name = changePathKeyToFilename(key)
changeRefsRoot(value, 1)
open("paths/#{yaml_name}.yaml", 'w') {|f| YAML.dump(value, f) }
end
def changePathKeyToFilename(key)
key.split('/')
.filter{|v| !v.empty?}
.map{|v| v.gsub(/[{}]/, '')}
.join('-')
end
main ARGV[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment