Class: Rage::OpenAPI::Parser

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

Instance Method Summary collapse

Instance Method Details

#parse_dangling_comments(node, comments) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
# File 'lib/rage/openapi/parser.rb', line 4

def parse_dangling_comments(node, comments)
  i = 0

  while i < comments.length
    children = nil
    expression = comments[i].slice.delete_prefix("#").strip

    if expression =~ /@deprecated\b/
      if node.deprecated
        Rage::OpenAPI.__log_warn "duplicate @deprecated tag detected at #{location_msg(comments[i])}"
      else
        node.deprecated = true
      end
      children = find_children(comments[i + 1..])

    elsif expression =~ /@private\b/
      if node.private
        Rage::OpenAPI.__log_warn "duplicate @private tag detected at #{location_msg(comments[i])}"
      else
        node.private = true
      end
      children = find_children(comments[i + 1..])

    elsif expression =~ /@version\s/
      if node.root.version
        Rage::OpenAPI.__log_warn "duplicate @version tag detected at #{location_msg(comments[i])}"
      else
        node.root.version = expression[9..]
      end

    elsif expression =~ /@title\s/
      if node.root.title
        Rage::OpenAPI.__log_warn "duplicate @title tag detected at #{location_msg(comments[i])}"
      else
        node.root.title = expression[7..]
      end

    elsif expression =~ /@auth\s/
      method, name, tail_name = expression[6..].split(" ", 3)
      children = find_children(comments[i + 1..])

      if tail_name
        Rage::OpenAPI.__log_warn "incorrect `@auth` name detected at #{location_msg(comments[i])}; security scheme name cannot contain spaces"
      end

      auth_entry = {
        method:,
        name: name || method,
        definition: children.any? ? YAML.safe_load(children.join("\n")) : { "type" => "http", "scheme" => "bearer" }
      }

      if !node.controller.__before_action_exists?(method.to_sym)
        Rage::OpenAPI.__log_warn "referenced before action `#{method}` is not defined in #{node.controller} at #{location_msg(comments[i])}; ensure a corresponding `before_action` call exists"
      elsif node.auth.include?(auth_entry) || node.root.parent_nodes.any? { |parent_node| parent_node.auth.include?(auth_entry) }
        Rage::OpenAPI.__log_warn "duplicate @auth tag detected at #{location_msg(comments[i])}"
      else
        node.auth << auth_entry
      end
    end

    if children&.any?
      i += children.length + 1
    else
      i += 1
    end
  end
end

#parse_method_comments(node, comments) ⇒ Object



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rage/openapi/parser.rb', line 72

def parse_method_comments(node, comments)
  i = 0

  while i < comments.length
    children = nil
    expression = comments[i].slice.delete_prefix("#").strip

    if !expression.start_with?("@")
      if node.summary
        Rage::OpenAPI.__log_warn "invalid summary entry detected at #{location_msg(comments[i])}; summary should only be one line"
      else
        node.summary = expression
      end

    elsif expression =~ /@deprecated\b/
      if node.parents.any?(&:deprecated)
        Rage::OpenAPI.__log_warn "duplicate `@deprecated` tag detected at #{location_msg(comments[i])}; tag already exists in a parent class"
      else
        node.deprecated = true
      end
      children = find_children(comments[i + 1..])

    elsif expression =~ /@private\b/
      if node.parents.any?(&:private)
        Rage::OpenAPI.__log_warn "duplicate `@private` tag detected at #{location_msg(comments[i])}; tag already exists in a parent class"
      else
        node.private = true
      end
      children = find_children(comments[i + 1..])

    elsif expression =~ /@description\s/
      children = find_children(comments[i + 1..])
      node.description = [expression[13..]] + children

    elsif expression =~ /@response\s/
      response = expression[10..].strip
      status, response_data = if response =~ /^\d{3}$/
        [response, nil]
      elsif response =~ /^\d{3}/
        response.split(" ", 2)
      else
        ["200", response]
      end

      if node.responses.has_key?(status)
        Rage::OpenAPI.__log_warn "duplicate `@response` tag detected at #{location_msg(comments[i])}"
      elsif response_data.nil?
        node.responses[status] = nil
      else
        parsed = Rage::OpenAPI::Parsers::Response.parse(
          response_data,
          namespace: Rage::OpenAPI.__module_parent(node.controller)
        )

        if parsed
          node.responses[status] = parsed
        else
          Rage::OpenAPI.__log_warn "unrecognized `@response` tag detected at #{location_msg(comments[i])}"
        end
      end

    elsif expression =~ /@request\s/
      request = expression[9..]
      if node.request
        Rage::OpenAPI.__log_warn "duplicate `@request` tag detected at #{location_msg(comments[i])}"
      else
        parsed = Rage::OpenAPI::Parsers::Request.parse(
          request,
          namespace: Rage::OpenAPI.__module_parent(node.controller)
        )

        if parsed
          node.request = parsed
        else
          Rage::OpenAPI.__log_warn "unrecognized `@request` tag detected at #{location_msg(comments[i])}"
        end
      end

    elsif expression =~ /@internal\b/
      # no-op
      children = find_children(comments[i + 1..])

    else
      Rage::OpenAPI.__log_warn "unrecognized `#{expression.split(" ")[0]}` tag detected at #{location_msg(comments[i])}"
    end

    if children&.any?
      i += children.length + 1
    else
      i += 1
    end
  end
end