Class: Rage::OpenAPI::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/openapi/converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ Converter

Returns a new instance of Converter.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rage/openapi/converter.rb', line 4

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

#runObject



18
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
# File 'lib/rage/openapi/converter.rb', line 18

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_parameters = []
    path = node.http_path.gsub(/:(\w+)/) do
      path_parameters << $1
      "{#{$1}}"
    end

    unless memo.key?(path)
      memo[path] = {}
      path_parameters.each do |parameter|
        (memo[path]["parameters"] ||= []) << {
          "in" => "path",
          "name" => parameter,
          "required" => true,
          "schema" => { "type" => parameter.end_with?("id") ? "integer" : "string" }
        }
      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" => build_tags(node)
    }

    memo[path][method]["responses"] = if node.responses.any?
      node.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"] = { "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