From 07de270d3fb160e5c7dd0c3d67e3ccde7d67cf8b Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 20 Oct 2018 10:59:56 +0800 Subject: [PATCH 1/6] update ruby samples with OAS3 spec --- ...ith-fake-endpoints-models-for-testing.yaml | 8 ++++++++ samples/client/petstore/ruby/README.md | 6 ++++++ samples/client/petstore/ruby/docs/FakeApi.md | 8 ++++---- samples/client/petstore/ruby/docs/StoreApi.md | 2 +- samples/client/petstore/ruby/docs/UserApi.md | 8 ++++---- samples/client/petstore/ruby/lib/petstore.rb | 6 ++++++ .../ruby/lib/petstore/api/fake_api.rb | 10 +++++++++- .../ruby/lib/petstore/api/store_api.rb | 2 ++ .../ruby/lib/petstore/api/user_api.rb | 8 ++++++++ .../ruby/lib/petstore/models/body4.rb | 12 ++++++------ .../ruby/lib/petstore/models/format_test.rb | 19 ------------------- 11 files changed, 54 insertions(+), 35 deletions(-) diff --git a/modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml index 78ba11aaebc0..cac57e392e1c 100644 --- a/modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml @@ -969,6 +969,14 @@ paths: - requiredFile servers: - url: 'http://petstore.swagger.io:80/v2' + - url: https://api.gigantic-server.com:8080/{version} + description: The production API server + variables: + version: + enum: + - 'v1' + - 'v2' + default: 'v2' components: requestBodies: UserArray: diff --git a/samples/client/petstore/ruby/README.md b/samples/client/petstore/ruby/README.md index 0bc344953b2e..b6b342f3819c 100644 --- a/samples/client/petstore/ruby/README.md +++ b/samples/client/petstore/ruby/README.md @@ -118,6 +118,12 @@ Class | Method | HTTP request | Description - [Petstore::ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md) - [Petstore::ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md) - [Petstore::ArrayTest](docs/ArrayTest.md) + - [Petstore::Body](docs/Body.md) + - [Petstore::Body1](docs/Body1.md) + - [Petstore::Body2](docs/Body2.md) + - [Petstore::Body3](docs/Body3.md) + - [Petstore::Body4](docs/Body4.md) + - [Petstore::Body5](docs/Body5.md) - [Petstore::Capitalization](docs/Capitalization.md) - [Petstore::Cat](docs/Cat.md) - [Petstore::Category](docs/Category.md) diff --git a/samples/client/petstore/ruby/docs/FakeApi.md b/samples/client/petstore/ruby/docs/FakeApi.md index b27a909ce86d..a0a9023412de 100644 --- a/samples/client/petstore/ruby/docs/FakeApi.md +++ b/samples/client/petstore/ruby/docs/FakeApi.md @@ -58,7 +58,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: */* @@ -104,7 +104,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: */* @@ -150,7 +150,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: */* @@ -196,7 +196,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: */* diff --git a/samples/client/petstore/ruby/docs/StoreApi.md b/samples/client/petstore/ruby/docs/StoreApi.md index 2ebdc4ddc0ed..464c8dbb854d 100644 --- a/samples/client/petstore/ruby/docs/StoreApi.md +++ b/samples/client/petstore/ruby/docs/StoreApi.md @@ -185,7 +185,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: application/xml, application/json diff --git a/samples/client/petstore/ruby/docs/UserApi.md b/samples/client/petstore/ruby/docs/UserApi.md index ae8f3fad0f13..8981435d9d9a 100644 --- a/samples/client/petstore/ruby/docs/UserApi.md +++ b/samples/client/petstore/ruby/docs/UserApi.md @@ -53,7 +53,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: Not defined @@ -95,7 +95,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: Not defined @@ -137,7 +137,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: Not defined @@ -353,7 +353,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: Not defined diff --git a/samples/client/petstore/ruby/lib/petstore.rb b/samples/client/petstore/ruby/lib/petstore.rb index d5d034f8f3f9..385c8fbce7ed 100644 --- a/samples/client/petstore/ruby/lib/petstore.rb +++ b/samples/client/petstore/ruby/lib/petstore.rb @@ -24,6 +24,12 @@ require 'petstore/models/array_of_array_of_number_only' require 'petstore/models/array_of_number_only' require 'petstore/models/array_test' +require 'petstore/models/body' +require 'petstore/models/body1' +require 'petstore/models/body2' +require 'petstore/models/body3' +require 'petstore/models/body4' +require 'petstore/models/body5' require 'petstore/models/capitalization' require 'petstore/models/cat' require 'petstore/models/category' diff --git a/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb b/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb index cc1bdddf5ea8..9a03e8be6a6e 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb @@ -46,6 +46,8 @@ def fake_outer_boolean_serialize_with_http_info(opts = {}) header_params = {} # HTTP header 'Accept' (if needed) header_params['Accept'] = @api_client.select_header_accept(['*/*']) + # HTTP header 'Content-Type' + header_params['Content-Type'] = @api_client.select_header_content_type(['application/json']) # form parameters form_params = {} @@ -93,6 +95,8 @@ def fake_outer_composite_serialize_with_http_info(opts = {}) header_params = {} # HTTP header 'Accept' (if needed) header_params['Accept'] = @api_client.select_header_accept(['*/*']) + # HTTP header 'Content-Type' + header_params['Content-Type'] = @api_client.select_header_content_type(['application/json']) # form parameters form_params = {} @@ -140,6 +144,8 @@ def fake_outer_number_serialize_with_http_info(opts = {}) header_params = {} # HTTP header 'Accept' (if needed) header_params['Accept'] = @api_client.select_header_accept(['*/*']) + # HTTP header 'Content-Type' + header_params['Content-Type'] = @api_client.select_header_content_type(['application/json']) # form parameters form_params = {} @@ -187,6 +193,8 @@ def fake_outer_string_serialize_with_http_info(opts = {}) header_params = {} # HTTP header 'Accept' (if needed) header_params['Accept'] = @api_client.select_header_accept(['*/*']) + # HTTP header 'Content-Type' + header_params['Content-Type'] = @api_client.select_header_content_type(['application/json']) # form parameters form_params = {} @@ -585,7 +593,7 @@ def test_enum_parameters_with_http_info(opts = {}) # query parameters query_params = {} - query_params[:'enum_query_string_array'] = @api_client.build_collection_param(opts[:'enum_query_string_array'], :csv) if !opts[:'enum_query_string_array'].nil? + query_params[:'enum_query_string_array'] = @api_client.build_collection_param(opts[:'enum_query_string_array'], :multi) if !opts[:'enum_query_string_array'].nil? query_params[:'enum_query_string'] = opts[:'enum_query_string'] if !opts[:'enum_query_string'].nil? query_params[:'enum_query_integer'] = opts[:'enum_query_integer'] if !opts[:'enum_query_integer'].nil? query_params[:'enum_query_double'] = opts[:'enum_query_double'] if !opts[:'enum_query_double'].nil? 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 a7a77e842e70..55d208a244cf 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/store_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/store_api.rb @@ -208,6 +208,8 @@ def place_order_with_http_info(order, opts = {}) header_params = {} # HTTP header 'Accept' (if needed) header_params['Accept'] = @api_client.select_header_accept(['application/xml', 'application/json']) + # HTTP header 'Content-Type' + header_params['Content-Type'] = @api_client.select_header_content_type(['application/json']) # form parameters form_params = {} 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 95fd5fe257f8..bbacd07c4536 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/user_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/user_api.rb @@ -50,6 +50,8 @@ def create_user_with_http_info(user, opts = {}) # header parameters header_params = {} + # HTTP header 'Content-Type' + header_params['Content-Type'] = @api_client.select_header_content_type(['application/json']) # form parameters form_params = {} @@ -98,6 +100,8 @@ def create_users_with_array_input_with_http_info(user, opts = {}) # header parameters header_params = {} + # HTTP header 'Content-Type' + header_params['Content-Type'] = @api_client.select_header_content_type(['application/json']) # form parameters form_params = {} @@ -146,6 +150,8 @@ def create_users_with_list_input_with_http_info(user, opts = {}) # header parameters header_params = {} + # HTTP header 'Content-Type' + header_params['Content-Type'] = @api_client.select_header_content_type(['application/json']) # form parameters form_params = {} @@ -404,6 +410,8 @@ def update_user_with_http_info(username, user, opts = {}) # header parameters header_params = {} + # HTTP header 'Content-Type' + header_params['Content-Type'] = @api_client.select_header_content_type(['application/json']) # form parameters form_params = {} diff --git a/samples/client/petstore/ruby/lib/petstore/models/body4.rb b/samples/client/petstore/ruby/lib/petstore/models/body4.rb index 7c0387d9b371..7b0954a6061d 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/body4.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/body4.rb @@ -1,12 +1,12 @@ =begin -#Swagger Petstore +#OpenAPI Petstore #This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ OpenAPI spec version: 1.0.0 -Contact: apiteam@swagger.io -Generated by: https://github.com/swagger-api/swagger-codegen.git -Swagger Codegen version: 3.0.0-SNAPSHOT + +Generated by: https://openapi-generator.tech +OpenAPI Generator version: 3.3.2-SNAPSHOT =end @@ -29,7 +29,7 @@ def self.attribute_map end # Attribute type mapping. - def self.swagger_types + def self.openapi_types { :'param' => :'String', :'param2' => :'String' @@ -102,7 +102,7 @@ def hash # @return [Object] Returns the model itself def build_from_hash(attributes) return nil unless attributes.is_a?(Hash) - self.class.swagger_types.each_pair do |key, type| + self.class.openapi_types.each_pair do |key, type| if type =~ /\AArray<(.*)>/i # check to ensure the input is an array given that the the attribute # is documented as an array but the input is not diff --git a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb index 98a903e3123f..5539c10c6c7e 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/format_test.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/format_test.rb @@ -195,10 +195,6 @@ def list_invalid_properties invalid_properties.push('invalid value for "byte", byte cannot be nil.') end - if @byte !~ Regexp.new(/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/) - invalid_properties.push('invalid value for "byte", must conform to the pattern /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/.') - end - if @date.nil? invalid_properties.push('invalid value for "date", date cannot be nil.') end @@ -234,7 +230,6 @@ def valid? return false if !@double.nil? && @double < 67.8 return false if !@string.nil? && @string !~ Regexp.new(/[a-z]/i) return false if @byte.nil? - return false if @byte !~ Regexp.new(/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/) return false if @date.nil? return false if @password.nil? return false if @password.to_s.length > 64 @@ -326,20 +321,6 @@ def string=(string) @string = string end - # Custom attribute writer method with validation - # @param [Object] byte Value to be assigned - def byte=(byte) - if byte.nil? - fail ArgumentError, 'byte cannot be nil' - end - - if byte !~ Regexp.new(/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/) - fail ArgumentError, 'invalid value for "byte", must conform to the pattern /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/.' - end - - @byte = byte - end - # Custom attribute writer method with validation # @param [Object] password Value to be assigned def password=(password) From f13e7584832ca9024c39225059a0604495a098ca Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 21 Oct 2018 01:17:58 +0800 Subject: [PATCH 2/6] add server support to ruby api client --- .../codegen/utils/URLPathUtils.java | 2 +- .../ruby-client/configuration.mustache | 66 ++++++++++++++++ ...ith-fake-endpoints-models-for-testing.yaml | 15 +++- .../ruby/lib/petstore/configuration.rb | 76 +++++++++++++++++++ .../petstore/ruby/spec/configuration_spec.rb | 26 +++++++ 5 files changed, 183 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java index fdfa6fdfd6bc..26030abbe1b2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java @@ -168,7 +168,7 @@ public static String getProtocolAndHost(URL url) { */ public static String getHost(OpenAPI openAPI) { if (openAPI.getServers() != null && openAPI.getServers().size() > 0) { - return sanitizeUrl(openAPI.getServers().get(0).getUrl()); + return sanitizeUrl(getServerURL(openAPI.getServers().get(0)).toString()); } return LOCAL_HOST; } diff --git a/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache b/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache index 7c7e1d82dc5a..e00274aa1012 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache @@ -219,5 +219,71 @@ module {{moduleName}} {{/authMethods}} } end + + # Returns an array of Server setting + def server_settings + [ + {{#servers}} + { + url: "{{{url}}}", + description: "{{{description}}}{{^description}}No descriptoin provided{{/description}}", + {{#variables}} + {{#-first}} + variables: { + {{/-first}} + {{{name}}}: { + description: "{{{description}}}{{^description}}No descriptoin provided{{/description}}", + default_value: "{{{defaultValue}}}", + {{#enumValues}} + {{#-first}} + enum_values: [ + {{/-first}} + "{{{.}}}"{{^-last}},{{/-last}} + {{#-last}} + ] + {{/-last}} + {{/enumValues}} + }{{^-last}},{{/-last}} + {{#-last}} + } + {{/-last}} + {{/variables}} + }{{^-last}},{{/-last}} + {{/servers}} + ] + end + + # Returns URL based on server settings + # + # @param index array index of the server settings + # @param variables hash of variable and the corresponding value + def server_url(index, variables={}) + servers = server_settings + + # check array index out of bound + if (index < 0 || index > servers.size) + raise_error(ArgumentError) + end + + server = servers[index] + url = server[:url] + + # go through variable and assign a value + server[:variables].each do |name, variable| + if variables.key?(name) + if (server[:variables][name][:enum_values].include? variables[name]) + url.gsub! "{" + name.to_s + "}", variables[name] + else + raise_error(ArgumentError) + end + else + # use default value + url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value] + end + end + + url + end + end end diff --git a/modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml index cac57e392e1c..8614ee5b56e3 100644 --- a/modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml @@ -968,7 +968,20 @@ paths: required: - requiredFile servers: - - url: 'http://petstore.swagger.io:80/v2' + - url: 'http://{server}.swagger.io:{port}/v2' + description: petstore server + variables: + server: + enum: + - 'petstore' + - 'qa-petstore' + - 'dev-petstore' + default: 'petstore' + port: + enum: + - 80 + - 8080 + default: 80 - url: https://api.gigantic-server.com:8080/{version} description: The production API server variables: diff --git a/samples/client/petstore/ruby/lib/petstore/configuration.rb b/samples/client/petstore/ruby/lib/petstore/configuration.rb index 8811d01ffb92..b6a39db7aa07 100644 --- a/samples/client/petstore/ruby/lib/petstore/configuration.rb +++ b/samples/client/petstore/ruby/lib/petstore/configuration.rb @@ -226,5 +226,81 @@ def auth_settings }, } end + + # Returns an array of Server setting + def server_settings + [ + { + url: "http://{server}.swagger.io:{port}/v2", + description: "petstore server", + variables: { + server: { + description: "No descriptoin provided", + default_value: "petstore", + enum_values: [ + "petstore", + "qa-petstore", + "dev-petstore" + ] + }, + port: { + description: "No descriptoin provided", + default_value: "80", + enum_values: [ + "80", + "8080" + ] + } + } + }, + { + url: "https://api.gigantic-server.com:8080/{version}", + description: "The production API server", + variables: { + version: { + description: "No descriptoin provided", + default_value: "v2", + enum_values: [ + "v1", + "v2" + ] + } + } + } + ] + end + + # Returns URL based on server settings + # + # @param index array index of the server settings + # @param variables hash of variable and the corresponding value + def server_url(index, variables={}) + servers = server_settings + + # check array index out of bound + if (index < 0 || index > servers.size) + raise_error(ArgumentError) + end + + server = servers[index] + url = server[:url] + + # go through variable and assign a value + server[:variables].each do |name, variable| + if variables.key?(name) + if (server[:variables][name][:enum_values].include? variables[name]) + url.gsub! "{" + name.to_s + "}", variables[name] + else + raise_error(ArgumentError) + end + else + # use default value + url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value] + end + end + + url + end + end end diff --git a/samples/client/petstore/ruby/spec/configuration_spec.rb b/samples/client/petstore/ruby/spec/configuration_spec.rb index 45637e30d6d0..9db30bd0f500 100644 --- a/samples/client/petstore/ruby/spec/configuration_spec.rb +++ b/samples/client/petstore/ruby/spec/configuration_spec.rb @@ -22,4 +22,30 @@ end end end + + describe 'server settings' do + it 'should return an array of server settings' do + expect(config.auth_settings).not_to be_empty + end + + it 'should get the first url' do + url = config.server_url(0, {server: "dev-petstore", port: "8080"}) + expect(url).to eq("http://dev-petstore.swagger.io:8080/v2") + end + + it 'should get the first url with default values' do + url = config.server_url(0) + expect(url).to eq("http://petstore.swagger.io:80/v2") + end + + it 'should get the second url with default values' do + url = config.server_url(1) + expect(url).to eq("https://api.gigantic-server.com:8080/v2") + end + + it 'should get the second url' do + url = config.server_url(1, {version: "v1"}) + expect(url).to eq("https://api.gigantic-server.com:8080/v1") + end + end end From 651bc02a8f20540d1e56130ceb32001738b668b5 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 21 Oct 2018 01:30:32 +0800 Subject: [PATCH 3/6] minor format change --- bin/utils/ensure-up-to-date | 2 +- .../ruby-client/configuration.mustache | 45 +++++++++---------- .../ruby/lib/petstore/api/another_fake_api.rb | 1 - .../ruby/lib/petstore/api/fake_api.rb | 1 - .../api/fake_classname_tags123_api.rb | 1 - .../petstore/ruby/lib/petstore/api/pet_api.rb | 1 - .../ruby/lib/petstore/api/store_api.rb | 1 - .../ruby/lib/petstore/api/user_api.rb | 1 - .../ruby/lib/petstore/configuration.rb | 45 +++++++++---------- .../petstore/ruby/spec/configuration_spec.rb | 4 +- 10 files changed, 47 insertions(+), 55 deletions(-) diff --git a/bin/utils/ensure-up-to-date b/bin/utils/ensure-up-to-date index 7ce762d807ed..f7b86a587f9e 100755 --- a/bin/utils/ensure-up-to-date +++ b/bin/utils/ensure-up-to-date @@ -10,7 +10,7 @@ echo "Please press CTRL+C to stop or the script will continue in 5 seconds." sleep 5 # LIST OF SCRIPTS: -./bin/ruby-client-petstore.sh > /dev/null 2>&1 +./bin/openapi3/ruby-client-petstore.sh > /dev/null 2>&1 ./bin/java-petstore-all.sh > /dev/null 2>&1 ./bin/java-jaxrs-petstore-server-all.sh > /dev/null 2>&1 ./bin/spring-all-pestore.sh > /dev/null 2>&1 diff --git a/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache b/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache index e00274aa1012..3534fcc28bed 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache @@ -257,33 +257,32 @@ module {{moduleName}} # # @param index array index of the server settings # @param variables hash of variable and the corresponding value - def server_url(index, variables={}) - servers = server_settings + def server_url(index, variables = {}) + servers = server_settings - # check array index out of bound - if (index < 0 || index > servers.size) - raise_error(ArgumentError) - end + # check array index out of bound + if (index < 0 || index > servers.size) + raise_error(ArgumentError) + end - server = servers[index] - url = server[:url] - - # go through variable and assign a value - server[:variables].each do |name, variable| - if variables.key?(name) - if (server[:variables][name][:enum_values].include? variables[name]) - url.gsub! "{" + name.to_s + "}", variables[name] - else - raise_error(ArgumentError) - end - else - # use default value - url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value] - end + server = servers[index] + url = server[:url] + + # go through variable and assign a value + server[:variables].each do |name, variable| + if variables.key?(name) + if (server[:variables][name][:enum_values].include? variables[name]) + url.gsub! "{" + name.to_s + "}", variables[name] + else + raise_error(ArgumentError) + end + else + # use default value + url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value] end + end - url + url end - end 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 497d7f3b1316..9658d1f0023d 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 @@ -73,6 +73,5 @@ def call_123_test_special_tags_with_http_info(client, opts = {}) end return data, status_code, headers end - end end diff --git a/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb b/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb index 9a03e8be6a6e..92bc7b467bd1 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb @@ -732,6 +732,5 @@ def test_json_form_data_with_http_info(param, param2, opts = {}) end return data, status_code, headers end - end end diff --git a/samples/client/petstore/ruby/lib/petstore/api/fake_classname_tags123_api.rb b/samples/client/petstore/ruby/lib/petstore/api/fake_classname_tags123_api.rb index 74847bd4a2b6..68f2296943ac 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/fake_classname_tags123_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/fake_classname_tags123_api.rb @@ -73,6 +73,5 @@ def test_classname_with_http_info(client, opts = {}) end return data, status_code, headers end - end end 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 70c7b7a0d84a..c227fea2c088 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb @@ -508,6 +508,5 @@ def upload_file_with_required_file_with_http_info(pet_id, required_file, opts = 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 55d208a244cf..48bbaac7dbf3 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/store_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/store_api.rb @@ -229,6 +229,5 @@ def place_order_with_http_info(order, opts = {}) end return data, status_code, headers 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 bbacd07c4536..9651e33756ea 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/user_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/user_api.rb @@ -430,6 +430,5 @@ def update_user_with_http_info(username, user, opts = {}) end return data, status_code, headers end - end end diff --git a/samples/client/petstore/ruby/lib/petstore/configuration.rb b/samples/client/petstore/ruby/lib/petstore/configuration.rb index b6a39db7aa07..c83f22c2e125 100644 --- a/samples/client/petstore/ruby/lib/petstore/configuration.rb +++ b/samples/client/petstore/ruby/lib/petstore/configuration.rb @@ -274,33 +274,32 @@ def server_settings # # @param index array index of the server settings # @param variables hash of variable and the corresponding value - def server_url(index, variables={}) - servers = server_settings + def server_url(index, variables = {}) + servers = server_settings - # check array index out of bound - if (index < 0 || index > servers.size) - raise_error(ArgumentError) - end + # check array index out of bound + if (index < 0 || index > servers.size) + raise_error(ArgumentError) + end - server = servers[index] - url = server[:url] - - # go through variable and assign a value - server[:variables].each do |name, variable| - if variables.key?(name) - if (server[:variables][name][:enum_values].include? variables[name]) - url.gsub! "{" + name.to_s + "}", variables[name] - else - raise_error(ArgumentError) - end - else - # use default value - url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value] - end + server = servers[index] + url = server[:url] + + # go through variable and assign a value + server[:variables].each do |name, variable| + if variables.key?(name) + if (server[:variables][name][:enum_values].include? variables[name]) + url.gsub! "{" + name.to_s + "}", variables[name] + else + raise_error(ArgumentError) + end + else + # use default value + url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value] end + end - url + url end - end end diff --git a/samples/client/petstore/ruby/spec/configuration_spec.rb b/samples/client/petstore/ruby/spec/configuration_spec.rb index 9db30bd0f500..5ab2c26d612f 100644 --- a/samples/client/petstore/ruby/spec/configuration_spec.rb +++ b/samples/client/petstore/ruby/spec/configuration_spec.rb @@ -29,7 +29,7 @@ end it 'should get the first url' do - url = config.server_url(0, {server: "dev-petstore", port: "8080"}) + url = config.server_url(0, server: "dev-petstore", port: "8080") expect(url).to eq("http://dev-petstore.swagger.io:8080/v2") end @@ -44,7 +44,7 @@ end it 'should get the second url' do - url = config.server_url(1, {version: "v1"}) + url = config.server_url(1, version: "v1") expect(url).to eq("https://api.gigantic-server.com:8080/v1") end end From ade09aa7227e6dd028b72551ad156d6fbca58209 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 21 Oct 2018 02:12:19 +0800 Subject: [PATCH 4/6] minor format fix, skip form models --- bin/openapi3/ruby-client-petstore.sh | 2 +- .../src/main/resources/ruby-client/configuration.mustache | 2 +- samples/client/petstore/ruby/README.md | 6 ------ samples/client/petstore/ruby/lib/petstore.rb | 6 ------ .../petstore/ruby/lib/petstore/api/another_fake_api.rb | 1 + samples/client/petstore/ruby/lib/petstore/api/fake_api.rb | 1 + .../ruby/lib/petstore/api/fake_classname_tags123_api.rb | 1 + samples/client/petstore/ruby/lib/petstore/api/pet_api.rb | 1 + samples/client/petstore/ruby/lib/petstore/api/store_api.rb | 1 + samples/client/petstore/ruby/lib/petstore/api/user_api.rb | 1 + 10 files changed, 8 insertions(+), 14 deletions(-) diff --git a/bin/openapi3/ruby-client-petstore.sh b/bin/openapi3/ruby-client-petstore.sh index 76c321582b00..67420ee1df5c 100755 --- a/bin/openapi3/ruby-client-petstore.sh +++ b/bin/openapi3/ruby-client-petstore.sh @@ -27,6 +27,6 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/ruby-client -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -g ruby -c bin/ruby-petstore.json -o samples/client/petstore/ruby $@" +ags="generate -t modules/openapi-generator/src/main/resources/ruby-client -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -g ruby -c bin/ruby-petstore.json -o samples/client/petstore/ruby -DskipFormModel=true $@" java $JAVA_OPTS -jar $executable $ags diff --git a/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache b/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache index 3534fcc28bed..0f2f52833e12 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache @@ -278,7 +278,7 @@ module {{moduleName}} end else # use default value - url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value] + url.gsub! "{" + name.to_s + "}", server[:variables][name][:default_value] end end diff --git a/samples/client/petstore/ruby/README.md b/samples/client/petstore/ruby/README.md index b6b342f3819c..0bc344953b2e 100644 --- a/samples/client/petstore/ruby/README.md +++ b/samples/client/petstore/ruby/README.md @@ -118,12 +118,6 @@ Class | Method | HTTP request | Description - [Petstore::ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md) - [Petstore::ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md) - [Petstore::ArrayTest](docs/ArrayTest.md) - - [Petstore::Body](docs/Body.md) - - [Petstore::Body1](docs/Body1.md) - - [Petstore::Body2](docs/Body2.md) - - [Petstore::Body3](docs/Body3.md) - - [Petstore::Body4](docs/Body4.md) - - [Petstore::Body5](docs/Body5.md) - [Petstore::Capitalization](docs/Capitalization.md) - [Petstore::Cat](docs/Cat.md) - [Petstore::Category](docs/Category.md) diff --git a/samples/client/petstore/ruby/lib/petstore.rb b/samples/client/petstore/ruby/lib/petstore.rb index 385c8fbce7ed..d5d034f8f3f9 100644 --- a/samples/client/petstore/ruby/lib/petstore.rb +++ b/samples/client/petstore/ruby/lib/petstore.rb @@ -24,12 +24,6 @@ require 'petstore/models/array_of_array_of_number_only' require 'petstore/models/array_of_number_only' require 'petstore/models/array_test' -require 'petstore/models/body' -require 'petstore/models/body1' -require 'petstore/models/body2' -require 'petstore/models/body3' -require 'petstore/models/body4' -require 'petstore/models/body5' require 'petstore/models/capitalization' require 'petstore/models/cat' require 'petstore/models/category' 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 9658d1f0023d..497d7f3b1316 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 @@ -73,5 +73,6 @@ def call_123_test_special_tags_with_http_info(client, opts = {}) end return data, status_code, headers end + end end diff --git a/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb b/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb index 92bc7b467bd1..9a03e8be6a6e 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/fake_api.rb @@ -732,5 +732,6 @@ def test_json_form_data_with_http_info(param, param2, opts = {}) end return data, status_code, headers end + end end diff --git a/samples/client/petstore/ruby/lib/petstore/api/fake_classname_tags123_api.rb b/samples/client/petstore/ruby/lib/petstore/api/fake_classname_tags123_api.rb index 68f2296943ac..74847bd4a2b6 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/fake_classname_tags123_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/fake_classname_tags123_api.rb @@ -73,5 +73,6 @@ def test_classname_with_http_info(client, opts = {}) end return data, status_code, headers end + end end 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 c227fea2c088..70c7b7a0d84a 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/pet_api.rb @@ -508,5 +508,6 @@ def upload_file_with_required_file_with_http_info(pet_id, required_file, opts = 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 48bbaac7dbf3..55d208a244cf 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/store_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/store_api.rb @@ -229,5 +229,6 @@ def place_order_with_http_info(order, opts = {}) end return data, status_code, headers 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 9651e33756ea..bbacd07c4536 100644 --- a/samples/client/petstore/ruby/lib/petstore/api/user_api.rb +++ b/samples/client/petstore/ruby/lib/petstore/api/user_api.rb @@ -430,5 +430,6 @@ def update_user_with_http_info(username, user, opts = {}) end return data, status_code, headers end + end end From 8b4c8258b94559cb566025919653b51f2138087b Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 21 Oct 2018 18:02:07 +0800 Subject: [PATCH 5/6] better exception and add test for invaid value --- .../src/main/resources/ruby-client/configuration.mustache | 4 ++-- samples/client/petstore/ruby/lib/petstore/configuration.rb | 4 ++-- samples/client/petstore/ruby/spec/configuration_spec.rb | 5 +++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache b/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache index 0f2f52833e12..dcfb00042289 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache @@ -262,7 +262,7 @@ module {{moduleName}} # check array index out of bound if (index < 0 || index > servers.size) - raise_error(ArgumentError) + fail ArgumentError "Invalid index #{index} when selecting the server. Must be less than #{servers.size}" end server = servers[index] @@ -274,7 +274,7 @@ module {{moduleName}} if (server[:variables][name][:enum_values].include? variables[name]) url.gsub! "{" + name.to_s + "}", variables[name] else - raise_error(ArgumentError) + fail ArgumentError, "The variable `#{name}` in the server URL has invalid value #{variables[name]}. Must be #{server[:variables][name][:enum_values]}." end else # use default value diff --git a/samples/client/petstore/ruby/lib/petstore/configuration.rb b/samples/client/petstore/ruby/lib/petstore/configuration.rb index c83f22c2e125..63b8dce9e59f 100644 --- a/samples/client/petstore/ruby/lib/petstore/configuration.rb +++ b/samples/client/petstore/ruby/lib/petstore/configuration.rb @@ -279,7 +279,7 @@ def server_url(index, variables = {}) # check array index out of bound if (index < 0 || index > servers.size) - raise_error(ArgumentError) + fail ArgumentError "Invalid index #{index} when selecting the server. Must be less than #{servers.size}" end server = servers[index] @@ -291,7 +291,7 @@ def server_url(index, variables = {}) if (server[:variables][name][:enum_values].include? variables[name]) url.gsub! "{" + name.to_s + "}", variables[name] else - raise_error(ArgumentError) + fail ArgumentError, "The variable `#{name}` in the server URL has invalid value #{variables[name]}. Must be #{server[:variables][name][:enum_values]}." end else # use default value diff --git a/samples/client/petstore/ruby/spec/configuration_spec.rb b/samples/client/petstore/ruby/spec/configuration_spec.rb index 5ab2c26d612f..f288a4be8376 100644 --- a/samples/client/petstore/ruby/spec/configuration_spec.rb +++ b/samples/client/petstore/ruby/spec/configuration_spec.rb @@ -47,5 +47,10 @@ url = config.server_url(1, version: "v1") expect(url).to eq("https://api.gigantic-server.com:8080/v1") end + + it 'should raise error due to invalid enum value' do + url = config.server_url(1, version: "v6") + expect{config.server_url(1, version: "v6")}.to raise_error(ArgumentError) + end end end From 22a4a07b52d54a983d854a4dd1af50fff2225152 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 21 Oct 2018 18:36:45 +0800 Subject: [PATCH 6/6] remove exception test code --- samples/client/petstore/ruby/spec/configuration_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/client/petstore/ruby/spec/configuration_spec.rb b/samples/client/petstore/ruby/spec/configuration_spec.rb index f288a4be8376..e7f9eb397270 100644 --- a/samples/client/petstore/ruby/spec/configuration_spec.rb +++ b/samples/client/petstore/ruby/spec/configuration_spec.rb @@ -49,7 +49,6 @@ end it 'should raise error due to invalid enum value' do - url = config.server_url(1, version: "v6") expect{config.server_url(1, version: "v6")}.to raise_error(ArgumentError) end end