-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from hahwul/main
Sync
- Loading branch information
Showing
15 changed files
with
327 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
Metrics/CyclomaticComplexity: | ||
Description: Disallows methods with a cyclomatic complexity higher than `MaxComplexity` | ||
MaxComplexity: 25 | ||
MaxComplexity: 30 | ||
Enabled: true | ||
Severity: Warning |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: noir | ||
version: 0.3.0 | ||
version: 0.4.0 | ||
|
||
authors: | ||
- hahwul <[email protected]> | ||
|
4 changes: 2 additions & 2 deletions
4
spec/detector/detect_swagger_spec.cr → spec/detector/detect_oas2_spec.cr
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,40 @@ | ||
require "../../src/detector/detectors/*" | ||
require "../../src/models/code_locator" | ||
|
||
describe "Detect OAS 3.0 Docs" do | ||
options = default_options() | ||
instance = DetectorOas3.new options | ||
|
||
it "json format" do | ||
content = <<-EOS | ||
{ | ||
"openapi": "3.0.0", | ||
"info": "test" | ||
} | ||
EOS | ||
|
||
instance.detect("docs.json", content).should eq(true) | ||
end | ||
it "yaml format" do | ||
content = <<-EOS | ||
openapi: 3.0.0 | ||
info: | ||
version: 1.0.0 | ||
EOS | ||
|
||
instance.detect("docs.yml", content).should eq(true) | ||
end | ||
|
||
it "code_locator" do | ||
content = <<-EOS | ||
{ | ||
"openapi": "3.0.0", | ||
"info": "test" | ||
} | ||
EOS | ||
|
||
instance.detect("docs.json", content) | ||
locator = CodeLocator.instance | ||
locator.get("oas3-json").should eq("docs.json") | ||
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
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,143 @@ | ||
require "../../models/analyzer" | ||
|
||
class AnalyzerOAS3 < Analyzer | ||
def get_base_path(servers) | ||
base_path = @url | ||
servers.as_a.each do |server_obj| | ||
if server_obj["url"].to_s.starts_with?("http") | ||
user_uri = URI.parse(@url) | ||
source_uri = URI.parse(server_obj["url"].to_s) | ||
if user_uri.host == source_uri.host | ||
base_path = @url + source_uri.path | ||
break | ||
end | ||
end | ||
end | ||
|
||
base_path | ||
end | ||
|
||
def analyze | ||
locator = CodeLocator.instance | ||
oas3_json = locator.get("oas3-json") | ||
oas3_yaml = locator.get("oas3-yaml") | ||
|
||
if !oas3_json.nil? | ||
if File.exists?(oas3_json) | ||
content = File.read(oas3_json, encoding: "utf-8", invalid: :skip) | ||
json_obj = JSON.parse(content) | ||
|
||
base_path = get_base_path json_obj["servers"] | ||
|
||
json_obj["paths"].as_h.each do |path, path_obj| | ||
path_obj.as_h.each do |method, method_obj| | ||
params = [] of Param | ||
|
||
if method_obj.is_a?(JSON::Any) && method_obj.is_a?(Hash(String, JSON::Any)) | ||
if method_obj.as_h.has_key?("parameters") | ||
method_obj["parameters"].as_a.each do |param_obj| | ||
param_name = param_obj["name"].to_s | ||
if param_obj["in"] == "query" | ||
param = Param.new(param_name, "", "query") | ||
params << param | ||
elsif param_obj["in"] == "header" | ||
param = Param.new(param_name, "", "header") | ||
params << param | ||
end | ||
end | ||
end | ||
|
||
if method_obj.as_h.has_key?("requestBody") | ||
method_obj["requestBody"]["content"].as_h.each do |content_type, content_obj| | ||
if content_type == "application/json" | ||
content_obj["schema"]["properties"].as_h.each do |param_name, _| | ||
param = Param.new(param_name, "", "json") | ||
params << param | ||
end | ||
elsif content_type == "application/x-www-form-urlencoded" | ||
content_obj["schema"]["properties"].as_h.each do |param_name, _| | ||
param = Param.new(param_name, "", "form") | ||
params << param | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
if params.size > 0 && params.size > 0 | ||
@result << Endpoint.new(base_path + path, method.upcase, params + params) | ||
elsif params.size > 0 | ||
@result << Endpoint.new(base_path + path, method.upcase, params) | ||
elsif params.size > 0 | ||
@result << Endpoint.new(base_path + path, method.upcase, params) | ||
else | ||
@result << Endpoint.new(base_path + path, method.upcase) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
if !oas3_yaml.nil? | ||
if File.exists?(oas3_yaml) | ||
content = File.read(oas3_yaml, encoding: "utf-8", invalid: :skip) | ||
yaml_obj = YAML.parse(content) | ||
base_path = get_base_path yaml_obj["servers"] | ||
|
||
yaml_obj["paths"].as_h.each do |path, path_obj| | ||
path_obj.as_h.each do |method, method_obj| | ||
params = [] of Param | ||
|
||
if method_obj.is_a?(YAML::Any) && method_obj.is_a?(Hash(String, YAML::Any)) | ||
if method_obj.as_h.has_key?("parameters") | ||
method_obj["parameters"].as_a.each do |param_obj| | ||
param_name = param_obj["name"].to_s | ||
if param_obj["in"] == "query" | ||
param = Param.new(param_name, "", "query") | ||
params << param | ||
elsif param_obj["in"] == "header" | ||
param = Param.new(param_name, "", "header") | ||
params << param | ||
end | ||
end | ||
end | ||
|
||
if method_obj.as_h.has_key?("requestBody") | ||
method_obj["requestBody"]["content"].as_h.each do |content_type, content_obj| | ||
if content_type == "application/json" | ||
content_obj["schema"]["properties"].as_h.each do |param_name, _| | ||
param = Param.new(param_name.to_s, "", "json") | ||
params << param | ||
end | ||
elsif content_type == "application/x-www-form-urlencoded" | ||
content_obj["schema"]["properties"].as_h.each do |param_name, _| | ||
param = Param.new(param_name.to_s, "", "form") | ||
params << param | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
if params.size > 0 && params.size > 0 | ||
@result << Endpoint.new(base_path + path.to_s, method.to_s.upcase, params + params) | ||
elsif params.size > 0 | ||
@result << Endpoint.new(base_path + path.to_s, method.to_s.upcase, params) | ||
elsif params.size > 0 | ||
@result << Endpoint.new(base_path + path.to_s, method.to_s.upcase, params) | ||
else | ||
@result << Endpoint.new(base_path + path.to_s, method.to_s.upcase) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
@result | ||
end | ||
end | ||
|
||
def analyzer_oas3(options : Hash(Symbol, String)) | ||
instance = AnalyzerOAS3.new(options) | ||
instance.analyze | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require "../../models/detector" | ||
require "../../utils/json" | ||
require "../../utils/yaml" | ||
require "../../models/code_locator" | ||
|
||
class DetectorOas2 < Detector | ||
def detect(filename : String, file_contents : String) : Bool | ||
check = false | ||
if valid_json? file_contents | ||
data = JSON.parse(file_contents) | ||
begin | ||
if data["swagger"].as_s.includes? "2." | ||
check = true | ||
locator = CodeLocator.instance | ||
locator.set("swagger-json", filename) | ||
end | ||
rescue | ||
end | ||
elsif valid_yaml? file_contents | ||
data = YAML.parse(file_contents) | ||
begin | ||
if data["swagger"].as_s.includes? "2." | ||
check = true | ||
locator = CodeLocator.instance | ||
locator.set("swagger-yaml", filename) | ||
end | ||
rescue | ||
end | ||
end | ||
|
||
check | ||
end | ||
|
||
def set_name | ||
@name = "oas2" | ||
end | ||
end |
Oops, something went wrong.