Skip to content

Commit

Permalink
supported multidimensional data required
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario.RuizSanchez committed May 3, 2019
1 parent 9c329ea commit f02d683
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
78 changes: 66 additions & 12 deletions lib/open_api_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses

bodies.each do |body|
if body.keys.include?(:required) and body[:required].size > 0
output << "# required data: #{body[:required].join(", ")}"
data_required += body[:required]
data_required += get_required_data(body)
output << "# required data: #{data_required.inspect}"
end

if body.keys.include?(:properties) and body[:properties].size > 0
Expand Down Expand Up @@ -420,7 +420,7 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
unless data_required.empty?
output << "data_required: ["
output << ":#{data_required.uniq.join(", :")}"
output << ":#{data_required.uniq.join(", :")}".gsub(':{','{')
output << "],"
end
unless data_read_only.empty?
Expand All @@ -443,17 +443,16 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
unless data_examples.empty?
unless data_required.empty?
reqdata = []
data_examples[0].each do |edata|
data_required.each do |rdata|
if edata.scan(/^#{rdata}:/).size>0 or edata.scan(/:/).size==0
reqdata << edata
end
begin
data_ex = eval("{#{data_examples[0].join(", ")}}")
rescue
data_ex = {}
end
end
reqdata = filter(data_ex, data_required)
unless reqdata.empty?
output << "data: {"
output << reqdata.join(", \n")
output << "},"
phsd = pretty_hash_symbolized(reqdata)
phsd[0]="data: {"
output += phsd
end
end
unless data_read_only.empty? or !data_required.empty?
Expand Down Expand Up @@ -769,5 +768,60 @@ class << self
end
return data_examples_all_of, bodies
end
# Get required data
private def get_required_data(body)
data_required = []
if body.keys.include?(:required) and body[:required].size > 0
body[:required].each do |r|
data_required << r.to_sym
end
end
data_required.each do |key|
if body.key?(:properties) and body[:properties][key].is_a?(Hash) and
body[:properties][key].key?(:required) and body[:properties][key][:required].size>0
dr = get_required_data(body[:properties][key])
dr.each do |k|
data_required.push({key => k})
end
end
end
return data_required
end
#filter hash
private def filter(hash, keys)
result = {}
keys = [keys] unless keys.is_a?(Array)
keys.each do |k|
if k.is_a?(Symbol) and hash.key?(k)
if hash[k].is_a?(Hash)
result[k] = {}
else
result[k] = hash[k]
end
elsif k.is_a?(Hash) and hash.key?(k.keys[0])
result[k.keys[0]][k.values[0]] = filter(hash[k.keys[0]], k.values[0]).values[0]
end
end
return result
end
#gen pretty hash symbolized
private def pretty_hash_symbolized(hash)
output = []
output << "{"
hash.each do |kr,kv|
if kv.kind_of?(Hash)
restv = pretty_hash_symbolized(kv)
restv[0] = "#{kr}: {"
output += restv
else
output << "#{kr}: #{kv.inspect}, "
end
end
output << "},"
return output
end
end
end
4 changes: 2 additions & 2 deletions 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.7.2'
s.version = '0.7.3'
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 All @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.license = 'MIT'
s.add_runtime_dependency 'oas_parser', '~> 0.16', '>= 0.16.0'
s.add_runtime_dependency 'rufo', '~> 0.4', '>= 0.4.1'
s.add_runtime_dependency 'nice_hash', '~> 1.9', '>= 1.9.1'
s.add_runtime_dependency 'nice_hash', '~> 1.12', '>= 1.12.4'
s.add_development_dependency 'rspec', '~> 3.8', '>= 3.8.0'
s.add_development_dependency 'coveralls', '~> 0.8', '>= 0.8.22'
s.test_files = s.files.grep(%r{^(test|spec|features)/})
Expand Down

0 comments on commit f02d683

Please sign in to comment.