Class: OpenApi::Generator
- Inherits:
-
Object
- Object
- OpenApi::Generator
- Defined in:
- lib/open-api/generator.rb
Constant Summary collapse
- HIDDEN_ROOT_KEYS =
[:output_file_path, :base_paths]
Class Method Summary collapse
- .add_path(route_wrapper, paths, common_base_path, opts = {}) ⇒ Object
- .build(opts = {}) ⇒ Object
- .build_endpoint_content(base_paths, opts = {}) ⇒ Object
- .find_base_paths(opts = {}) ⇒ Object
- .find_common_base_path(base_paths) ⇒ Object
- .log_message(level, message, opts = {}) ⇒ Object
- .write(opts = {}) ⇒ Object
Class Method Details
.add_path(route_wrapper, paths, common_base_path, opts = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/open-api/generator.rb', line 84 def add_path(route_wrapper, paths, common_base_path, opts = {}) relative_path = OpenApi::Endpoints.relative_path(route_wrapper.path.to_s, common_base_path) route_wrapper.parts.each do |path_param| relative_path = relative_path .gsub(%r{(\A|\/)\:(#{path_param})(\Z|\/)}, '\1{\2}\3') .gsub(/\(\.\:#{path_param}\)/, ".{#{path_param}}") end path = (paths[relative_path] ||= {}) verb_key = OpenApi::Endpoints.verb_key(route_wrapper) if path.include?(verb_key) = "Warning: Multiple OpenApi::Endpoints match #{route_wrapper.verb} " \ "#{relative_path} ... skipping entry for route" if route_wrapper.name.present? (:warn, "#{base_message} '#{route_wrapper.name}'", opts) else (:warn, "#{base_message} #{route_wrapper.verb} #{route_wrapper.path}", opts) end return nil end path end |
.build(opts = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/open-api/generator.rb', line 6 def build(opts = {}) base_paths = find_base_paths(opts) doc = OpenApi..reject { |k, _v| HIDDEN_ROOT_KEYS.include?(k.to_sym) } doc[:info] = OpenApi::Utils.(doc[:info]) if doc[:info].is_a?(Hash) , paths, definitions = build_endpoint_content(base_paths, opts) doc[:tags] = OpenApi::Utils.(.values) if .present? doc[:paths] = OpenApi::Utils.(paths, start_depth: 2, end_depth: 4) doc[:definitions] = OpenApi::Utils.(definitions, start_depth: 2, end_depth: 3) doc = OpenApi::Utils.(doc, end_depth: 2) doc[:swagger] = doc[:swagger].to_s if doc.include?(:swagger) doc end |
.build_endpoint_content(base_paths, opts = {}) ⇒ Object
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 |
# File 'lib/open-api/generator.rb', line 51 def build_endpoint_content(base_paths, opts = {}) paths = {} = {} definitions = {} common_base_path = find_common_base_path(base_paths) global_opts = opts.merge(base_path: common_base_path) base_paths.each do |base_path, base_path_opts| opts = global_opts.merge(base_path_opts) OpenApi::Endpoints.find_matching_routes(base_path, opts).each do |route_wrapper| controller_name = (route_wrapper.controller).split('/').map(&:camelize).join('::') + 'Controller' controller = controller_name.constantize if controller.nil? (:warn, "Can't resolve controller: #{route_wrapper.controller}", opts) next end next unless controller.respond_to?(:open_api_endpoint_metadata) = controller.(route_wrapper.action, route_wrapper.path, opts.merge(base_path: base_path)) next if [:hidden] path = add_path(route_wrapper, paths, common_base_path, opts) next if path.nil? OpenApi::Endpoints.() = OpenApi::Objects.resolve_refs(, definitions, controller, opts) = OpenApi::Tags.resolve_refs(, , controller, opts) path[OpenApi::Endpoints.verb_key(route_wrapper)] = end end [, paths, definitions] end |
.find_base_paths(opts = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/open-api/generator.rb', line 106 def find_base_paths(opts = {}) base_paths = opts[:base_paths] || OpenApi.[:base_paths] if base_paths.is_a?(Array) base_paths = base_paths.map(&:to_s).reject(&:blank?).uniq base_paths = Hash[(base_paths.map do |base_path| [base_path.starts_with?('/') ? base_path : "/#{base_path}", {}] end)] elsif base_paths.is_a?(Hash) base_paths = Hash[(base_paths.map do |base_path, api_opts| fail "Expected options hash for base path '#{base_path}'" unless api_opts.is_a?(Hash) [base_path.starts_with?('/') ? base_path : "/#{base_path}", api_opts] end)] else fail "Invalid value for 'base_paths': Expected Hash or Array" end if base_paths.blank? fail 'Missing API base paths; Must be passed as base_paths option, or base_paths must ' \ 'be configured in the OpenApi initializer (config/initializers/open_api.rb)' end base_paths end |
.find_common_base_path(base_paths) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/open-api/generator.rb', line 128 def find_common_base_path(base_paths) return nil if base_paths.blank? split_paths = base_paths.map do |base_path| base_path.split('/').reject(&:blank?) end path_count = split_paths.length first_path = split_paths[0] return "/#{first_path.join('/')}" if path_count == 1 common_elems = 0 while common_elems < first_path.length path_elem_idx = 0 while path_elem_idx < path_count - 1 cmp_path = split_paths[path_elem_idx + 1] break if cmp_path.length <= common_elems break if cmp_path[common_elems] != first_path[common_elems] path_elem_idx += 1 end break if path_elem_idx < path_count - 1 common_elems += 1 end return '/' if common_elems == 0 "/#{first_path[0..(common_elems - 1)].join('/')}" end |
.log_message(level, message, opts = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/open-api/generator.rb', line 40 def (level, , opts = {}) unless [:debug, :info, :warn, :error, :fatal].include?(level) fail "Invalid message level: #{level}" end if opts[:stdout] puts "[#{level}] #{message}" else Rails.logger.send(level, .to_s) end end |
.write(opts = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/open-api/generator.rb', line 24 def write(opts = {}) output_file_path = opts[:output_file_path] || OpenApi.[:output_file_path] unless output_file_path.respond_to?(:to_s) && output_file_path.to_s.present? fail 'Missing output file path; Must be passed as output_file_path option, or ' \ 'output_file_path must be configured in the OpenApi initializer ' \ '(config/initializers/open_api.rb)' end doc = nil File.open(output_file_path.to_s, 'w') do |file| file.write JSON.pretty_generate(doc = build(opts)) end doc end |