Class: Rage::OpenAPI::Converter
- Inherits:
-
Object
- Object
- Rage::OpenAPI::Converter
- Defined in:
- lib/rage/openapi/converter.rb
Instance Method Summary collapse
-
#initialize(nodes) ⇒ Converter
constructor
A new instance of Converter.
- #run ⇒ Object
Constructor Details
#initialize(nodes) ⇒ Converter
Returns a new instance of Converter.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rage/openapi/converter.rb', line 5 def initialize(nodes) @nodes = nodes @used_tags = Set.new @used_security_schemes = Set.new @spec = { "openapi" => "3.0.0", "info" => {}, "components" => {}, "tags" => [], "paths" => {} } end |
Instance Method Details
#run ⇒ Object
19 20 21 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 |
# File 'lib/rage/openapi/converter.rb', line 19 def run @spec["info"] = { "version" => @nodes.version || "1.0.0", "title" => @nodes.title || build_app_name } @spec["paths"] = @nodes.leaves.each_with_object({}) do |node, memo| next if node.private || node.parents.any?(&:private) path_params = [] path = node.http_path.gsub(/:(\w+)/) do path_params << $1 "{#{$1}}" end unless memo.key?(path) memo[path] = {} path_params.each do |param| documented_path_param = node.parameters.delete(param) (memo[path]["parameters"] ||= []) << { "in" => "path", "name" => param, "required" => true, "description" => documented_path_param&.dig(:description) || "", "schema" => get_param_type_spec(param, documented_path_param&.dig(:type)) } end end method = node.http_method.downcase memo[path][method] = { "summary" => node.summary || "", "description" => node.description&.join(" ") || "", "deprecated" => !!(node.deprecated || node.parents.any?(&:deprecated)), "security" => build_security(node), "tags" => (node) } if node.parameters.any? has_file_param = node.parameters.values.any? { |p| !p.key?(:ref) && p[:type] && p[:type]["format"] == "binary" } if has_file_param schema_properties = {} schema_required = [] query_ref_parameters = [] # When file params are present, non-ref params become part of the multipart/form-data # request body schema. Shared refs (e.g., #/components/parameters/...) are kept as # regular parameter references since we can't inline their definitions into the schema. node.parameters.each do |param_name, param_info| if param_info.key?(:ref) # shared parameter refs stay as top-level parameters query_ref_parameters << param_info[:ref] else # inline params become properties in the multipart schema property_schema = get_param_type_spec(param_name, param_info[:type]).dup if param_info[:description] && !param_info[:description].empty? property_schema["description"] = param_info[:description] end schema_properties[param_name] = property_schema schema_required << param_name if param_info[:required] end end memo[path][method]["requestBody"] = { "content" => { "multipart/form-data" => { "schema" => { "type" => "object", "properties" => schema_properties }.tap { |s| s["required"] = schema_required if schema_required.any? } } } } memo[path][method]["parameters"] = query_ref_parameters if query_ref_parameters.any? else memo[path][method]["parameters"] = build_parameters(node) end end responses = node.parents.reverse.map(&:responses).reduce(&:merge).merge(node.responses) memo[path][method]["responses"] = if responses.any? responses.each_with_object({}) do |(status, response), memo| memo[status] = if response.nil? { "description" => "" } elsif response.key?("$ref") && response["$ref"].start_with?("#/components/responses") response else { "description" => "", "content" => { "application/json" => { "schema" => response } } } end end else { "200" => { "description" => "" } } end if node.request if node.request.key?("$ref") && node.request["$ref"].start_with?("#/components/requestBodies") memo[path][method]["requestBody"] = node.request else memo[path][method]["requestBody"] ||= {} (memo[path][method]["requestBody"]["content"] ||= {})["application/json"] = { "schema" => node.request } end end end if @used_security_schemes.any? @spec["components"]["securitySchemes"] = @used_security_schemes.each_with_object({}) do |auth_entry, memo| memo[auth_entry[:name]] = auth_entry[:definition] end end if (shared_components = Rage::OpenAPI.__shared_components["components"]) shared_components.each do |definition_type, definitions| (@spec["components"][definition_type] ||= {}).merge!(definitions || {}) end end @spec["tags"] = @used_tags.sort.map { |tag| { "name" => tag } } @spec end |