From aad0547b40ea28c0ee447bb3b2337c793db9373c Mon Sep 17 00:00:00 2001 From: xhh Date: Thu, 21 Jan 2016 18:01:03 +0800 Subject: [PATCH] Ruby: support binary (byte array) for body parameter and response --- .../codegen/languages/RubyClientCodegen.java | 1 + .../src/main/resources/ruby/api.mustache | 11 +- .../main/resources/ruby/api_client.mustache | 3 + .../petstore/ruby/lib/petstore/api/pet_api.rb | 129 ++++++++++++++++-- .../ruby/lib/petstore/api/store_api.rb | 8 -- .../ruby/lib/petstore/api/user_api.rb | 12 -- .../petstore/ruby/lib/petstore/api_client.rb | 3 + samples/client/petstore/ruby/spec/pet_spec.rb | 39 +++++- .../client/petstore/ruby/spec/store_spec.rb | 9 +- 9 files changed, 167 insertions(+), 48 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java index ddc4f35346f..5ff6e82cff1 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java @@ -86,6 +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"); // remove modelPackage and apiPackage added by default Iterator itr = cliOptions.iterator(); diff --git a/modules/swagger-codegen/src/main/resources/ruby/api.mustache b/modules/swagger-codegen/src/main/resources/ruby/api.mustache index d7967135336..7df6c3ec42a 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/api.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/api.mustache @@ -70,9 +70,10 @@ module {{moduleName}} # http body (model) {{^bodyParam}}post_body = nil - {{/bodyParam}}{{#bodyParam}}post_body = @api_client.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}) - {{/bodyParam}} - + {{/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}} auth_names = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}] data, status_code, headers = @api_client.call_api(:{{httpMethod}}, path, :header_params => header_params, @@ -90,7 +91,3 @@ module {{moduleName}} end {{/operations}} end - - - - diff --git a/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache b/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache index e9c2160450e..cd17609028a 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache @@ -106,6 +106,9 @@ 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' + # handle file downloading - save response body into a tmp file and return the File instance return download_file(response) if return_type == 'File' diff --git a/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb b/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb index 9641ad0b707..c0b6bf2736d 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb @@ -51,7 +51,6 @@ module Petstore # 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, @@ -108,7 +107,6 @@ module Petstore # 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, @@ -166,7 +164,6 @@ module Petstore # http body (model) post_body = nil - auth_names = ['petstore_auth'] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, @@ -225,7 +222,6 @@ module Petstore # http body (model) post_body = nil - auth_names = ['petstore_auth'] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, @@ -286,7 +282,6 @@ module Petstore # http body (model) post_body = nil - auth_names = ['api_key'] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, @@ -353,7 +348,6 @@ module Petstore # http body (model) post_body = nil - auth_names = ['petstore_auth'] data, status_code, headers = @api_client.call_api(:POST, path, :header_params => header_params, @@ -416,7 +410,6 @@ module Petstore # http body (model) post_body = nil - auth_names = ['petstore_auth'] data, status_code, headers = @api_client.call_api(:DELETE, path, :header_params => header_params, @@ -482,7 +475,6 @@ module Petstore # http body (model) post_body = nil - auth_names = ['petstore_auth'] data, status_code, headers = @api_client.call_api(:POST, path, :header_params => header_params, @@ -495,9 +487,122 @@ module Petstore end return data, status_code, headers end + + # Fake endpoint to test byte array return by 'Find pet by ID' + # 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] + 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 + end + + # Fake endpoint to test byte array return by 'Find pet by ID' + # 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 + 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 ..." + end + + # verify the required parameter 'pet_id' is set + fail "Missing the required parameter 'pet_id' when calling get_pet_by_id_with_byte_array" if pet_id.nil? + + # resource path + path = "/pet/{petId}?testing_byte_array=true".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s) + + # query parameters + query_params = {} + + # header parameters + header_params = {} + + # HTTP header 'Accept' (if needed) + _header_accept = ['application/json', 'application/xml'] + _header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result + + # HTTP header 'Content-Type' + _header_content_type = [] + header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type) + + # form parameters + form_params = {} + + # 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 => 'Byte Array') + 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 + return data, status_code, headers + end + + # 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 + # @return [nil] + def add_pet_using_byte_array(opts = {}) + add_pet_using_byte_array_with_http_info(opts) + return nil + end + + # 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 + # @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 + @api_client.config.logger.debug "Calling API: PetApi#add_pet_using_byte_array ..." + end + + # resource path + path = "/pet?testing_byte_array=true".sub('{format}','json') + + # query parameters + query_params = {} + + # header parameters + header_params = {} + + # HTTP header 'Accept' (if needed) + _header_accept = ['application/json', 'application/xml'] + _header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result + + # HTTP header 'Content-Type' + _header_content_type = ['application/json', 'application/xml'] + header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type) + + # form parameters + form_params = {} + + # http body (model) + post_body = opts[:'body'] + post_body = post_body.pack('C*') if post_body + + auth_names = ['petstore_auth'] + data, status_code, headers = @api_client.call_api(:POST, path, + :header_params => header_params, + :query_params => query_params, + :form_params => form_params, + :body => post_body, + :auth_names => auth_names) + if @api_client.config.debugging + @api_client.config.logger.debug "API called: PetApi#add_pet_using_byte_array\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" + end + return data, status_code, headers + end end end - - - - diff --git a/samples/client/petstore/ruby/lib/petstore/api/store_api.rb b/samples/client/petstore/ruby/lib/petstore/api/store_api.rb index 1d8a92e288f..18ad29af7e9 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/store_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/store_api.rb @@ -49,7 +49,6 @@ module Petstore # http body (model) post_body = nil - auth_names = ['api_key'] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, @@ -107,7 +106,6 @@ module Petstore # 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, @@ -168,7 +166,6 @@ module Petstore # http body (model) post_body = nil - auth_names = [] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, @@ -229,7 +226,6 @@ module Petstore # http body (model) post_body = nil - auth_names = [] data, status_code, headers = @api_client.call_api(:DELETE, path, :header_params => header_params, @@ -244,7 +240,3 @@ module Petstore end end end - - - - diff --git a/samples/client/petstore/ruby/lib/petstore/api/user_api.rb b/samples/client/petstore/ruby/lib/petstore/api/user_api.rb index e1c8332b835..7b44fa8808a 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/user_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/user_api.rb @@ -51,7 +51,6 @@ module Petstore # 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, @@ -108,7 +107,6 @@ module Petstore # 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, @@ -165,7 +163,6 @@ module Petstore # 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, @@ -226,7 +223,6 @@ module Petstore # http body (model) post_body = nil - auth_names = [] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, @@ -282,7 +278,6 @@ module Petstore # http body (model) post_body = nil - auth_names = [] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, @@ -342,7 +337,6 @@ module Petstore # http body (model) post_body = nil - auth_names = [] data, status_code, headers = @api_client.call_api(:GET, path, :header_params => header_params, @@ -405,7 +399,6 @@ module Petstore # 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, @@ -465,7 +458,6 @@ module Petstore # http body (model) post_body = nil - auth_names = [] data, status_code, headers = @api_client.call_api(:DELETE, path, :header_params => header_params, @@ -480,7 +472,3 @@ module Petstore end end end - - - - diff --git a/samples/client/petstore/ruby/lib/petstore/api_client.rb b/samples/client/petstore/ruby/lib/petstore/api_client.rb index a38f5aa81d7..747bcf85f96 100644 --- a/samples/client/petstore/ruby/lib/petstore/api_client.rb +++ b/samples/client/petstore/ruby/lib/petstore/api_client.rb @@ -106,6 +106,9 @@ 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' + # handle file downloading - save response body into a tmp file and return the File instance return download_file(response) if return_type == 'File' diff --git a/samples/client/petstore/ruby/spec/pet_spec.rb b/samples/client/petstore/ruby/spec/pet_spec.rb index b9aa9398bc2..dc2ff2b5488 100644 --- a/samples/client/petstore/ruby/spec/pet_spec.rb +++ b/samples/client/petstore/ruby/spec/pet_spec.rb @@ -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) @@ -84,6 +94,25 @@ describe "Pet" do end end + it "should create and get pet with byte array" 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) + + 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.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 @@ -122,16 +151,18 @@ describe "Pet" do 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 diff --git a/samples/client/petstore/ruby/spec/store_spec.rb b/samples/client/petstore/ruby/spec/store_spec.rb index 30743fd374e..46b6071becc 100644 --- a/samples/client/petstore/ruby/spec/store_spec.rb +++ b/samples/client/petstore/ruby/spec/store_spec.rb @@ -3,16 +3,15 @@ require 'spec_helper' describe "Store" do before do @api = Petstore::StoreApi.new(API_CLIENT) - @order_id = prepare_store(@api) - end - - after do - @api.delete_order(@order_id) end it "should fetch an order" do + @order_id = prepare_store(@api) + item = @api.get_order_by_id(@order_id) item.id.should == @order_id + + @api.delete_order(@order_id) end it "should featch the inventory" do