mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-18 22:27:04 +00:00
[Python] Add Flag to Allow Ignoring Operation Servers (#18981)
* [Python] Add Flag to Allow Ignoring Operation Servers * generate samples * add tests
This commit is contained in:
@@ -230,7 +230,7 @@ class ApiClient:
|
||||
body = self.sanitize_for_serialization(body)
|
||||
|
||||
# request url
|
||||
if _host is None:
|
||||
if _host is None or self.configuration.ignore_operation_servers:
|
||||
url = self.configuration.host + resource_path
|
||||
else:
|
||||
# use server/host defined in path or operation instead
|
||||
|
||||
@@ -31,6 +31,9 @@ class Configuration:
|
||||
"""This class contains various settings of the API client.
|
||||
|
||||
:param host: Base url.
|
||||
:param ignore_operation_servers
|
||||
Boolean to ignore operation servers for the API client.
|
||||
Config will use `host` as the base url regardless of the operation servers.
|
||||
:param api_key: Dict to store API key(s).
|
||||
Each entry in the dict specifies an API key.
|
||||
The dict key is the name of the security scheme in the OAS specification.
|
||||
@@ -143,6 +146,7 @@ conf = petstore_api.Configuration(
|
||||
signing_info=None,
|
||||
server_index=None, server_variables=None,
|
||||
server_operation_index=None, server_operation_variables=None,
|
||||
ignore_operation_servers=False,
|
||||
ssl_ca_cert=None,
|
||||
retries=None,
|
||||
*,
|
||||
@@ -161,6 +165,9 @@ conf = petstore_api.Configuration(
|
||||
self.server_operation_variables = server_operation_variables or {}
|
||||
"""Default server variables
|
||||
"""
|
||||
self.ignore_operation_servers = ignore_operation_servers
|
||||
"""Ignore operation servers
|
||||
"""
|
||||
self.temp_folder_path = None
|
||||
"""Temp file folder for downloading files
|
||||
"""
|
||||
|
||||
@@ -8,6 +8,7 @@ import weakref
|
||||
from tests.util import async_test
|
||||
import petstore_api
|
||||
|
||||
HOST = 'http://localhost/v2'
|
||||
|
||||
class TestApiClient(unittest.TestCase):
|
||||
|
||||
@@ -20,3 +21,31 @@ class TestApiClient(unittest.TestCase):
|
||||
rest_pool_ref = client.rest_client.pool_manager
|
||||
|
||||
self.assertTrue(rest_pool_ref.closed)
|
||||
|
||||
@async_test
|
||||
async def test_ignore_operation_servers(self):
|
||||
config = petstore_api.Configuration(host=HOST)
|
||||
client = petstore_api.ApiClient(config)
|
||||
user_api_instance = petstore_api.api.user_api.UserApi(client)
|
||||
|
||||
config_ignore = petstore_api.Configuration(host=HOST, ignore_operation_servers=True)
|
||||
client_ignore = petstore_api.ApiClient(config_ignore)
|
||||
user_api_instance_ignore = petstore_api.api.user_api.UserApi(client_ignore)
|
||||
|
||||
params_to_serialize = {
|
||||
'user': petstore_api.User(id=1, username='test'),
|
||||
'_request_auth': None,
|
||||
'_content_type': 'application/json',
|
||||
'_headers': None,
|
||||
'_host_index': 0
|
||||
}
|
||||
|
||||
# operation servers should be used
|
||||
_, url, *_ = user_api_instance._create_user_serialize(**params_to_serialize)
|
||||
self.assertEqual(client.configuration.host, HOST)
|
||||
self.assertEqual(url, 'http://petstore.swagger.io/v2/user')
|
||||
|
||||
# operation servers should be ignored
|
||||
_, url_ignore, *_ = user_api_instance_ignore._create_user_serialize(**params_to_serialize)
|
||||
self.assertEqual(client.configuration.host, HOST)
|
||||
self.assertEqual(url_ignore, HOST + '/user')
|
||||
|
||||
Reference in New Issue
Block a user