diff --git a/README.md b/README.md index fd226d1..e23067c 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,6 @@ Import a Swagger or Open API file and create a Ruby Request Hash file including The Request Hash will include also the pattern (regular expressions) of the fields, parameters, default values... -On this first preliminary version we only fully support Open API v2. Open API v3 is not fully supported yet. - The output of this gem will be following the specification of Request Hashes: https://github.com/MarioRuiz/Request-Hash The Request Hashes generated will be able to be used with any Ruby Http Client and it is adapted even better with nice_http gem: https://github.com/MarioRuiz/nice_http diff --git a/lib/open_api_import.rb b/lib/open_api_import.rb index 1ac39d7..67d1680 100644 --- a/lib/open_api_import.rb +++ b/lib/open_api_import.rb @@ -60,10 +60,6 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses end if raw[:swagger].to_f < 2.0 raise "Unsupported Swagger version. Only versions >= 2.0 are valid." - elsif raw[:swagger].to_f >= 3.0 - message = "Take in consideration Open API #{raw[:swagger]} is not fully supported for the moment while we are in preliminary version of open_api_import." - warn message - @logger.warn message end base_host = "" @@ -228,10 +224,26 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses end end + # todo: for open api 3.0 add the new Link feature: https://swagger.io/docs/specification/links/ + # todo: for open api 3.0 is not getting the required params in all cases + + # for the case open api 3 with cont.requestBody.content.'applicatin/json'.schema + # example: petstore-expanded.yaml operationId=addPet + if cont.key?(:requestBody) and cont[:requestBody].key?(:content) and + cont[:requestBody][:content].key?(:'application/json') and cont[:requestBody][:content][:'application/json'].key?(:schema) + cont[:parameters] = [] unless cont.key?(:parameters) + cont[:parameters] << {in: 'body', schema: cont[:requestBody][:content][:'application/json'][:schema] } + end - # todo: for open api 3.0 is not getting the required params if cont.key?(:parameters) && cont[:parameters].is_a?(Array) cont[:parameters].each do |p| + if p.keys.include?(:schema) and p[:schema].include?(:type) + type = p[:schema][:type] + elsif p.keys.include?(:type) + type = p[:type] + else + type = "" + end if p[:in] == "path" if create_method_name == :operationId param_name = p[:name] @@ -242,19 +254,13 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses end params_path << param_name #params_required << param_name if p[:required].to_s=="true" - - if p.keys.include?(:description) - description_parameters << "# #{p[:name]}: (#{p[:type]}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}" - end + description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}" elsif p[:in] == "query" params_query << p[:name] params_required << p[:name] if p[:required].to_s=="true" - if p.keys.include?(:description) - description_parameters << "# #{p[:name]}: (#{p[:type]}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}" - end + description_parameters << "# #{p[:name]}: (#{type}) #{"(required)" if p[:required].to_s=="true"} #{p[:description]}" elsif p[:in] == "body" if p.keys.include?(:schema) - #jal if p[:schema].key?(:oneOf) bodies = p[:schema][:oneOf] elsif p[:schema].key?(:anyOf) @@ -556,7 +562,14 @@ class << self private def get_response_examples(v) # TODO: take in consideration the case allOf, oneOf... schema.items.allOf[0].properties schema.items.allOf[1].properties # example on https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/yaml/petstore-expanded.yaml + v=v.dup response_example = Array.new() + # for open api 3.0 with responses schema inside content + if v.key?(:content) && v[:content].is_a?(Hash) && v[:content].key?(:'application/json') && + v[:content][:'application/json'].key?(:schema) + v=v[:content][:'application/json'].dup + end + if v.key?(:examples) && v[:examples].is_a?(Hash) && v[:examples].key?(:'application/json') if v[:examples][:'application/json'].is_a?(String) response_example << v[:examples][:'application/json'] @@ -575,6 +588,36 @@ class << self end response_example << "]" end + # for open api 3.0. examples on reponses, for example: api-with-examples.yaml + elsif v.key?(:content) && v[:content].is_a?(Hash) && v[:content].key?(:'application/json') && + v[:content][:'application/json'].key?(:examples) + v[:content][:'application/json'][:examples].each do |tk, tv| + #todo: for the moment we only take in consideration the first example of response. + # we need to decide how to manage to do it correctly + if tv.key?(:value) + tresp = tv[:value] + else + tresp = "" + end + if tresp.is_a?(String) + response_example << tresp + elsif tresp.is_a?(Hash) + exs = tresp.to_s + exs.gsub!(/:(\w+)=>/, "\n\\1: ") + response_example << exs + elsif tresp.is_a?(Array) + response_example << "[" + tresp.each do |ex| + exs = ex.to_s + if ex.is_a?(Hash) + exs.gsub!(/:(\w+)=>/, "\n\\1: ") + end + response_example << (exs + ", ") + end + response_example << "]" + end + break #only the first one it is considered + end elsif v.key?(:schema) && v[:schema].is_a?(Hash) && (v[:schema].key?(:properties) || (v[:schema].key?(:items) && v[:schema][:items].key?(:properties)) || diff --git a/open_api_import.gemspec b/open_api_import.gemspec index 6a874a0..65462a1 100644 --- a/open_api_import.gemspec +++ b/open_api_import.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'open_api_import' - s.version = '0.4.1' + s.version = '0.5.0' s.summary = "OpenApiImport -- Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses with all the examples. The file can be in JSON or YAML" s.description = "OpenApiImport -- Import a Swagger or Open API file and create a Ruby Request Hash file including all requests and responses with all the examples. The file can be in JSON or YAML" s.authors = ["Mario Ruiz"]