From 84c6b869f8ca1c044bf74ef652e98f7e7c4b95e4 Mon Sep 17 00:00:00 2001 From: xhh Date: Thu, 25 Feb 2016 11:48:30 +0800 Subject: [PATCH] Ruby client: handle empty/reserved operation id by using a normalized one instead of throwing errors --- .../codegen/languages/RubyClientCodegen.java | 11 +++++++--- .../petstore/ruby/lib/petstore/api/pet_api.rb | 20 +++++++++---------- .../petstore/ruby/spec/api/PetApi_spec.rb | 6 +++--- 3 files changed, 21 insertions(+), 16 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 da8221b676c..62e387a4a50 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 @@ -38,6 +38,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig { protected String gemAuthor = ""; protected String gemAuthorEmail = ""; + protected static int emptyMethodNameCounter = 0; public RubyClientCodegen() { super(); @@ -409,14 +410,18 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig { @Override public String toOperationId(String operationId) { - // throw exception if method name is empty + // rename to empty_method_name_1 (e.g.) if method name is empty if (StringUtils.isEmpty(operationId)) { - throw new RuntimeException("Empty method name (operationId) not allowed"); + operationId = underscore("empty_method_name_" + emptyMethodNameCounter++); + LOGGER.warn("Empty method name (operationId) found. Renamed to " + operationId); + return operationId; } // method name cannot use reserved keyword, e.g. return if (reservedWords.contains(operationId)) { - throw new RuntimeException(operationId + " (reserved word) cannot be used as method name"); + String newOperationId = underscore("call_" + operationId); + LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + newOperationId); + return newOperationId; } return underscore(sanitizeName(operationId)); 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 c8aa23b6565..daed60caeb6 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb @@ -506,29 +506,29 @@ module Petstore # 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 package ID of pet that needs to be fetched # @param [Hash] opts the optional parameters # @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) + def pet_pet_idtesting_byte_arraytrue_get(package, opts = {}) + data, status_code, headers = pet_pet_idtesting_byte_arraytrue_get_with_http_info(package, 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 package ID of pet that needs to be fetched # @param [Hash] opts the optional parameters # @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 = {}) + def pet_pet_idtesting_byte_arraytrue_get_with_http_info(package, opts = {}) if @api_client.config.debugging - @api_client.config.logger.debug "Calling API: PetApi#get_pet_by_id_with_byte_array ..." + @api_client.config.logger.debug "Calling API: PetApi#pet_pet_idtesting_byte_arraytrue_get ..." 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? + # verify the required parameter 'package' is set + fail "Missing the required parameter 'package' when calling pet_pet_idtesting_byte_arraytrue_get" if package.nil? # resource path - path = "/pet/{petId}?testing_byte_array=true".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s) + path = "/pet/{petId}?testing_byte_array=true".sub('{format}','json').sub('{' + 'package' + '}', package.to_s) # query parameters query_params = {} @@ -559,7 +559,7 @@ module Petstore :auth_names => auth_names, :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}" + @api_client.config.logger.debug "API called: PetApi#pet_pet_idtesting_byte_arraytrue_get\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" end return data, status_code, headers end diff --git a/samples/client/petstore/ruby/spec/api/PetApi_spec.rb b/samples/client/petstore/ruby/spec/api/PetApi_spec.rb index 457be15f9a4..e4973e5a8c7 100644 --- a/samples/client/petstore/ruby/spec/api/PetApi_spec.rb +++ b/samples/client/petstore/ruby/spec/api/PetApi_spec.rb @@ -169,13 +169,13 @@ describe 'PetApi' do end end - # unit tests for get_pet_by_id_with_byte_array + # unit tests for pet_pet_idtesting_byte_arraytrue_get # 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 package ID of pet that needs to be fetched # @param [Hash] opts the optional parameters # @return [String] - describe 'get_pet_by_id_with_byte_array test' do + describe 'pet_pet_idtesting_byte_arraytrue_get test' do it "should work" do # assertion here # should be_a()