mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 12:40:53 +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:
parent
38d189b159
commit
0d05ee35f0
@ -232,7 +232,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
|
||||
|
@ -24,6 +24,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.
|
||||
@ -148,6 +151,7 @@ conf = {{{packageName}}}.Configuration(
|
||||
{{/hasHttpSignatureMethods}}
|
||||
server_index=None, server_variables=None,
|
||||
server_operation_index=None, server_operation_variables=None,
|
||||
ignore_operation_servers=False,
|
||||
ssl_ca_cert=None,
|
||||
retries=None,
|
||||
*,
|
||||
@ -166,6 +170,9 @@ conf = {{{packageName}}}.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
|
||||
"""
|
||||
|
@ -228,7 +228,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
|
||||
|
@ -33,6 +33,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.
|
||||
@ -84,6 +87,7 @@ conf = openapi_client.Configuration(
|
||||
access_token=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,
|
||||
*,
|
||||
@ -102,6 +106,9 @@ conf = openapi_client.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
|
||||
"""
|
||||
|
@ -228,7 +228,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
|
||||
|
@ -33,6 +33,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.
|
||||
@ -84,6 +87,7 @@ conf = openapi_client.Configuration(
|
||||
access_token=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,
|
||||
*,
|
||||
@ -102,6 +106,9 @@ conf = openapi_client.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
|
||||
"""
|
||||
|
@ -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')
|
||||
|
@ -227,7 +227,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
|
||||
|
@ -32,6 +32,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.
|
||||
@ -144,6 +147,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,
|
||||
*,
|
||||
@ -162,6 +166,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
|
||||
"""
|
||||
|
@ -56,6 +56,33 @@ class ApiClientTests(unittest.TestCase):
|
||||
self.assertEqual('test_username', client.configuration.username)
|
||||
self.assertEqual('test_password', client.configuration.password)
|
||||
|
||||
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')
|
||||
|
||||
def test_select_header_accept(self):
|
||||
accepts = ['APPLICATION/JSON', 'APPLICATION/XML']
|
||||
accept = self.api_client.select_header_accept(accepts)
|
||||
|
@ -58,6 +58,18 @@ class TestConfiguration(unittest.TestCase):
|
||||
c1 = petstore_api.Configuration(access_token="12345")
|
||||
self.assertEqual(c1.access_token, "12345")
|
||||
|
||||
def test_ignore_operation_servers(self):
|
||||
self.config.ignore_operation_servers = True
|
||||
self.assertTrue(self.config.ignore_operation_servers)
|
||||
self.config.ignore_operation_servers = False
|
||||
self.assertFalse(self.config.ignore_operation_servers)
|
||||
|
||||
c1 = petstore_api.Configuration(ignore_operation_servers=True)
|
||||
self.assertTrue(c1.ignore_operation_servers)
|
||||
|
||||
c2 = petstore_api.Configuration()
|
||||
self.assertFalse(c2.ignore_operation_servers)
|
||||
|
||||
def test_get_host_settings(self):
|
||||
host_settings = self.config.get_host_settings()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user