From 9aa15d9f0946bf80c4273a2dbcb498bbef5aa9c5 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 25 Jul 2023 19:29:06 +0800 Subject: [PATCH] [ruby] Add tests to operation servers in ruby client (#16179) * add tests to operation servers in ruby client * update samples * revert change in spec, remove invalid? check in anyof model * fix hsot table * fix circleci config * fix * fix host table * fix host table * minor fix * fix * reorder * update * fix --- .circleci/config.yml | 15 +- .../ruby-client/partial_anyof_module.mustache | 2 +- ...ith-fake-endpoints-models-for-testing.yaml | 45 +++++ pom.xml | 2 +- .../ruby/lib/petstore/configuration.rb | 183 ++++++++++++++++++ .../ruby/lib/petstore/models/mammal_anyof.rb | 2 +- .../ruby/spec/custom/configuration_spec.rb | 2 +- .../petstore/ruby/spec/custom/pet_spec.rb | 43 ++++ 8 files changed, 287 insertions(+), 7 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2649197674a..bcf8db2b158 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -38,9 +38,18 @@ commands: # a reusable command with parameters - run: command: 'sudo docker info >/dev/null 2>&1 || sudo service docker start; ' - run: - command: |- - printf '127.0.0.1 petstore.swagger.io - ' | sudo tee -a /etc/hosts + command: | + sudo tee -a /etc/hosts \<<< "127.0.0.1 path.v1.test.openapi-generator.tech" + sudo tee -a /etc/hosts \<<< "127.0.0.1 path.v2.test.openapi-generator.tech" + sudo tee -a /etc/hosts \<<< "127.0.0.111 path.v3.test.openapi-generator.tech" + sudo tee -a /etc/hosts \<<< "127.0.0.1 operation.v1.test.openapi-generator.tech" + sudo tee -a /etc/hosts \<<< "127.0.0.1 operation.v2.test.openapi-generator.tech" + sudo tee -a /etc/hosts \<<< "127.0.0.111 operation.v3.test.openapi-generator.tech" + sudo tee -a /etc/hosts \<<< "127.0.0.1 server.v1.test.openapi-generator.tech" + sudo tee -a /etc/hosts \<<< "127.0.0.1 server.v2.test.openapi-generator.tech" + sudo tee -a /etc/hosts \<<< "127.0.0.111 server.v3.test.openapi-generator.tech" + sudo tee -a /etc/hosts \<<< "127.0.0.1 petstore.swagger.io" + cat /etc/hosts # - run: docker pull openapitools/openapi-petstore # - run: docker run -d -e OPENAPI_BASE_PATH=/v3 -e DISABLE_API_KEY=1 -e DISABLE_OAUTH=1 -p 80:8080 openapitools/openapi-petstore - run: docker pull swaggerapi/petstore diff --git a/modules/openapi-generator/src/main/resources/ruby-client/partial_anyof_module.mustache b/modules/openapi-generator/src/main/resources/ruby-client/partial_anyof_module.mustache index ccd63dc0d85..b41dd20a7d0 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/partial_anyof_module.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/partial_anyof_module.mustache @@ -81,7 +81,7 @@ # raise if data contains keys that are not known to the model raise unless (data.keys - const.acceptable_attributes).empty? model = const.build_from_hash(data) - return model if model && model.valid? + return model if model end end end diff --git a/modules/openapi-generator/src/test/resources/3_0/ruby/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/openapi-generator/src/test/resources/3_0/ruby/petstore-with-fake-endpoints-models-for-testing.yaml index 0c8345a80f5..64dc36b3411 100644 --- a/modules/openapi-generator/src/test/resources/3_0/ruby/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/ruby/petstore-with-fake-endpoints-models-for-testing.yaml @@ -48,6 +48,15 @@ paths: - 80 - 8080 default: 80 + - url: 'http://path.{version}.test.openapi-generator.tech/v2' + description: test server with variables + variables: + version: + description: target server + enum: + - 'v1' + - 'v2' + default: 'v2' post: tags: - pet @@ -178,6 +187,33 @@ paths: - 'read:pets' deprecated: true '/pet/{petId}': + servers: + - url: 'http://petstore.swagger.io/v2' + - url: 'http://path-server-test.petstore.local/v2' + - url: 'http://{server}.swagger.io:{port}/v2' + description: test server with variables + variables: + server: + description: target server + enum: + - 'petstore' + - 'qa-petstore' + - 'dev-petstore' + default: 'petstore' + port: + enum: + - 80 + - 8080 + default: 80 + - url: 'http://path.{version}.test.openapi-generator.tech/v2' + description: test server with variables + variables: + version: + description: target server + enum: + - 'v1' + - 'v2' + default: 'v2' get: tags: - pet @@ -1256,6 +1292,15 @@ servers: default: 'v2' - url: https://127.0.0.1/no_varaible description: The local server without variables + - url: http://server.{version}.openapi-generator.tech + description: The openapi-generator test server + variables: + version: + description: target server + enum: + - 'v1' + - 'v2' + default: 'v2' components: requestBodies: UserArray: diff --git a/pom.xml b/pom.xml index e53caffcfc2..8608c0dd160 100644 --- a/pom.xml +++ b/pom.xml @@ -1192,8 +1192,8 @@ - samples/client/petstore/ruby-faraday samples/client/petstore/ruby + samples/client/petstore/ruby-faraday samples/client/petstore/ruby-autoload diff --git a/samples/client/petstore/ruby/lib/petstore/configuration.rb b/samples/client/petstore/ruby/lib/petstore/configuration.rb index 8b2780ab149..830c4c9b7f7 100644 --- a/samples/client/petstore/ruby/lib/petstore/configuration.rb +++ b/samples/client/petstore/ruby/lib/petstore/configuration.rb @@ -316,6 +316,20 @@ module Petstore { url: "https://127.0.0.1/no_varaible", description: "The local server without variables", + }, + { + url: "http://server.{version}.openapi-generator.tech", + description: "The openapi-generator test server", + variables: { + version: { + description: "target server", + default_value: "v2", + enum_values: [ + "v1", + "v2" + ] + } + } } ] end @@ -353,6 +367,114 @@ module Petstore ] } } + }, + { + url: "http://path.{version}.test.openapi-generator.tech/v2", + description: "test server with variables", + variables: { + version: { + description: "target server", + default_value: "v2", + enum_values: [ + "v1", + "v2" + ] + } + } + } + ], + "PetApi.delete_pet": [ + { + url: "http://petstore.swagger.io/v2", + description: "No description provided", + }, + { + url: "http://path-server-test.petstore.local/v2", + description: "No description provided", + }, + { + url: "http://{server}.swagger.io:{port}/v2", + description: "test server with variables", + variables: { + server: { + description: "target server", + default_value: "petstore", + enum_values: [ + "petstore", + "qa-petstore", + "dev-petstore" + ] + }, + port: { + description: "No description provided", + default_value: "80", + enum_values: [ + "80", + "8080" + ] + } + } + }, + { + url: "http://path.{version}.test.openapi-generator.tech/v2", + description: "test server with variables", + variables: { + version: { + description: "target server", + default_value: "v2", + enum_values: [ + "v1", + "v2" + ] + } + } + } + ], + "PetApi.get_pet_by_id": [ + { + url: "http://petstore.swagger.io/v2", + description: "No description provided", + }, + { + url: "http://path-server-test.petstore.local/v2", + description: "No description provided", + }, + { + url: "http://{server}.swagger.io:{port}/v2", + description: "test server with variables", + variables: { + server: { + description: "target server", + default_value: "petstore", + enum_values: [ + "petstore", + "qa-petstore", + "dev-petstore" + ] + }, + port: { + description: "No description provided", + default_value: "80", + enum_values: [ + "80", + "8080" + ] + } + } + }, + { + url: "http://path.{version}.test.openapi-generator.tech/v2", + description: "test server with variables", + variables: { + version: { + description: "target server", + default_value: "v2", + enum_values: [ + "v1", + "v2" + ] + } + } } ], "PetApi.update_pet": [ @@ -386,6 +508,67 @@ module Petstore ] } } + }, + { + url: "http://path.{version}.test.openapi-generator.tech/v2", + description: "test server with variables", + variables: { + version: { + description: "target server", + default_value: "v2", + enum_values: [ + "v1", + "v2" + ] + } + } + } + ], + "PetApi.update_pet_with_form": [ + { + url: "http://petstore.swagger.io/v2", + description: "No description provided", + }, + { + url: "http://path-server-test.petstore.local/v2", + description: "No description provided", + }, + { + url: "http://{server}.swagger.io:{port}/v2", + description: "test server with variables", + variables: { + server: { + description: "target server", + default_value: "petstore", + enum_values: [ + "petstore", + "qa-petstore", + "dev-petstore" + ] + }, + port: { + description: "No description provided", + default_value: "80", + enum_values: [ + "80", + "8080" + ] + } + } + }, + { + url: "http://path.{version}.test.openapi-generator.tech/v2", + description: "test server with variables", + variables: { + version: { + description: "target server", + default_value: "v2", + enum_values: [ + "v1", + "v2" + ] + } + } } ], } diff --git a/samples/client/petstore/ruby/lib/petstore/models/mammal_anyof.rb b/samples/client/petstore/ruby/lib/petstore/models/mammal_anyof.rb index 554b0558617..dc19b174a7f 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/mammal_anyof.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/mammal_anyof.rb @@ -89,7 +89,7 @@ module Petstore # raise if data contains keys that are not known to the model raise unless (data.keys - const.acceptable_attributes).empty? model = const.build_from_hash(data) - return model if model && model.valid? + return model if model end end end diff --git a/samples/client/petstore/ruby/spec/custom/configuration_spec.rb b/samples/client/petstore/ruby/spec/custom/configuration_spec.rb index 59405313127..5ee8f3ea1c4 100644 --- a/samples/client/petstore/ruby/spec/custom/configuration_spec.rb +++ b/samples/client/petstore/ruby/spec/custom/configuration_spec.rb @@ -61,7 +61,7 @@ describe Petstore::Configuration do } expect { config.base_url(:'PetApi.add_pet') - }.to raise_error(ArgumentError, 'Invalid index 10 when selecting the server. Must not be nil and must be less than 3') + }.to raise_error(ArgumentError, 'Invalid index 10 when selecting the server. Must not be nil and must be less than 4') end it 'should remove trailing slashes' do diff --git a/samples/client/petstore/ruby/spec/custom/pet_spec.rb b/samples/client/petstore/ruby/spec/custom/pet_spec.rb index 1fbec557bee..ae2285d7425 100644 --- a/samples/client/petstore/ruby/spec/custom/pet_spec.rb +++ b/samples/client/petstore/ruby/spec/custom/pet_spec.rb @@ -60,6 +60,49 @@ describe "Pet" do expect(pet.category.name).to eq("category test") end + it "should fetch a pet object using invalid operation path" do + # backup index + index_backup = @pet_api.api_client.config.server_operation_index + expect(index_backup).to eq({}) + # test operation index 1 (invalid path) + @pet_api.api_client.config.server_operation_index = { + :'PetApi.get_pet_by_id' => 1 + } + + expect(@pet_api.api_client.config.base_url(:'PetApi.get_pet_by_id')).to eq('http://path-server-test.petstore.local/v2') + + expect { + pet = @pet_api.get_pet_by_id(@pet_id) + }.to raise_error(Petstore::ApiError) # path-server-test.petstore.local is invalid (not defined in host table) + + # restore index + @pet_api.api_client.config.server_operation_index = index_backup + expect(@pet_api.api_client.config.server_operation_index).to eq({}) + end + + it "should fetch a pet object using operation path" do + # backup index + index_backup = @pet_api.api_client.config.server_operation_index + expect(index_backup).to eq({}) + # test operation index 3 + @pet_api.api_client.config.server_operation_index = { + :'PetApi.get_pet_by_id' => 3 + } + + expect(@pet_api.api_client.config.base_url(:'PetApi.get_pet_by_id')).to eq('http://path.v2.test.openapi-generator.tech/v2') + + pet = @pet_api.get_pet_by_id(@pet_id) + expect(pet).to be_a(Petstore::Pet) + expect(pet.id).to eq(@pet_id) + expect(pet.name).to eq("RUBY UNIT TESTING") + expect(pet.tags[0].name).to eq("tag test") + expect(pet.category.name).to eq("category test") + # restore index + @pet_api.api_client.config.server_operation_index = index_backup + expect(@pet_api.api_client.config.server_operation_index).to eq({}) + expect(@pet_api.api_client.config.base_url(:'PetApi.get_pet_by_id')).to eq('http://petstore.swagger.io/v2') + end + it "should fetch a pet object with http info" do pet, status_code, headers = @pet_api.get_pet_by_id_with_http_info(@pet_id) expect(status_code).to eq(200)