diff --git a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache index 220c8335c36f..ed5d0a6edb07 100644 --- a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache @@ -70,6 +70,11 @@ class RESTClientObject: self.ssl_context.check_hostname = False self.ssl_context.verify_mode = ssl.CERT_NONE + # Honor assert_hostname like the urllib3 client. Only when verify_ssl is + # on: enabling check_hostname would otherwise turn verification back on. + if configuration.verify_ssl and configuration.assert_hostname is not None: + self.ssl_context.check_hostname = bool(configuration.assert_hostname) + self.proxy = configuration.proxy self.proxy_headers = configuration.proxy_headers diff --git a/modules/openapi-generator/src/main/resources/python/httpx/rest.mustache b/modules/openapi-generator/src/main/resources/python/httpx/rest.mustache index 749b7ffbadbf..b86c1bdbe4df 100644 --- a/modules/openapi-generator/src/main/resources/python/httpx/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/httpx/rest.mustache @@ -62,6 +62,11 @@ class RESTClientObject: self.ssl_context.check_hostname = False self.ssl_context.verify_mode = ssl.CERT_NONE + # Honor assert_hostname like the urllib3 client. Only when verify_ssl is + # on: enabling check_hostname would otherwise turn verification back on. + if configuration.verify_ssl and configuration.assert_hostname is not None: + self.ssl_context.check_hostname = bool(configuration.assert_hostname) + self.proxy = configuration.proxy self.proxy_headers = configuration.proxy_headers diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py index f9c8d3b2bab2..f187a0e3cda4 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py @@ -79,6 +79,11 @@ def __init__(self, configuration) -> None: self.ssl_context.check_hostname = False self.ssl_context.verify_mode = ssl.CERT_NONE + # Honor assert_hostname like the urllib3 client. Only when verify_ssl is + # on: enabling check_hostname would otherwise turn verification back on. + if configuration.verify_ssl and configuration.assert_hostname is not None: + self.ssl_context.check_hostname = bool(configuration.assert_hostname) + self.proxy = configuration.proxy self.proxy_headers = configuration.proxy_headers diff --git a/samples/openapi3/client/petstore/python-aiohttp/tests/test_rest.py b/samples/openapi3/client/petstore/python-aiohttp/tests/test_rest.py index 8de3969aa065..9b148b4b1018 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/tests/test_rest.py +++ b/samples/openapi3/client/petstore/python-aiohttp/tests/test_rest.py @@ -1,4 +1,5 @@ import os +import ssl import unittest from unittest.mock import AsyncMock, Mock, patch @@ -72,3 +73,24 @@ async def test_multipart_requests_with_file_and_additional_properties(self): assert type(data_count) is str assert type(data_object) is str + + +class TestAssertHostname(unittest.TestCase): + def test_assert_hostname_false_disables_hostname_check(self): + config = petstore_api.Configuration() + config.assert_hostname = False + + client = petstore_api.rest.RESTClientObject(config) + + self.assertFalse(client.ssl_context.check_hostname) + self.assertEqual(ssl.CERT_REQUIRED, client.ssl_context.verify_mode) + + def test_assert_hostname_does_not_reenable_verification(self): + config = petstore_api.Configuration() + config.verify_ssl = False + config.assert_hostname = True + + client = petstore_api.rest.RESTClientObject(config) + + self.assertFalse(client.ssl_context.check_hostname) + self.assertEqual(ssl.CERT_NONE, client.ssl_context.verify_mode) diff --git a/samples/openapi3/client/petstore/python-httpx-sync/petstore_api/rest.py b/samples/openapi3/client/petstore/python-httpx-sync/petstore_api/rest.py index f28745ec0fe7..3ffe30d29fd4 100644 --- a/samples/openapi3/client/petstore/python-httpx-sync/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-httpx-sync/petstore_api/rest.py @@ -71,6 +71,11 @@ def __init__(self, configuration) -> None: self.ssl_context.check_hostname = False self.ssl_context.verify_mode = ssl.CERT_NONE + # Honor assert_hostname like the urllib3 client. Only when verify_ssl is + # on: enabling check_hostname would otherwise turn verification back on. + if configuration.verify_ssl and configuration.assert_hostname is not None: + self.ssl_context.check_hostname = bool(configuration.assert_hostname) + self.proxy = configuration.proxy self.proxy_headers = configuration.proxy_headers diff --git a/samples/openapi3/client/petstore/python-httpx/petstore_api/rest.py b/samples/openapi3/client/petstore/python-httpx/petstore_api/rest.py index f28745ec0fe7..3ffe30d29fd4 100644 --- a/samples/openapi3/client/petstore/python-httpx/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-httpx/petstore_api/rest.py @@ -71,6 +71,11 @@ def __init__(self, configuration) -> None: self.ssl_context.check_hostname = False self.ssl_context.verify_mode = ssl.CERT_NONE + # Honor assert_hostname like the urllib3 client. Only when verify_ssl is + # on: enabling check_hostname would otherwise turn verification back on. + if configuration.verify_ssl and configuration.assert_hostname is not None: + self.ssl_context.check_hostname = bool(configuration.assert_hostname) + self.proxy = configuration.proxy self.proxy_headers = configuration.proxy_headers diff --git a/samples/openapi3/client/petstore/python-httpx/tests/test_rest.py b/samples/openapi3/client/petstore/python-httpx/tests/test_rest.py index 4507319e8612..c20b2fc29a44 100644 --- a/samples/openapi3/client/petstore/python-httpx/tests/test_rest.py +++ b/samples/openapi3/client/petstore/python-httpx/tests/test_rest.py @@ -1,4 +1,5 @@ import os +import ssl import unittest from unittest.mock import AsyncMock, Mock, patch @@ -73,3 +74,24 @@ async def test_multipart_requests_with_file_and_additional_properties(self): assert type(data_count) is str assert type(data_object) is str + + +class TestAssertHostname(unittest.TestCase): + def test_assert_hostname_false_disables_hostname_check(self): + config = petstore_api.Configuration() + config.assert_hostname = False + + client = petstore_api.rest.RESTClientObject(config) + + self.assertFalse(client.ssl_context.check_hostname) + self.assertEqual(ssl.CERT_REQUIRED, client.ssl_context.verify_mode) + + def test_assert_hostname_does_not_reenable_verification(self): + config = petstore_api.Configuration() + config.verify_ssl = False + config.assert_hostname = True + + client = petstore_api.rest.RESTClientObject(config) + + self.assertFalse(client.ssl_context.check_hostname) + self.assertEqual(ssl.CERT_NONE, client.ssl_context.verify_mode) diff --git a/samples/openapi3/client/petstore/python-httpx2-sync/petstore_api/rest.py b/samples/openapi3/client/petstore/python-httpx2-sync/petstore_api/rest.py index 22dc7db551e3..95c4b0bb5fee 100644 --- a/samples/openapi3/client/petstore/python-httpx2-sync/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-httpx2-sync/petstore_api/rest.py @@ -71,6 +71,11 @@ def __init__(self, configuration) -> None: self.ssl_context.check_hostname = False self.ssl_context.verify_mode = ssl.CERT_NONE + # Honor assert_hostname like the urllib3 client. Only when verify_ssl is + # on: enabling check_hostname would otherwise turn verification back on. + if configuration.verify_ssl and configuration.assert_hostname is not None: + self.ssl_context.check_hostname = bool(configuration.assert_hostname) + self.proxy = configuration.proxy self.proxy_headers = configuration.proxy_headers diff --git a/samples/openapi3/client/petstore/python-httpx2/petstore_api/rest.py b/samples/openapi3/client/petstore/python-httpx2/petstore_api/rest.py index 22dc7db551e3..95c4b0bb5fee 100644 --- a/samples/openapi3/client/petstore/python-httpx2/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-httpx2/petstore_api/rest.py @@ -71,6 +71,11 @@ def __init__(self, configuration) -> None: self.ssl_context.check_hostname = False self.ssl_context.verify_mode = ssl.CERT_NONE + # Honor assert_hostname like the urllib3 client. Only when verify_ssl is + # on: enabling check_hostname would otherwise turn verification back on. + if configuration.verify_ssl and configuration.assert_hostname is not None: + self.ssl_context.check_hostname = bool(configuration.assert_hostname) + self.proxy = configuration.proxy self.proxy_headers = configuration.proxy_headers