forked from loafle/openapi-generator-original
Handle "binary" as String in Ruby client
as it seems weird to handle byte array (array of integer) in Ruby
This commit is contained in:
parent
aad0547b40
commit
8c19626095
@ -86,7 +86,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("map", "Hash");
|
||||
typeMapping.put("object", "Object");
|
||||
typeMapping.put("file", "File");
|
||||
typeMapping.put("binary", "Byte Array");
|
||||
typeMapping.put("binary", "String");
|
||||
|
||||
// remove modelPackage and apiPackage added by default
|
||||
Iterator<CliOption> itr = cliOptions.iterator();
|
||||
|
@ -70,10 +70,8 @@ module {{moduleName}}
|
||||
|
||||
# http body (model)
|
||||
{{^bodyParam}}post_body = nil
|
||||
{{/bodyParam}}{{#bodyParam}}{{#isBinary}}post_body = {{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}
|
||||
post_body = post_body.pack('C*') if post_body
|
||||
{{/isBinary}}{{^isBinary}}post_body = @api_client.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}})
|
||||
{{/isBinary}}{{/bodyParam}}
|
||||
{{/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,
|
||||
|
@ -106,8 +106,8 @@ module {{moduleName}}
|
||||
body = response.body
|
||||
return nil if body.nil? || body.empty?
|
||||
|
||||
# handle binary response (byte array)
|
||||
return body.bytes if return_type == 'Byte Array'
|
||||
# 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'
|
||||
@ -277,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) }
|
||||
|
@ -492,7 +492,7 @@ module Petstore
|
||||
# Returns a pet when ID < 10. ID > 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 [Byte Array]
|
||||
# @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
|
||||
@ -502,7 +502,7 @@ module Petstore
|
||||
# Returns a pet when ID < 10. ID > 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<(Byte Array, Fixnum, Hash)>] Byte Array 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 ..."
|
||||
@ -541,7 +541,7 @@ module Petstore
|
||||
:form_params => form_params,
|
||||
:body => post_body,
|
||||
:auth_names => auth_names,
|
||||
:return_type => 'Byte Array')
|
||||
: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
|
||||
@ -551,7 +551,7 @@ module Petstore
|
||||
# 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 [Byte Array] :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)
|
||||
@ -561,7 +561,7 @@ module Petstore
|
||||
# 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 [Byte Array] :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
|
||||
@ -589,8 +589,7 @@ module Petstore
|
||||
form_params = {}
|
||||
|
||||
# http body (model)
|
||||
post_body = opts[:'body']
|
||||
post_body = post_body.pack('C*') if post_body
|
||||
post_body = @api_client.object_to_http_body(opts[:'body'])
|
||||
|
||||
auth_names = ['petstore_auth']
|
||||
data, status_code, headers = @api_client.call_api(:POST, path,
|
||||
|
@ -106,8 +106,8 @@ module Petstore
|
||||
body = response.body
|
||||
return nil if body.nil? || body.empty?
|
||||
|
||||
# handle binary response (byte array)
|
||||
return body.bytes if return_type == 'Byte Array'
|
||||
# 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'
|
||||
@ -277,7 +277,7 @@ module Petstore
|
||||
# @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) }
|
||||
|
@ -94,17 +94,15 @@ describe "Pet" do
|
||||
end
|
||||
end
|
||||
|
||||
it "should create and get pet with byte array" do
|
||||
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
|
||||
bytes = serialize_json(pet).bytes
|
||||
@pet_api.add_pet_using_byte_array(body: bytes)
|
||||
str = serialize_json(pet)
|
||||
@pet_api.add_pet_using_byte_array(body: str)
|
||||
|
||||
fetchedBytes = @pet_api.get_pet_by_id_with_byte_array(pet.id)
|
||||
fetchedBytes.should be_a(Array)
|
||||
fetchedBytes[0].should be_a(Fixnum)
|
||||
|
||||
fetched = deserialize_json(fetchedBytes.pack('C*'), 'Pet')
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user