Support optional body parameter

This commit is contained in:
xhh
2015-04-16 18:20:36 +08:00
parent 61bf7f49cc
commit de21088619
7 changed files with 106 additions and 97 deletions

View File

@@ -42,22 +42,23 @@ class {{classname}}
# http body (model) # http body (model)
post_body = nil{{#bodyParam}} post_body = nil{{#bodyParam}}
if body != nil _body_param = {{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}
if body.is_a?(Array) if _body_param != nil
array = Array.new if _body_param.is_a?(Array)
body.each do |item| _array = Array.new
if item.respond_to?("to_body".to_sym) _body_param.each do |item|
array.push item.to_body if item.respond_to?(:to_body)
_array.push item.to_body
else else
array.push item _array.push item
end end
end end
post_body = array post_body = _array
else else
if body.respond_to?("to_body".to_sym) if _body_param.respond_to?(:to_body)
post_body = body.to_body post_body = _body_param.to_body
else else
post_body = body post_body = _body_param
end end
end end
end{{/bodyParam}} end{{/bodyParam}}

View File

@@ -32,22 +32,23 @@ class PetApi
# http body (model) # http body (model)
post_body = nil post_body = nil
if body != nil _body_param = opts[:'body']
if body.is_a?(Array) if _body_param != nil
array = Array.new if _body_param.is_a?(Array)
body.each do |item| _array = Array.new
if item.respond_to?("to_body".to_sym) _body_param.each do |item|
array.push item.to_body if item.respond_to?(:to_body)
_array.push item.to_body
else else
array.push item _array.push item
end end
end end
post_body = array post_body = _array
else else
if body.respond_to?("to_body".to_sym) if _body_param.respond_to?(:to_body)
post_body = body.to_body post_body = _body_param.to_body
else else
post_body = body post_body = _body_param
end end
end end
end end
@@ -83,22 +84,23 @@ class PetApi
# http body (model) # http body (model)
post_body = nil post_body = nil
if body != nil _body_param = opts[:'body']
if body.is_a?(Array) if _body_param != nil
array = Array.new if _body_param.is_a?(Array)
body.each do |item| _array = Array.new
if item.respond_to?("to_body".to_sym) _body_param.each do |item|
array.push item.to_body if item.respond_to?(:to_body)
_array.push item.to_body
else else
array.push item _array.push item
end end
end end
post_body = array post_body = _array
else else
if body.respond_to?("to_body".to_sym) if _body_param.respond_to?(:to_body)
post_body = body.to_body post_body = _body_param.to_body
else else
post_body = body post_body = _body_param
end end
end end
end end

View File

@@ -64,22 +64,23 @@ class StoreApi
# http body (model) # http body (model)
post_body = nil post_body = nil
if body != nil _body_param = opts[:'body']
if body.is_a?(Array) if _body_param != nil
array = Array.new if _body_param.is_a?(Array)
body.each do |item| _array = Array.new
if item.respond_to?("to_body".to_sym) _body_param.each do |item|
array.push item.to_body if item.respond_to?(:to_body)
_array.push item.to_body
else else
array.push item _array.push item
end end
end end
post_body = array post_body = _array
else else
if body.respond_to?("to_body".to_sym) if _body_param.respond_to?(:to_body)
post_body = body.to_body post_body = _body_param.to_body
else else
post_body = body post_body = _body_param
end end
end end
end end

View File

@@ -7,6 +7,8 @@ require 'logger'
module Swagger module Swagger
@configuration = Configuration.new
class << self class << self
attr_accessor :logger attr_accessor :logger
@@ -27,7 +29,6 @@ module Swagger
# end # end
# #
def configure def configure
self.configuration ||= Configuration.new
yield(configuration) if block_given? yield(configuration) if block_given?
# Configure logger. Default to use Rails # Configure logger. Default to use Rails

View File

@@ -32,22 +32,23 @@ class UserApi
# http body (model) # http body (model)
post_body = nil post_body = nil
if body != nil _body_param = opts[:'body']
if body.is_a?(Array) if _body_param != nil
array = Array.new if _body_param.is_a?(Array)
body.each do |item| _array = Array.new
if item.respond_to?("to_body".to_sym) _body_param.each do |item|
array.push item.to_body if item.respond_to?(:to_body)
_array.push item.to_body
else else
array.push item _array.push item
end end
end end
post_body = array post_body = _array
else else
if body.respond_to?("to_body".to_sym) if _body_param.respond_to?(:to_body)
post_body = body.to_body post_body = _body_param.to_body
else else
post_body = body post_body = _body_param
end end
end end
end end
@@ -83,22 +84,23 @@ class UserApi
# http body (model) # http body (model)
post_body = nil post_body = nil
if body != nil _body_param = opts[:'body']
if body.is_a?(Array) if _body_param != nil
array = Array.new if _body_param.is_a?(Array)
body.each do |item| _array = Array.new
if item.respond_to?("to_body".to_sym) _body_param.each do |item|
array.push item.to_body if item.respond_to?(:to_body)
_array.push item.to_body
else else
array.push item _array.push item
end end
end end
post_body = array post_body = _array
else else
if body.respond_to?("to_body".to_sym) if _body_param.respond_to?(:to_body)
post_body = body.to_body post_body = _body_param.to_body
else else
post_body = body post_body = _body_param
end end
end end
end end
@@ -134,22 +136,23 @@ class UserApi
# http body (model) # http body (model)
post_body = nil post_body = nil
if body != nil _body_param = opts[:'body']
if body.is_a?(Array) if _body_param != nil
array = Array.new if _body_param.is_a?(Array)
body.each do |item| _array = Array.new
if item.respond_to?("to_body".to_sym) _body_param.each do |item|
array.push item.to_body if item.respond_to?(:to_body)
_array.push item.to_body
else else
array.push item _array.push item
end end
end end
post_body = array post_body = _array
else else
if body.respond_to?("to_body".to_sym) if _body_param.respond_to?(:to_body)
post_body = body.to_body post_body = _body_param.to_body
else else
post_body = body post_body = _body_param
end end
end end
end end
@@ -288,22 +291,23 @@ class UserApi
# http body (model) # http body (model)
post_body = nil post_body = nil
if body != nil _body_param = opts[:'body']
if body.is_a?(Array) if _body_param != nil
array = Array.new if _body_param.is_a?(Array)
body.each do |item| _array = Array.new
if item.respond_to?("to_body".to_sym) _body_param.each do |item|
array.push item.to_body if item.respond_to?(:to_body)
_array.push item.to_body
else else
array.push item _array.push item
end end
end end
post_body = array post_body = _array
else else
if body.respond_to?("to_body".to_sym) if _body_param.respond_to?(:to_body)
post_body = body.to_body post_body = _body_param.to_body
else else
post_body = body post_body = _body_param
end end
end end
end end

View File

@@ -15,17 +15,17 @@ describe "Pet" do
end end
it "should find pets by status" do it "should find pets by status" do
pets = PetApi.findPetsByStatus('available') pets = PetApi.findPetsByStatus(:status => 'available')
pets.length.should >= 3 pets.length.should >= 3
end end
it "should not find a pet with invalid status" do it "should not find a pet with invalid status" do
pets = PetApi.findPetsByStatus('invalid-status') pets = PetApi.findPetsByStatus(:status => 'invalid-status')
pets.length.should == 0 pets.length.should == 0
end end
it "should find a pet by status" do it "should find a pet by status" do
pets = PetApi.findPetsByStatus("available,sold") pets = PetApi.findPetsByStatus(:status => "available,sold")
pets.map {|pet| pets.map {|pet|
if(pet.status != 'available' && pet.status != 'sold') if(pet.status != 'available' && pet.status != 'sold')
raise "pet status wasn't right" raise "pet status wasn't right"
@@ -35,7 +35,7 @@ describe "Pet" do
it "should update a pet" do it "should update a pet" do
pet = Pet.new({'id' => 10002, 'status' => 'sold'}) pet = Pet.new({'id' => 10002, 'status' => 'sold'})
PetApi.addPet(pet) PetApi.addPet(:body => pet)
fetched = PetApi.getPetById(10002) fetched = PetApi.getPetById(10002)
fetched.id.should == 10002 fetched.id.should == 10002
@@ -44,7 +44,7 @@ describe "Pet" do
it "should create a pet" do it "should create a pet" do
pet = Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING") pet = Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING")
PetApi.addPet(pet) PetApi.addPet(:body => pet)
pet = PetApi.getPetById(10002) pet = PetApi.getPetById(10002)
pet.id.should == 10002 pet.id.should == 10002

View File

@@ -47,10 +47,10 @@ end
# always delete and then re-create the pet object with 10002 # always delete and then re-create the pet object with 10002
def prepare_pet def prepare_pet
# remove the pet # remove the pet
PetApi.deletePet('special-key', 10002) PetApi.deletePet(10002, :api_key => 'special-key')
# recreate the pet # recreate the pet
pet = Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING") pet = Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING")
PetApi.addPet(pet) PetApi.addPet(:body => pet)
end end
# always delete and then re-create the store order # always delete and then re-create the store order
@@ -61,7 +61,7 @@ def prepare_store
"shipDate" => "2015-04-06T23:42:01.678Z", "shipDate" => "2015-04-06T23:42:01.678Z",
"status" => "placed", "status" => "placed",
"complete" => false) "complete" => false)
StoreApi.placeOrder(order) StoreApi.placeOrder(:body => order)
end end
configure_swagger configure_swagger