Skip to content

Commit

Permalink
added data patterns for all fields on data or responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario.RuizSanchez committed Jun 21, 2019
1 parent edec312 commit 69054e9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
66 changes: 56 additions & 10 deletions lib/open_api_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,10 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
if include_responses && cont.key?(:responses) && cont[:responses].is_a?(Hash)
cont[:responses].each do |k, v|
response_example = []

response_example = get_response_examples(v)
data_pattern += get_patterns(k, v[:schema]) if v.key?(:schema)
v[:description] = v[:description].to_s.gsub("'", %q(\\\'))


if !response_example.empty?
responses << "'#{k}': { "
responses << "message: '#{v[:description]}', "
Expand Down Expand Up @@ -325,14 +324,9 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
if dpv.keys.include?(:description)
description_parameters << "# #{dpk}: (#{dpv[:type]}) #{dpv[:description]}"
end
if dpv.keys.include?(:pattern)
#todo: control better the cases with back slashes
if dpv[:pattern].include?('\\/')
data_pattern << "#{dpk}: //"
else
data_pattern << "#{dpk}: /#{dpv[:pattern].to_s.gsub("\\/", "\/")}/"
end
end

data_pattern += get_patterns(dpk,dpv)

if dpv.keys.include?(:readOnly) and dpv[:readOnly] == true
data_read_only << dpk
end
Expand Down Expand Up @@ -795,6 +789,58 @@ class << self
return data_required
end

# Get patterns
private def get_patterns(dpk, dpv)
data_pattern = []

if dpv.keys.include?(:pattern)
#todo: control better the cases with back slashes
if dpv[:pattern].include?('\\/')
data_pattern << "#{dpk}: //"
else
data_pattern << "#{dpk}: /#{dpv[:pattern].to_s.gsub("\\/", "\/")}/"
end
elsif dpv.key?(:minLength) and dpv.key?(:maxLength)
data_pattern << "#{dpk}: :'#{dpv[:minLength]}-#{dpv[:maxLength]}:TN'"
elsif dpv.key?(:minLength) and !dpv.key?(:maxLength)
data_pattern << "#{dpk}: :'#{dpv[:minLength]}:TN'"
elsif !dpv.key?(:minLength) and dpv.key?(:maxLength)
data_pattern << "#{dpk}: :'0-#{dpv[:maxLength]}:TN'"
elsif dpv.key?(:minimum) and dpv.key?(:maximum) and dpv[:type]=='string'
data_pattern << "#{dpk}: :'#{dpv[:minimum]}-#{dpv[:maximum]}:TN'"
elsif dpv.key?(:minimum) and dpv.key?(:maximum)
data_pattern << "#{dpk}: #{dpv[:minimum]}..#{dpv[:maximum]}"
elsif dpv.key?(:minimum) and !dpv.key?(:maximum)
if RUBY_VERSION >= '2.6.0'
data_pattern << "#{dpk}: #{dpv[:minimum]}.."
else
data_pattern << "##{dpk}: #{dpv[:minimum]}.. # INFINITE only working on ruby>=2.6.0"
end
elsif !dpv.key?(:minimum) and dpv.key?(:maximum)
data_pattern << "#{dpk}: 0..#{dpv[:maximum]}"
elsif dpv.key?(:enum)
data_pattern << "#{dpk}: :'#{dpv[:enum].join('|')}'"
elsif dpv[:format] == 'date-time'
data_pattern << "#{dpk}: DateTime"
elsif dpv[:type] == 'boolean'
data_pattern << "#{dpk}: Boolean"
elsif dpv[:type] == 'array' and dpv.key?(:items) and dpv[:items].is_a?(Hash) and dpv[:items].key?(:enum) and dpv[:items][:enum].is_a?(Array)
#{:title=>"Balala", :type=>"array", :items=>{:type=>"string", :enum=>["uno","dos"], :example=>"uno"}}
data_pattern << "#{dpk}: [:'#{dpv[:items][:enum].join('|')}']"
elsif dpv[:type] == 'array' and dpv.key?(:items) and dpv[:items].is_a?(Hash) and !dpv[:items].key?(:enum) and dpv[:items].key?(:properties)
#{:title=>"Balala", :type=>"array", :items=>{title: 'xxxx, properties: {server: {enum:['ibm','msa','pytan']}}}
dpv[:items][:properties].each do |dpkk,dpvv|
data_pattern += get_patterns(dpkk,dpvv)
end
elsif dpv[:type] == 'object' and dpv.key?(:properties)
dpv[:properties].each do |dpkk,dpvv|
data_pattern += get_patterns(dpkk,dpvv)
end
end
return data_pattern

end

#filter hash
private def filter(hash, keys)
result = {}
Expand Down
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.5'
s.version = '0.8.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 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.12', '>= 1.12.8'
s.add_runtime_dependency 'nice_hash', '~> 1.12', '>= 1.12.12'
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 69054e9

Please sign in to comment.