-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
59 changed files
with
5,407 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,5 @@ group :test do | |
gem "rbnacl" | ||
gem "domain_name" | ||
gem "websocket-client-simple" | ||
gem "prism" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# frozen_string_literal: true | ||
|
||
## | ||
# Build OpenAPI specification for the app. Consists of three steps: | ||
# | ||
# * `Rage::OpenAPI::Builder` - build a tree of action nodes; | ||
# * `Rage::OpenAPI::Parser` - parse OpenAPI tags and save the result into the nodes; | ||
# * `Rage::OpenAPI::Converter` - convert the tree into an OpenAPI spec; | ||
# | ||
class Rage::OpenAPI::Builder | ||
class ParsingError < StandardError | ||
end | ||
|
||
def initialize(namespace: nil) | ||
@namespace = namespace.to_s if namespace | ||
|
||
@collectors_cache = {} | ||
@nodes = Rage::OpenAPI::Nodes::Root.new | ||
@routes = Rage.__router.routes.group_by { |route| route[:meta][:controller_class] } | ||
end | ||
|
||
def run | ||
parser = Rage::OpenAPI::Parser.new | ||
|
||
@routes.each do |controller, routes| | ||
next if skip_controller?(controller) | ||
|
||
parent_nodes = fetch_ancestors(controller).map do |klass| | ||
@nodes.new_parent_node(klass) { |node| parser.parse_dangling_comments(node, parse_class(klass).dangling_comments) } | ||
end | ||
|
||
routes.each do |route| | ||
action = route[:meta][:action] | ||
|
||
method_comments = fetch_ancestors(controller).filter_map { |klass| | ||
parse_class(klass).method_comments(action) | ||
}.first | ||
|
||
method_node = @nodes.new_method_node(controller, action, parent_nodes) | ||
method_node.http_method, method_node.http_path = route[:method], route[:path] | ||
|
||
parser.parse_method_comments(method_node, method_comments) | ||
end | ||
|
||
rescue ParsingError | ||
Rage::OpenAPI.__log_warn "skipping #{controller.name} because of parsing error" | ||
next | ||
end | ||
|
||
Rage::OpenAPI::Converter.new(@nodes).run | ||
end | ||
|
||
private | ||
|
||
def skip_controller?(controller) | ||
should_skip_controller = controller.nil? || !controller.ancestors.include?(RageController::API) | ||
should_skip_controller ||= !controller.name.start_with?(@namespace) if @namespace | ||
|
||
should_skip_controller | ||
end | ||
|
||
def fetch_ancestors(controller) | ||
controller.ancestors.take_while { |klass| klass != RageController::API } | ||
end | ||
|
||
def parse_class(klass) | ||
@collectors_cache[klass] ||= begin | ||
source_path, _ = Object.const_source_location(klass.name) | ||
ast = Prism.parse_file(source_path) | ||
|
||
raise ParsingError if ast.errors.any? | ||
|
||
# save the "comment => file" association | ||
ast.comments.each do |comment| | ||
comment.location.define_singleton_method(:__source_path) { source_path } | ||
end | ||
|
||
collector = Rage::OpenAPI::Collector.new(ast.comments) | ||
ast.value.accept(collector) | ||
|
||
collector | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
## | ||
# Collect all global comments or comments attached to methods in a class. | ||
# At this point we don't care whether these are Rage OpenAPI comments or not. | ||
# | ||
class Rage::OpenAPI::Collector < Prism::Visitor | ||
def initialize(comments) | ||
@comments = comments.dup | ||
@method_comments = {} | ||
end | ||
|
||
def dangling_comments | ||
@comments | ||
end | ||
|
||
def method_comments(method_name) | ||
@method_comments[method_name.to_s] | ||
end | ||
|
||
def visit_def_node(node) | ||
method_comments = [] | ||
start_line = node.location.start_line - 1 | ||
|
||
loop do | ||
comment_i = @comments.find_index { |comment| comment.location.start_line == start_line } | ||
if comment_i | ||
comment = @comments.delete_at(comment_i) | ||
method_comments << comment | ||
start_line -= 1 | ||
end | ||
|
||
break unless comment | ||
end | ||
|
||
@method_comments[node.name.to_s] = method_comments.reverse | ||
|
||
# reject comments inside methods | ||
@comments.reject! do |comment| | ||
comment.location.start_line >= node.location.start_line && comment.location.start_line <= node.location.end_line | ||
end | ||
end | ||
end |
Oops, something went wrong.