Skip to content

Commit

Permalink
[Ruby] Add Flag to Allow Ignoring Operation Servers (#18934)
Browse files Browse the repository at this point in the history
* [Ruby] Add Flag to Allow Ignoring Operation Servers

* update samples

* add tests

* fix tests
  • Loading branch information
ckoegel committed Jun 19, 2024
1 parent 5bc7aa3 commit 9919c23
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ module {{moduleName}}
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -146,6 +154,7 @@ module {{moduleName}}
@configure_session_blocks = []
{{/isHttpx}}
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -180,6 +189,7 @@ module {{moduleName}}

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class Configuration
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -170,6 +178,7 @@ def initialize
@return_binary_data = false
@params_encoder = nil
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -204,6 +213,7 @@ def base_path=(base_path)

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class Configuration
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -134,6 +142,7 @@ def initialize
@timeout = 60
@configure_session_blocks = []
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -168,6 +177,7 @@ def base_path=(base_path)

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class Configuration
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -166,6 +174,7 @@ def initialize
@timeout = 0
@params_encoding = nil
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -200,6 +209,7 @@ def base_path=(base_path)

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class Configuration
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -166,6 +174,7 @@ def initialize
@timeout = 0
@params_encoding = nil
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -200,6 +209,7 @@ def base_path=(base_path)

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down
10 changes: 10 additions & 0 deletions samples/client/petstore/ruby-faraday/lib/petstore/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class Configuration
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -170,6 +178,7 @@ def initialize
@return_binary_data = false
@params_encoder = nil
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -204,6 +213,7 @@ def base_path=(base_path)

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@
expect(config.base_url).to eq('https://localhost:8080/v2')
end

it 'ignores the operation server settings and uses the custom base url' do
config.scheme = 'https'
config.host = 'api.example.com'
config.base_path = '/v2'
config.ignore_operation_servers = true
expect(config.base_url).to eq('https://api.example.com/v2')
expect(config.base_url(:'PetApi.add_pet')).to eq('https://api.example.com/v2')
end

it 'throws argument error when attempting to use a server index that is out of bounds' do
config.server_operation_index = {
:'PetApi.add_pet' => 10
Expand Down
10 changes: 10 additions & 0 deletions samples/client/petstore/ruby-httpx/lib/petstore/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class Configuration
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -134,6 +142,7 @@ def initialize
@timeout = 60
@configure_session_blocks = []
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -168,6 +177,7 @@ def base_path=(base_path)

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down
10 changes: 10 additions & 0 deletions samples/client/petstore/ruby/lib/petstore/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class Configuration
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -166,6 +174,7 @@ def initialize
@timeout = 0
@params_encoding = nil
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -200,6 +209,7 @@ def base_path=(base_path)

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@
expect(config.base_url).to eq('https://localhost:8080/v2')
end

it 'ignores the operation server settings and uses the custom base url' do
config.scheme = 'https'
config.host = 'api.example.com'
config.base_path = '/v2'
config.ignore_operation_servers = true
expect(config.base_url).to eq('https://api.example.com/v2')
expect(config.base_url(:'PetApi.add_pet')).to eq('https://api.example.com/v2')
end

it 'throws argument error when attempting to use a server index that is out of bounds' do
config.server_operation_index = {
:'PetApi.add_pet' => 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class Configuration
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -166,6 +174,7 @@ def initialize
@timeout = 0
@params_encoding = nil
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -200,6 +209,7 @@ def base_path=(base_path)

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class Configuration
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -166,6 +174,7 @@ def initialize
@timeout = 0
@params_encoding = nil
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -200,6 +209,7 @@ def base_path=(base_path)

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ class Configuration
# @return [true, false]
attr_accessor :debugging

# Set this to ignore operation servers for the API client. This is useful when you need to
# send requests to a different server than the one specified in the OpenAPI document.
# Will default to the base url defined in the spec but can be overridden by setting
# `scheme`, `host`, `base_path` directly.
# Default to false.
# @return [true, false]
attr_accessor :ignore_operation_servers

# Defines the logger used for debugging.
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
#
Expand Down Expand Up @@ -166,6 +174,7 @@ def initialize
@timeout = 0
@params_encoding = nil
@debugging = false
@ignore_operation_servers = false
@inject_format = false
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
Expand Down Expand Up @@ -200,6 +209,7 @@ def base_path=(base_path)

# Returns base URL for specified operation based on server settings
def base_url(operation = nil)
return "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '') if ignore_operation_servers
if operation_server_settings.key?(operation) then
index = server_operation_index.fetch(operation, server_index)
server_url(index.nil? ? 0 : index, server_operation_variables.fetch(operation, server_variables), operation_server_settings[operation])
Expand Down

0 comments on commit 9919c23

Please sign in to comment.