From 04e81814861d5fff9c5020cd199ba5549b5c018b Mon Sep 17 00:00:00 2001 From: hahwul Date: Sun, 20 Aug 2023 01:31:32 +0900 Subject: [PATCH] (#25) Refactor JSON handling and error handling for method_obj in oas3 --- src/analyzer/analyzers/analyzer_oas3.cr | 88 +++++++++++++------------ 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/src/analyzer/analyzers/analyzer_oas3.cr b/src/analyzer/analyzers/analyzer_oas3.cr index e4160064..f65e8ed7 100644 --- a/src/analyzer/analyzers/analyzer_oas3.cr +++ b/src/analyzer/analyzers/analyzer_oas3.cr @@ -33,30 +33,32 @@ class AnalyzerOAS3 < Analyzer path_obj.as_h.each do |method, method_obj| params = [] of Param - 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 + 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 - 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 + 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 @@ -86,30 +88,32 @@ class AnalyzerOAS3 < Analyzer path_obj.as_h.each do |method, method_obj| params = [] of Param - 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 + 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 - 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 + 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