This is an automated email from the ASF dual-hosted git repository.

vatsrahul1001 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new f97e74a2527 Fix dag.test() failing for task ids with non-ASCII 
characters (#74472)
f97e74a2527 is described below

commit f97e74a252790f571923bf4cb38ef727ea0cb949
Author: Per Andersen <[email protected]>
AuthorDate: Fri Nov 27 12:35:41 2026 +0100

    Fix dag.test() failing for task ids with non-ASCII characters (#74472)
    
    * Fix dag.test() failing for task ids with non-ASCII characters
    
    A task id like "Købsmoms" passes task id validation, but dag.test() failed 
as
    soon as such a task pushed an XCom. In-process execution sends requests
    through httpx.WSGITransport, which puts the decoded URL path into PATH_INFO
    as a plain str. a2wsgi follows PEP 3333 and expects UTF-8 bytes decoded as
    latin-1, so any non-ASCII character in the path raised UnicodeDecodeError or
    UnicodeEncodeError and the task failed. Requests over a real HTTP connection
    are not affected.
    
    * Use httpx2 in the non-ASCII in-process transport tests
    
    The in-process Execution API transport now uses httpx2, and test_app.py
    no longer imports httpx after main was merged in.
    
    * Refer to httpx2 in the in-process path middleware docstring
    
    The in-process transport moved from httpx to httpx2.
    
    ---------
    
    Co-authored-by: Per Andersen <[email protected]>
---
 .../api_fastapi/execution_api/in_process.py        | 18 +++++++++++-
 .../unit/api_fastapi/execution_api/test_app.py     | 33 ++++++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/airflow-core/src/airflow/api_fastapi/execution_api/in_process.py 
b/airflow-core/src/airflow/api_fastapi/execution_api/in_process.py
index 1676409bb04..98f3999ce69 100644
--- a/airflow-core/src/airflow/api_fastapi/execution_api/in_process.py
+++ b/airflow-core/src/airflow/api_fastapi/execution_api/in_process.py
@@ -165,10 +165,26 @@ class InProcessExecutionAPI:
             thread.pydev_do_not_trace = True  # type: ignore[attr-defined]
         thread.start()
 
+        class PEP3333PathMiddleware(ASGIMiddleware):
+            """
+            Pass ``PATH_INFO`` and ``SCRIPT_NAME`` on to a2wsgi in PEP 3333 
form.
+
+            ``httpx2.WSGITransport`` puts the percent-decoded URL path into 
the environ as a regular ``str``,
+            whereas PEP 3333 (and ``a2wsgi``, which follows it) expects UTF-8 
bytes decoded as latin-1.
+            Without this, any non-ASCII character in the path, such as a 
non-ASCII ``task_id``, fails with a
+            ``UnicodeDecodeError`` or ``UnicodeEncodeError``.
+            """
+
+            def __call__(self, environ, start_response):
+                for key in ("PATH_INFO", "SCRIPT_NAME"):
+                    if key in environ:
+                        environ[key] = 
environ[key].encode("utf-8").decode("latin-1")
+                return super().__call__(environ, start_response)
+
         app = self.app
         # a2wsgi types ASGI scopes and messages as TypedDict unions and 
Starlette as mutable mappings,
         # so no app typed against Starlette matches a2wsgi's ``ASGIApp``.
-        middleware = ASGIMiddleware(cast("ASGIApp", 
_RequestScopedServerContextApp(app)), loop=loop)
+        middleware = PEP3333PathMiddleware(cast("ASGIApp", 
_RequestScopedServerContextApp(app)), loop=loop)
 
         # https://github.com/abersheeran/a2wsgi/discussions/64
         async def start_lifespan(cm: AsyncExitStack, app: FastAPI):
diff --git a/airflow-core/tests/unit/api_fastapi/execution_api/test_app.py 
b/airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
index 782726de7f7..36568ef12e2 100644
--- a/airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
+++ b/airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
@@ -299,6 +299,39 @@ def test_in_process_execution_api_transport_lifecycle():
     assert not thread.is_alive()
 
 
[email protected]("task_id", ["Købsmoms", "タスク"])
+def test_in_process_execution_api_handles_non_ascii_path(task_id):
+    """Non-ASCII path segments (e.g. a non-ASCII task_id) must reach the app 
intact."""
+    app = FastAPI()
+
+    @app.get("/echo/{task_id}")
+    async def echo(task_id: str):
+        return task_id
+
+    api = InProcessExecutionAPI(app)
+    with httpx2.Client(transport=api.transport) as client:
+        response = client.get(f"http://localhost/echo/{task_id}";)
+    assert response.status_code == 200
+    assert response.json() == task_id
+
+
[email protected]("script_name", ["/Købsmoms", "/タスク"])
+def test_in_process_execution_api_handles_non_ascii_script_name(script_name):
+    """A non-ASCII ``SCRIPT_NAME`` must reach the app intact as the ASGI 
``root_path``."""
+    app = FastAPI()
+
+    @app.get("/echo")
+    async def echo(request: Request):
+        return request.scope["root_path"]
+
+    api = InProcessExecutionAPI(app)
+    transport = httpx2.WSGITransport(app=api.transport.app, 
script_name=script_name)
+    with httpx2.Client(transport=transport) as client:
+        response = client.get("http://localhost/echo";)
+    assert response.status_code == 200
+    assert response.json() == script_name
+
+
 @pytest.fixture
 def in_process_db_app():
     engine = settings.async_engine

Reply via email to