[python] honor assert_hostname in the asyncio and httpx clients

YO
5d ago
+79-0

The urllib3 client passes Configuration.assert_hostname to the pool manager, but the asyncio (aiohttp) and httpx clients never read it, so assert_hostname=False has no effect there.

This sets ssl_context.check_hostname from it when verify_ssl is on. With verify_ssl off it is left alone, so certificate verification is not switched back on.

Context: the Kubernetes Python client hit this in its asyncio client (kubernetes-client/python#2795). The maintainers asked for the fix here in the generator rather than as a post-processing patch (kubernetes-client/gen#314).

Changes:

  • python/asyncio/rest.mustache and python/httpx/rest.mustache
  • regenerated rest.py in the python-aiohttp, python-httpx, python-httpx-sync, python-httpx2 and python-httpx2-sync samples
  • tests in the python-aiohttp and python-httpx samples (TestAssertHostname): they fail on master and pass with this change. The rest of each sample suite is unchanged (the pet API tests need a running petstore server).

PR checklist

  • Read the contribution guidelines.
  • Updated samples: regenerated the five affected samples with openapi-generator-cli 7.27.0 and the changed templates (batch bin/configs/<config>.yaml); the generated rest.py files match the committed ones exactly. Only rest.mustache changed, so no other generated file is affected.
  • @mention the technical committee: @cbornet @tomplus @krjakbrjak @fa0311 @AndreyVMarkelov

Summary by cubic

Honors assert_hostname in the asyncio (aiohttp) and httpx clients so assert_hostname=False disables hostname checks there, matching the urllib3 client. This fixes the Kubernetes Python client's asyncio client (kubernetes-client/python#2795). Hostname checking is only applied when verify_ssl is on, so disabling verification never re-enables it.

  • Added tests in the aiohttp and httpx samples covering both cases.