Skip to content

[Ruby] Support "binary" format for body parameter and response #1934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public RubyClientCodegen() {
typeMapping.put("map", "Hash");
typeMapping.put("object", "Object");
typeMapping.put("file", "File");
typeMapping.put("binary", "String");

// remove modelPackage and apiPackage added by default
Iterator<CliOption> itr = cliOptions.iterator();
Expand Down
5 changes: 0 additions & 5 deletions modules/swagger-codegen/src/main/resources/ruby/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ module {{moduleName}}
{{^bodyParam}}post_body = nil
{{/bodyParam}}{{#bodyParam}}post_body = @api_client.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}})
{{/bodyParam}}

auth_names = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]
data, status_code, headers = @api_client.call_api(:{{httpMethod}}, path,
:header_params => header_params,
Expand All @@ -90,7 +89,3 @@ module {{moduleName}}
end
{{/operations}}
end




Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ module {{moduleName}}
body = response.body
return nil if body.nil? || body.empty?

# return response body directly for String return type
return body if return_type == 'String'

# handle file downloading - save response body into a tmp file and return the File instance
return download_file(response) if return_type == 'File'

Expand Down Expand Up @@ -274,7 +277,7 @@ module {{moduleName}}
# @param [Object] model object to be converted into JSON string
# @return [String] JSON string representation of the object
def object_to_http_body(model)
return if model.nil?
return model if model.nil? || model.is_a?(String)
_body = nil
if model.is_a?(Array)
_body = model.map{|m| object_to_hash(m) }
Expand Down
24 changes: 5 additions & 19 deletions samples/client/petstore/ruby/lib/petstore/api/pet_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def update_pet_with_http_info(opts = {})
# http body (model)
post_body = @api_client.object_to_http_body(opts[:'body'])


auth_names = ['petstore_auth']
data, status_code, headers = @api_client.call_api(:PUT, path,
:header_params => header_params,
Expand Down Expand Up @@ -108,7 +107,6 @@ def add_pet_with_http_info(opts = {})
# http body (model)
post_body = @api_client.object_to_http_body(opts[:'body'])


auth_names = ['petstore_auth']
data, status_code, headers = @api_client.call_api(:POST, path,
:header_params => header_params,
Expand Down Expand Up @@ -166,7 +164,6 @@ def find_pets_by_status_with_http_info(opts = {})
# http body (model)
post_body = nil


auth_names = ['petstore_auth']
data, status_code, headers = @api_client.call_api(:GET, path,
:header_params => header_params,
Expand Down Expand Up @@ -225,7 +222,6 @@ def find_pets_by_tags_with_http_info(opts = {})
# http body (model)
post_body = nil


auth_names = ['petstore_auth']
data, status_code, headers = @api_client.call_api(:GET, path,
:header_params => header_params,
Expand Down Expand Up @@ -286,7 +282,6 @@ def get_pet_by_id_with_http_info(pet_id, opts = {})
# http body (model)
post_body = nil


auth_names = ['api_key']
data, status_code, headers = @api_client.call_api(:GET, path,
:header_params => header_params,
Expand Down Expand Up @@ -353,7 +348,6 @@ def update_pet_with_form_with_http_info(pet_id, opts = {})
# http body (model)
post_body = nil


auth_names = ['petstore_auth']
data, status_code, headers = @api_client.call_api(:POST, path,
:header_params => header_params,
Expand Down Expand Up @@ -416,7 +410,6 @@ def delete_pet_with_http_info(pet_id, opts = {})
# http body (model)
post_body = nil


auth_names = ['petstore_auth']
data, status_code, headers = @api_client.call_api(:DELETE, path,
:header_params => header_params,
Expand Down Expand Up @@ -482,7 +475,6 @@ def upload_file_with_http_info(pet_id, opts = {})
# http body (model)
post_body = nil


auth_names = ['petstore_auth']
data, status_code, headers = @api_client.call_api(:POST, path,
:header_params => header_params,
Expand All @@ -500,7 +492,7 @@ def upload_file_with_http_info(pet_id, opts = {})
# Returns a pet when ID &lt; 10. ID &gt; 10 or nonintegers will simulate API error conditions
# @param pet_id ID of pet that needs to be fetched
# @param [Hash] opts the optional parameters
# @return [binary]
# @return [String]
def get_pet_by_id_with_byte_array(pet_id, opts = {})
data, status_code, headers = get_pet_by_id_with_byte_array_with_http_info(pet_id, opts)
return data
Expand All @@ -510,7 +502,7 @@ def get_pet_by_id_with_byte_array(pet_id, opts = {})
# Returns a pet when ID &lt; 10. ID &gt; 10 or nonintegers will simulate API error conditions
# @param pet_id ID of pet that needs to be fetched
# @param [Hash] opts the optional parameters
# @return [Array<(binary, Fixnum, Hash)>] binary data, response status code and response headers
# @return [Array<(String, Fixnum, Hash)>] String data, response status code and response headers
def get_pet_by_id_with_byte_array_with_http_info(pet_id, opts = {})
if @api_client.config.debugging
@api_client.config.logger.debug "Calling API: PetApi#get_pet_by_id_with_byte_array ..."
Expand Down Expand Up @@ -542,15 +534,14 @@ def get_pet_by_id_with_byte_array_with_http_info(pet_id, opts = {})
# http body (model)
post_body = nil


auth_names = ['api_key']
data, status_code, headers = @api_client.call_api(:GET, path,
:header_params => header_params,
:query_params => query_params,
:form_params => form_params,
:body => post_body,
:auth_names => auth_names,
:return_type => 'binary')
:return_type => 'String')
if @api_client.config.debugging
@api_client.config.logger.debug "API called: PetApi#get_pet_by_id_with_byte_array\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end
Expand All @@ -560,7 +551,7 @@ def get_pet_by_id_with_byte_array_with_http_info(pet_id, opts = {})
# Fake endpoint to test byte array in body parameter for adding a new pet to the store
#
# @param [Hash] opts the optional parameters
# @option opts [binary] :body Pet object in the form of byte array
# @option opts [String] :body Pet object in the form of byte array
# @return [nil]
def add_pet_using_byte_array(opts = {})
add_pet_using_byte_array_with_http_info(opts)
Expand All @@ -570,7 +561,7 @@ def add_pet_using_byte_array(opts = {})
# Fake endpoint to test byte array in body parameter for adding a new pet to the store
#
# @param [Hash] opts the optional parameters
# @option opts [binary] :body Pet object in the form of byte array
# @option opts [String] :body Pet object in the form of byte array
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def add_pet_using_byte_array_with_http_info(opts = {})
if @api_client.config.debugging
Expand Down Expand Up @@ -600,7 +591,6 @@ def add_pet_using_byte_array_with_http_info(opts = {})
# http body (model)
post_body = @api_client.object_to_http_body(opts[:'body'])


auth_names = ['petstore_auth']
data, status_code, headers = @api_client.call_api(:POST, path,
:header_params => header_params,
Expand All @@ -615,7 +605,3 @@ def add_pet_using_byte_array_with_http_info(opts = {})
end
end
end




8 changes: 0 additions & 8 deletions samples/client/petstore/ruby/lib/petstore/api/store_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def get_inventory_with_http_info(opts = {})
# http body (model)
post_body = nil


auth_names = ['api_key']
data, status_code, headers = @api_client.call_api(:GET, path,
:header_params => header_params,
Expand Down Expand Up @@ -107,7 +106,6 @@ def place_order_with_http_info(opts = {})
# http body (model)
post_body = @api_client.object_to_http_body(opts[:'body'])


auth_names = []
data, status_code, headers = @api_client.call_api(:POST, path,
:header_params => header_params,
Expand Down Expand Up @@ -168,7 +166,6 @@ def get_order_by_id_with_http_info(order_id, opts = {})
# http body (model)
post_body = nil


auth_names = []
data, status_code, headers = @api_client.call_api(:GET, path,
:header_params => header_params,
Expand Down Expand Up @@ -229,7 +226,6 @@ def delete_order_with_http_info(order_id, opts = {})
# http body (model)
post_body = nil


auth_names = []
data, status_code, headers = @api_client.call_api(:DELETE, path,
:header_params => header_params,
Expand All @@ -244,7 +240,3 @@ def delete_order_with_http_info(order_id, opts = {})
end
end
end




12 changes: 0 additions & 12 deletions samples/client/petstore/ruby/lib/petstore/api/user_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def create_user_with_http_info(opts = {})
# http body (model)
post_body = @api_client.object_to_http_body(opts[:'body'])


auth_names = []
data, status_code, headers = @api_client.call_api(:POST, path,
:header_params => header_params,
Expand Down Expand Up @@ -108,7 +107,6 @@ def create_users_with_array_input_with_http_info(opts = {})
# http body (model)
post_body = @api_client.object_to_http_body(opts[:'body'])


auth_names = []
data, status_code, headers = @api_client.call_api(:POST, path,
:header_params => header_params,
Expand Down Expand Up @@ -165,7 +163,6 @@ def create_users_with_list_input_with_http_info(opts = {})
# http body (model)
post_body = @api_client.object_to_http_body(opts[:'body'])


auth_names = []
data, status_code, headers = @api_client.call_api(:POST, path,
:header_params => header_params,
Expand Down Expand Up @@ -226,7 +223,6 @@ def login_user_with_http_info(opts = {})
# http body (model)
post_body = nil


auth_names = []
data, status_code, headers = @api_client.call_api(:GET, path,
:header_params => header_params,
Expand Down Expand Up @@ -282,7 +278,6 @@ def logout_user_with_http_info(opts = {})
# http body (model)
post_body = nil


auth_names = []
data, status_code, headers = @api_client.call_api(:GET, path,
:header_params => header_params,
Expand Down Expand Up @@ -342,7 +337,6 @@ def get_user_by_name_with_http_info(username, opts = {})
# http body (model)
post_body = nil


auth_names = []
data, status_code, headers = @api_client.call_api(:GET, path,
:header_params => header_params,
Expand Down Expand Up @@ -405,7 +399,6 @@ def update_user_with_http_info(username, opts = {})
# http body (model)
post_body = @api_client.object_to_http_body(opts[:'body'])


auth_names = []
data, status_code, headers = @api_client.call_api(:PUT, path,
:header_params => header_params,
Expand Down Expand Up @@ -465,7 +458,6 @@ def delete_user_with_http_info(username, opts = {})
# http body (model)
post_body = nil


auth_names = []
data, status_code, headers = @api_client.call_api(:DELETE, path,
:header_params => header_params,
Expand All @@ -480,7 +472,3 @@ def delete_user_with_http_info(username, opts = {})
end
end
end




5 changes: 4 additions & 1 deletion samples/client/petstore/ruby/lib/petstore/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def deserialize(response, return_type)
body = response.body
return nil if body.nil? || body.empty?

# return response body directly for String return type
return body if return_type == 'String'

# handle file downloading - save response body into a tmp file and return the File instance
return download_file(response) if return_type == 'File'

Expand Down Expand Up @@ -274,7 +277,7 @@ def select_header_content_type(content_types)
# @param [Object] model object to be converted into JSON string
# @return [String] JSON string representation of the object
def object_to_http_body(model)
return if model.nil?
return model if model.nil? || model.is_a?(String)
_body = nil
if model.is_a?(Array)
_body = model.map{|m| object_to_hash(m) }
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/ruby/spec/api/PetApi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
# Returns a pet when ID &lt; 10. ID &gt; 10 or nonintegers will simulate API error conditions
# @param pet_id ID of pet that needs to be fetched
# @param [Hash] opts the optional parameters
# @return [binary]
# @return [String]
describe 'get_pet_by_id_with_byte_array test' do
it "should work" do
# assertion here
Expand All @@ -173,7 +173,7 @@
# Fake endpoint to test byte array in body parameter for adding a new pet to the store
#
# @param [Hash] opts the optional parameters
# @option opts [binary] :body Pet object in the form of byte array
# @option opts [String] :body Pet object in the form of byte array
# @return [nil]
describe 'add_pet_using_byte_array test' do
it "should work" do
Expand Down
37 changes: 33 additions & 4 deletions samples/client/petstore/ruby/spec/pet_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
require 'spec_helper'
require 'json'

def serialize_json(o)
API_CLIENT.object_to_http_body(o)
end

def deserialize_json(s, type)
headers = {'Content-Type' => 'application/json'}
response = double('response', headers: headers, body: s)
API_CLIENT.deserialize(response, type)
end

describe "Pet" do
before do
@pet_api = Petstore::PetApi.new(API_CLIENT)
Expand Down Expand Up @@ -84,6 +94,23 @@
end
end

it "should create and get pet with byte array (binary, string)" do
pet = @pet_api.get_pet_by_id(@pet_id)
pet.id = @pet_id + 1
str = serialize_json(pet)
@pet_api.add_pet_using_byte_array(body: str)

fetched_str = @pet_api.get_pet_by_id_with_byte_array(pet.id)
fetched_str.should be_a(String)
fetched = deserialize_json(fetched_str, 'Pet')
fetched.should be_a(Petstore::Pet)
fetched.id.should == pet.id
fetched.category.should be_a(Petstore::Category)
fetched.category.name.should == pet.category.name

@pet_api.delete_pet(pet.id)
end

it "should update a pet" do
pet = @pet_api.get_pet_by_id(@pet_id)
pet.id.should == @pet_id
Expand Down Expand Up @@ -122,16 +149,18 @@
end

it "should create a pet" do
pet = Petstore::Pet.new('id' => 10003, 'name' => "RUBY UNIT TESTING")
id = @pet_id + 1

pet = Petstore::Pet.new('id' => id, 'name' => "RUBY UNIT TESTING")
result = @pet_api.add_pet(:body => pet)
# nothing is returned
result.should be_nil

pet = @pet_api.get_pet_by_id(10003)
pet.id.should == 10003
pet = @pet_api.get_pet_by_id(id)
pet.id.should == id
pet.name.should == "RUBY UNIT TESTING"

@pet_api.delete_pet(10003)
@pet_api.delete_pet(id)
end

it "should upload a file to a pet" do
Expand Down
Loading