Skip to content

Commit 2679819

Browse files
authored
[Ruby] Client: fix base_url when no server_operation_index is defined (OpenAPITools#15162)
As discussed in OpenAPITools#7415 (comment), it seems unlikely the code was correct. server_operation_index is a hash table. In Ruby, `hash[key]` will return the value associated with `key`. If key is absent, `nil` is returned. Because that is sometimes undesirable, there is also `hash.fetch(key)`, which raises an error if the key is absent. It also allows you to specify a default to fall back on: `hash.fetch(key, default)` will return `default` if the key is absent. So, since not all users will specify a 'server per operation' (or at least: I'm not), the old code would usually set `index` to the `server_index`, which is initialized to 0. The subsequent `if index == nil` will usually return false (`0 != nil` in Ruby), after which the `server_url` call on line 177 constructs the url based on the `server_operation_variables` and `operation_server_settings`, assuming we are dealing with the case where a server per operation is configured. The case where the url should be constructed from `scheme`, `host`, etc. is only called if either `server_index` is explicitly set to `nil` or the key `operation` is explicitly associated with the value `nil` in the `server_operation_index` hash table, both of which seem inappropriate.
1 parent e6c64d3 commit 2679819

7 files changed

Lines changed: 7 additions & 7 deletions

File tree

modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ module {{moduleName}}
171171

172172
# Returns base URL for specified operation based on server settings
173173
def base_url(operation = nil)
174-
index = server_operation_index.fetch(operation, server_index)
174+
index = server_operation_index[operation]
175175
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if index == nil
176176

177177
server_url(index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])

samples/client/petstore/ruby-autoload/lib/petstore/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def base_path=(base_path)
200200

201201
# Returns base URL for specified operation based on server settings
202202
def base_url(operation = nil)
203-
index = server_operation_index.fetch(operation, server_index)
203+
index = server_operation_index[operation]
204204
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if index == nil
205205

206206
server_url(index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])

samples/client/petstore/ruby-faraday/lib/petstore/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def base_path=(base_path)
204204

205205
# Returns base URL for specified operation based on server settings
206206
def base_url(operation = nil)
207-
index = server_operation_index.fetch(operation, server_index)
207+
index = server_operation_index[operation]
208208
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if index == nil
209209

210210
server_url(index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])

samples/client/petstore/ruby/lib/petstore/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def base_path=(base_path)
200200

201201
# Returns base URL for specified operation based on server settings
202202
def base_url(operation = nil)
203-
index = server_operation_index.fetch(operation, server_index)
203+
index = server_operation_index[operation]
204204
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if index == nil
205205

206206
server_url(index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])

samples/openapi3/client/extensions/x-auth-id-alias/ruby-client/lib/x_auth_id_alias/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def base_path=(base_path)
200200

201201
# Returns base URL for specified operation based on server settings
202202
def base_url(operation = nil)
203-
index = server_operation_index.fetch(operation, server_index)
203+
index = server_operation_index[operation]
204204
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if index == nil
205205

206206
server_url(index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])

samples/openapi3/client/features/dynamic-servers/ruby/lib/dynamic_servers/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def base_path=(base_path)
200200

201201
# Returns base URL for specified operation based on server settings
202202
def base_url(operation = nil)
203-
index = server_operation_index.fetch(operation, server_index)
203+
index = server_operation_index[operation]
204204
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if index == nil
205205

206206
server_url(index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])

samples/openapi3/client/features/generate-alias-as-model/ruby-client/lib/petstore/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def base_path=(base_path)
200200

201201
# Returns base URL for specified operation based on server settings
202202
def base_url(operation = nil)
203-
index = server_operation_index.fetch(operation, server_index)
203+
index = server_operation_index[operation]
204204
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if index == nil
205205

206206
server_url(index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])

0 commit comments

Comments
 (0)