Skip to content

Commit

Permalink
if no data example by default depends on type
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Jun 12, 2020
1 parent 67783a3 commit e3e7a45
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
27 changes: 14 additions & 13 deletions lib/open_api_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,17 @@ def self.from(swagger_file, create_method_name: :operation_id, include_responses
end
end

if dpv.key?(:type) and dpv[:type].downcase == "string"
valv = '"' + valv + '"'
#todo: consider check default and insert it
if dpv.key?(:type)
params_data << get_examples({dpk => dpv}, :only_value, true).join
params_data[-1].chop!.chop! if params_data[-1].to_s[-2..-1]==', '
params_data.pop if params_data[-1].match?(/^\s*$/im)
else
#todo: consider check default and insert it
if valv.to_s == ""
valv = '"' + valv + '"'
end
params_data << "#{dpk}: #{valv}"
end
params_data << "#{dpk}: #{valv}"
}

if params_data.size > 0
Expand Down Expand Up @@ -730,14 +732,14 @@ class << self
else
#todo: differ between response examples and data examples
if type == :only_value
example << get_response_examples({schema: val}).join("\n")
example << get_response_examples({schema: val}, remove_readonly).join("\n")
else
example << " #{prop.to_sym}: " + get_response_examples({schema: val}).join("\n") + ", "
example << " #{prop.to_sym}: " + get_response_examples({schema: val}, remove_readonly).join("\n") + ", "
end
end
when "object"
#todo: differ between response examples and data examples
res_ex = get_response_examples({schema: val})
res_ex = get_response_examples({schema: val}, remove_readonly)
if res_ex.size == 0
res_ex = "{ }"
else
Expand All @@ -755,7 +757,7 @@ class << self
end

# Retrieve the response examples from the hash
private def get_response_examples(v)
private def get_response_examples(v, remove_readonly = false)
# 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
Expand All @@ -765,7 +767,6 @@ class << self
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 Down Expand Up @@ -817,9 +818,9 @@ class << self
end
elsif v.key?(:schema) && v[:schema].is_a?(Hash) &&
(v[:schema].key?(:properties) ||
(v[:schema].key?(:items) && v[:schema][:items].key?(:properties)) ||
(v[:schema].key?(:items) && v[:schema][:items].key?(:allOf)) ||
v[:schema].key?(:allOf))
(v[:schema].key?(:items) && v[:schema][:items].key?(:properties)) ||
(v[:schema].key?(:items) && v[:schema][:items].key?(:allOf)) ||
v[:schema].key?(:allOf))
properties = {}
if v[:schema].key?(:properties)
properties = v[:schema][:properties]
Expand All @@ -837,7 +838,7 @@ class << self
response_example << "["
end

response_example += get_examples(properties) unless properties.empty?
response_example += get_examples(properties, :key_value, remove_readonly) unless properties.empty?

unless response_example.empty?
if v[:schema].key?(:properties) || v[:schema].key?(:allOf)
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.10.4'
s.version = '0.10.5'
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
3 changes: 2 additions & 1 deletion spec/open_api_import/open_api_import_data_examples_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'open_api_import'

#todo: add test for data_examples to include values according to type in case no examples supplied, fex: doo: 0 instead of doo: "" if type is intenger
RSpec.describe OpenApiImport do

describe '#from' do
Expand All @@ -14,7 +15,7 @@
req = Swagger::SwaggerPetstore::V1_0_0::Root.add_pet()
expect(req.key?(:data_examples)).to eq true
expect(req[:data_examples].class).to eq Array
expect(req[:data_examples]).to eq ([{name: "", tag: ""}])
expect(req[:data_examples]).to eq ([{name: "string", tag: "string"}])
end
end
end

0 comments on commit e3e7a45

Please sign in to comment.