From 1d02f0374b05de9155ccf82afca04f0d5e8fa35c Mon Sep 17 00:00:00 2001 From: Akira Tanimura Date: Mon, 4 Mar 2019 23:22:45 +0900 Subject: [PATCH] [Ruby] Escape string interpolation notation of Ruby (#2261) (#2287) * escape string interpolation notation of Ruby (#2261) * update samples (#2261) --- .../languages/AbstractRubyCodegen.java | 6 ++--- .../codegen/ruby/AbstractRubyCodegenTest.java | 26 +++++++++++++++++++ .../ruby/docs/ModelReturn.md | 8 ++++++ .../ruby/lib/petstore/api_error.rb | 19 ++++++++++++++ .../ruby/lib/petstore/models/model_return.rb | 17 ++++++++---- .../ruby/spec/api/fake_api_spec.rb | 4 +-- .../ruby/spec/api/another_fake_api_spec.rb | 4 +-- .../petstore/ruby/spec/api/fake_api_spec.rb | 4 +-- .../api/fake_classname_tags123_api_spec.rb | 4 +-- .../petstore/ruby/spec/api/pet_api_spec.rb | 4 +-- .../petstore/ruby/spec/api/store_api_spec.rb | 4 +-- .../petstore/ruby/spec/api/user_api_spec.rb | 4 +-- 12 files changed, 82 insertions(+), 22 deletions(-) create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/AbstractRubyCodegenTest.java diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java index 7c7184e9944..3714ee75ace 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java @@ -33,10 +33,10 @@ import java.util.Locale; import static org.openapitools.codegen.utils.StringUtils.underscore; -abstract class AbstractRubyCodegen extends DefaultCodegen implements CodegenConfig { +abstract public class AbstractRubyCodegen extends DefaultCodegen implements CodegenConfig { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRubyCodegen.class); - AbstractRubyCodegen() { + public AbstractRubyCodegen() { super(); setReservedWordsLowerCase( @@ -177,7 +177,7 @@ abstract class AbstractRubyCodegen extends DefaultCodegen implements CodegenConf @Override public String escapeUnsafeCharacters(String input) { - return input.replace("=end", "=_end").replace("=begin", "=_begin"); + return input.replace("=end", "=_end").replace("=begin", "=_begin").replace("#{", "\\#{"); } @Override diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/AbstractRubyCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/AbstractRubyCodegenTest.java new file mode 100644 index 00000000000..6f1105b2943 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/AbstractRubyCodegenTest.java @@ -0,0 +1,26 @@ +package org.openapitools.codegen.ruby; + +import org.openapitools.codegen.languages.AbstractRubyCodegen; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +/** + * Tests for AbstractRubyCodegen + */ +public class AbstractRubyCodegenTest { + private AbstractRubyCodegen codegen; + + @BeforeMethod + public void setup() { + codegen = new AbstractRubyCodegen() { + }; + } + + @Test + public void testEscapeUnsafeCharacters() { + Assert.assertEquals(codegen.escapeUnsafeCharacters("=begin"), "=_begin"); + Assert.assertEquals(codegen.escapeUnsafeCharacters("=end"), "=_end"); + Assert.assertEquals(codegen.escapeUnsafeCharacters("#{x}"), "\\#{x}"); + } +} diff --git a/samples/client/petstore-security-test/ruby/docs/ModelReturn.md b/samples/client/petstore-security-test/ruby/docs/ModelReturn.md index e12d8622c10..5021c4994d0 100644 --- a/samples/client/petstore-security-test/ruby/docs/ModelReturn.md +++ b/samples/client/petstore-security-test/ruby/docs/ModelReturn.md @@ -5,4 +5,12 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- **_return** | **Integer** | property description */ ' \" =_end -- \\r\\n \\n \\r | [optional] +## Code Sample + +```ruby +require 'Petstore' + +instance = Petstore::ModelReturn.new(_return: null) +``` + diff --git a/samples/client/petstore-security-test/ruby/lib/petstore/api_error.rb b/samples/client/petstore-security-test/ruby/lib/petstore/api_error.rb index 3dc9887bc29..a39ecc654b0 100644 --- a/samples/client/petstore-security-test/ruby/lib/petstore/api_error.rb +++ b/samples/client/petstore-security-test/ruby/lib/petstore/api_error.rb @@ -34,5 +34,24 @@ module Petstore super arg end end + + # Override to_s to display a friendly error message + def to_s + message + end + + def message + if @message.nil? + msg = "Error message: the server returns an error" + else + msg = @message + end + + msg += "\nHTTP status code: #{code}" if code + msg += "\nResponse headers: #{response_headers}" if response_headers + msg += "\nResponse body: #{response_body}" if response_body + + msg + end end end diff --git a/samples/client/petstore-security-test/ruby/lib/petstore/models/model_return.rb b/samples/client/petstore-security-test/ruby/lib/petstore/models/model_return.rb index ac9bf1359c7..0c0bf0dc13b 100644 --- a/samples/client/petstore-security-test/ruby/lib/petstore/models/model_return.rb +++ b/samples/client/petstore-security-test/ruby/lib/petstore/models/model_return.rb @@ -35,13 +35,20 @@ module Petstore # Initializes the object # @param [Hash] attributes Model attributes in the form of hash def initialize(attributes = {}) - return unless attributes.is_a?(Hash) + if (!attributes.is_a?(Hash)) + fail ArgumentError, "The input argument (attributes) must be a hash in `Petstore::ModelReturn` initialize method" + end - # convert string to symbol for hash key - attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v } + # check to see if the attribute exists and convert string to symbol for hash key + attributes = attributes.each_with_object({}) { |(k, v), h| + if (!self.class.attribute_map.key?(k.to_sym)) + fail ArgumentError, "`#{k}` is not a valid attribute in `Petstore::ModelReturn`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect + end + h[k.to_sym] = v + } - if attributes.has_key?(:'return') - self._return = attributes[:'return'] + if attributes.key?(:'_return') + self._return = attributes[:'_return'] end end diff --git a/samples/client/petstore-security-test/ruby/spec/api/fake_api_spec.rb b/samples/client/petstore-security-test/ruby/spec/api/fake_api_spec.rb index 607674de41e..d810979862a 100644 --- a/samples/client/petstore-security-test/ruby/spec/api/fake_api_spec.rb +++ b/samples/client/petstore-security-test/ruby/spec/api/fake_api_spec.rb @@ -19,7 +19,7 @@ require 'json' describe 'FakeApi' do before do # run before each test - @instance = Petstore::FakeApi.new + @api_instance = Petstore::FakeApi.new end after do @@ -28,7 +28,7 @@ describe 'FakeApi' do describe 'test an instance of FakeApi' do it 'should create an instance of FakeApi' do - expect(@instance).to be_instance_of(Petstore::FakeApi) + expect(@api_instance).to be_instance_of(Petstore::FakeApi) end end diff --git a/samples/client/petstore/ruby/spec/api/another_fake_api_spec.rb b/samples/client/petstore/ruby/spec/api/another_fake_api_spec.rb index a884f0dd9f6..2d6b88fcb8f 100644 --- a/samples/client/petstore/ruby/spec/api/another_fake_api_spec.rb +++ b/samples/client/petstore/ruby/spec/api/another_fake_api_spec.rb @@ -19,7 +19,7 @@ require 'json' describe 'AnotherFakeApi' do before do # run before each test - @instance = Petstore::AnotherFakeApi.new + @api_instance = Petstore::AnotherFakeApi.new end after do @@ -28,7 +28,7 @@ describe 'AnotherFakeApi' do describe 'test an instance of AnotherFakeApi' do it 'should create an instance of AnotherFakeApi' do - expect(@instance).to be_instance_of(Petstore::AnotherFakeApi) + expect(@api_instance).to be_instance_of(Petstore::AnotherFakeApi) end end diff --git a/samples/client/petstore/ruby/spec/api/fake_api_spec.rb b/samples/client/petstore/ruby/spec/api/fake_api_spec.rb index a41fa3ff3b1..a7c514fed5d 100644 --- a/samples/client/petstore/ruby/spec/api/fake_api_spec.rb +++ b/samples/client/petstore/ruby/spec/api/fake_api_spec.rb @@ -19,7 +19,7 @@ require 'json' describe 'FakeApi' do before do # run before each test - @instance = Petstore::FakeApi.new + @api_instance = Petstore::FakeApi.new end after do @@ -28,7 +28,7 @@ describe 'FakeApi' do describe 'test an instance of FakeApi' do it 'should create an instance of FakeApi' do - expect(@instance).to be_instance_of(Petstore::FakeApi) + expect(@api_instance).to be_instance_of(Petstore::FakeApi) end end diff --git a/samples/client/petstore/ruby/spec/api/fake_classname_tags123_api_spec.rb b/samples/client/petstore/ruby/spec/api/fake_classname_tags123_api_spec.rb index 6298fa4d485..4f815de3974 100644 --- a/samples/client/petstore/ruby/spec/api/fake_classname_tags123_api_spec.rb +++ b/samples/client/petstore/ruby/spec/api/fake_classname_tags123_api_spec.rb @@ -19,7 +19,7 @@ require 'json' describe 'FakeClassnameTags123Api' do before do # run before each test - @instance = Petstore::FakeClassnameTags123Api.new + @api_instance = Petstore::FakeClassnameTags123Api.new end after do @@ -28,7 +28,7 @@ describe 'FakeClassnameTags123Api' do describe 'test an instance of FakeClassnameTags123Api' do it 'should create an instance of FakeClassnameTags123Api' do - expect(@instance).to be_instance_of(Petstore::FakeClassnameTags123Api) + expect(@api_instance).to be_instance_of(Petstore::FakeClassnameTags123Api) end end diff --git a/samples/client/petstore/ruby/spec/api/pet_api_spec.rb b/samples/client/petstore/ruby/spec/api/pet_api_spec.rb index 578d23af1ca..89bf9c5eb99 100644 --- a/samples/client/petstore/ruby/spec/api/pet_api_spec.rb +++ b/samples/client/petstore/ruby/spec/api/pet_api_spec.rb @@ -19,7 +19,7 @@ require 'json' describe 'PetApi' do before do # run before each test - @instance = Petstore::PetApi.new + @api_instance = Petstore::PetApi.new end after do @@ -28,7 +28,7 @@ describe 'PetApi' do describe 'test an instance of PetApi' do it 'should create an instance of PetApi' do - expect(@instance).to be_instance_of(Petstore::PetApi) + expect(@api_instance).to be_instance_of(Petstore::PetApi) end end diff --git a/samples/client/petstore/ruby/spec/api/store_api_spec.rb b/samples/client/petstore/ruby/spec/api/store_api_spec.rb index c55d5be7984..b00cca76e44 100644 --- a/samples/client/petstore/ruby/spec/api/store_api_spec.rb +++ b/samples/client/petstore/ruby/spec/api/store_api_spec.rb @@ -19,7 +19,7 @@ require 'json' describe 'StoreApi' do before do # run before each test - @instance = Petstore::StoreApi.new + @api_instance = Petstore::StoreApi.new end after do @@ -28,7 +28,7 @@ describe 'StoreApi' do describe 'test an instance of StoreApi' do it 'should create an instance of StoreApi' do - expect(@instance).to be_instance_of(Petstore::StoreApi) + expect(@api_instance).to be_instance_of(Petstore::StoreApi) end end diff --git a/samples/client/petstore/ruby/spec/api/user_api_spec.rb b/samples/client/petstore/ruby/spec/api/user_api_spec.rb index 32228e3af37..5efa8bd15fe 100644 --- a/samples/client/petstore/ruby/spec/api/user_api_spec.rb +++ b/samples/client/petstore/ruby/spec/api/user_api_spec.rb @@ -19,7 +19,7 @@ require 'json' describe 'UserApi' do before do # run before each test - @instance = Petstore::UserApi.new + @api_instance = Petstore::UserApi.new end after do @@ -28,7 +28,7 @@ describe 'UserApi' do describe 'test an instance of UserApi' do it 'should create an instance of UserApi' do - expect(@instance).to be_instance_of(Petstore::UserApi) + expect(@api_instance).to be_instance_of(Petstore::UserApi) end end