Skip to content

Commit

Permalink
first version supporting open api 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario.RuizSanchez committed Feb 13, 2019
1 parent d544a1a commit 5fa73a5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 56 additions & 13 deletions lib/open_api_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down Expand Up @@ -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']
Expand All @@ -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)) ||
Expand Down
2 changes: 1 addition & 1 deletion open_api_import.gemspec
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down

0 comments on commit 5fa73a5

Please sign in to comment.