From f0425d77ef9aa50c3e97ec8c69f94b87788eacc3 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 2 Aug 2018 22:00:50 +0800 Subject: [PATCH] [Ruby] Better handling of operationID starting with a number (#719) * better handling of operationId starting with numbers in Ruby * update Rubocop to use Layout --- .../codegen/languages/RubyClientCodegen.java | 6 ++++++ .../main/resources/ruby-client/rubocop.mustache | 2 +- samples/client/petstore/ruby/.rubocop.yml | 2 +- samples/client/petstore/ruby/README.md | 6 +++--- .../client/petstore/ruby/docs/AnotherFakeApi.md | 12 ++++++------ .../ruby/lib/petstore/api/another_fake_api.rb | 16 ++++++++-------- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyClientCodegen.java index 74558b990b1..b80b423311d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyClientCodegen.java @@ -462,6 +462,12 @@ public class RubyClientCodegen extends AbstractRubyCodegen { return newOperationId; } + // operationId starts with a number + if (operationId.matches("^\\d.*")) { + LOGGER.warn(operationId + " (starting with a number) cannot be used as method name. Renamed to " + underscore(sanitizeName("call_" + operationId))); + operationId = "call_" + operationId; + } + return underscore(sanitizeName(operationId)); } diff --git a/modules/openapi-generator/src/main/resources/ruby-client/rubocop.mustache b/modules/openapi-generator/src/main/resources/ruby-client/rubocop.mustache index 9bc9d341d8b..98c7e3c7e51 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/rubocop.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/rubocop.mustache @@ -136,7 +136,7 @@ Style/UnneededPercentQ: # Align `end` with the matching keyword or starting expression except for # assignments, where it should be aligned with the LHS. -Lint/EndAlignment: +Layout/EndAlignment: Enabled: true EnforcedStyleAlignWith: variable AutoCorrect: true diff --git a/samples/client/petstore/ruby/.rubocop.yml b/samples/client/petstore/ruby/.rubocop.yml index 9bc9d341d8b..98c7e3c7e51 100644 --- a/samples/client/petstore/ruby/.rubocop.yml +++ b/samples/client/petstore/ruby/.rubocop.yml @@ -136,7 +136,7 @@ Style/UnneededPercentQ: # Align `end` with the matching keyword or starting expression except for # assignments, where it should be aligned with the LHS. -Lint/EndAlignment: +Layout/EndAlignment: Enabled: true EnforcedStyleAlignWith: variable AutoCorrect: true diff --git a/samples/client/petstore/ruby/README.md b/samples/client/petstore/ruby/README.md index 62ce92438f3..0bc344953b2 100644 --- a/samples/client/petstore/ruby/README.md +++ b/samples/client/petstore/ruby/README.md @@ -59,10 +59,10 @@ client = Petstore::Client.new # Client | client model begin #To test special tags - result = api_instance.test_special_tags(client) + result = api_instance.call_123_test_special_tags(client) p result rescue Petstore::ApiError => e - puts "Exception when calling AnotherFakeApi->test_special_tags: #{e}" + puts "Exception when calling AnotherFakeApi->call_123_test_special_tags: #{e}" end ``` @@ -73,7 +73,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2* Class | Method | HTTP request | Description ------------ | ------------- | ------------- | ------------- -*Petstore::AnotherFakeApi* | [**test_special_tags**](docs/AnotherFakeApi.md#test_special_tags) | **PATCH** /another-fake/dummy | To test special tags +*Petstore::AnotherFakeApi* | [**call_123_test_special_tags**](docs/AnotherFakeApi.md#call_123_test_special_tags) | **PATCH** /another-fake/dummy | To test special tags *Petstore::FakeApi* | [**fake_outer_boolean_serialize**](docs/FakeApi.md#fake_outer_boolean_serialize) | **POST** /fake/outer/boolean | *Petstore::FakeApi* | [**fake_outer_composite_serialize**](docs/FakeApi.md#fake_outer_composite_serialize) | **POST** /fake/outer/composite | *Petstore::FakeApi* | [**fake_outer_number_serialize**](docs/FakeApi.md#fake_outer_number_serialize) | **POST** /fake/outer/number | diff --git a/samples/client/petstore/ruby/docs/AnotherFakeApi.md b/samples/client/petstore/ruby/docs/AnotherFakeApi.md index da2d5ff0b19..2d4891e0660 100644 --- a/samples/client/petstore/ruby/docs/AnotherFakeApi.md +++ b/samples/client/petstore/ruby/docs/AnotherFakeApi.md @@ -4,15 +4,15 @@ All URIs are relative to *http://petstore.swagger.io:80/v2* Method | HTTP request | Description ------------- | ------------- | ------------- -[**test_special_tags**](AnotherFakeApi.md#test_special_tags) | **PATCH** /another-fake/dummy | To test special tags +[**call_123_test_special_tags**](AnotherFakeApi.md#call_123_test_special_tags) | **PATCH** /another-fake/dummy | To test special tags -# **test_special_tags** -> Client test_special_tags(client) +# **call_123_test_special_tags** +> Client call_123_test_special_tags(client) To test special tags -To test special tags +To test special tags and operation ID starting with number ### Example ```ruby @@ -24,10 +24,10 @@ client = Petstore::Client.new # Client | client model begin #To test special tags - result = api_instance.test_special_tags(client) + result = api_instance.call_123_test_special_tags(client) p result rescue Petstore::ApiError => e - puts "Exception when calling AnotherFakeApi->test_special_tags: #{e}" + puts "Exception when calling AnotherFakeApi->call_123_test_special_tags: #{e}" end ``` diff --git a/samples/client/petstore/ruby/lib/petstore/api/another_fake_api.rb b/samples/client/petstore/ruby/lib/petstore/api/another_fake_api.rb index af627260205..7d5554a463c 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/another_fake_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/another_fake_api.rb @@ -20,27 +20,27 @@ module Petstore @api_client = api_client end # To test special tags - # To test special tags + # To test special tags and operation ID starting with number # @param client client model # @param [Hash] opts the optional parameters # @return [Client] - def test_special_tags(client, opts = {}) - data, _status_code, _headers = test_special_tags_with_http_info(client, opts) + def call_123_test_special_tags(client, opts = {}) + data, _status_code, _headers = call_123_test_special_tags_with_http_info(client, opts) data end # To test special tags - # To test special tags + # To test special tags and operation ID starting with number # @param client client model # @param [Hash] opts the optional parameters # @return [Array<(Client, Fixnum, Hash)>] Client data, response status code and response headers - def test_special_tags_with_http_info(client, opts = {}) + def call_123_test_special_tags_with_http_info(client, opts = {}) if @api_client.config.debugging - @api_client.config.logger.debug 'Calling API: AnotherFakeApi.test_special_tags ...' + @api_client.config.logger.debug 'Calling API: AnotherFakeApi.call_123_test_special_tags ...' end # verify the required parameter 'client' is set if @api_client.config.client_side_validation && client.nil? - fail ArgumentError, "Missing the required parameter 'client' when calling AnotherFakeApi.test_special_tags" + fail ArgumentError, "Missing the required parameter 'client' when calling AnotherFakeApi.call_123_test_special_tags" end # resource path local_var_path = '/another-fake/dummy' @@ -69,7 +69,7 @@ module Petstore :auth_names => auth_names, :return_type => 'Client') if @api_client.config.debugging - @api_client.config.logger.debug "API called: AnotherFakeApi#test_special_tags\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" + @api_client.config.logger.debug "API called: AnotherFakeApi#call_123_test_special_tags\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}" end return data, status_code, headers end