From 210761931f0d26a1db21aa576ca057ee351e375e Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Wed, 10 Jun 2026 14:21:46 -0700 Subject: [PATCH 1/2] fix(http-client-python): construct paging JSON body outside prepare_request For paging operations with a flattened JSON model body, the body construction (if body is _Unset: ...) was emitted inside the prepare_request closure. Assigning ody there made it a function-local, so reading if body is _Unset: raised UnboundLocalError on every page fetch. Move the construction out of the closure and before the body is serialized into the request content. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../fix-paging-unset-body-placement-2026-06-10.md | 7 +++++++ .../pygen/codegen/serializers/builder_serializer.py | 11 +++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 .chronus/changes/fix-paging-unset-body-placement-2026-06-10.md diff --git a/.chronus/changes/fix-paging-unset-body-placement-2026-06-10.md b/.chronus/changes/fix-paging-unset-body-placement-2026-06-10.md new file mode 100644 index 00000000000..a86f2075fa3 --- /dev/null +++ b/.chronus/changes/fix-paging-unset-body-placement-2026-06-10.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Fix `UnboundLocalError` for paging operations with a flattened JSON model body. The request body is now constructed once outside the `prepare_request` callback (and before the body is serialized into the request content) instead of inside the closure, where assigning `body` made it an unbound local on every page fetch. diff --git a/packages/http-client-python/generator/pygen/codegen/serializers/builder_serializer.py b/packages/http-client-python/generator/pygen/codegen/serializers/builder_serializer.py index 5dcf94e32b4..c615baf827f 100644 --- a/packages/http-client-python/generator/pygen/codegen/serializers/builder_serializer.py +++ b/packages/http-client-python/generator/pygen/codegen/serializers/builder_serializer.py @@ -931,7 +931,8 @@ def _call_request_builder_helper( if builder.parameters.has_body and builder.parameters.body_parameter.flattened: # serialize flattened body before passing to request builder as well retval.extend(_serialize_flattened_body(builder.parameters.body_parameter)) - if is_json_model_type(builder.parameters): + if is_json_model_type(builder.parameters) and not is_paging: + # For paging, we put the json model body outside `prepare_request` retval.extend(_serialize_json_model_body(builder.parameters.body_parameter, builder.parameters.parameters)) if builder.has_form_data_body: retval.extend(self._create_body_parameter(builder)) @@ -1369,7 +1370,13 @@ def call_next_link_request_builder(self, builder: PagingOperationType) -> list[s return retval def _prepare_request_callback(self, builder: PagingOperationType) -> list[str]: - retval = self._initialize_overloads(builder) + retval: list[str] = [] + # The json model body must be constructed before `_initialize_overloads` + # serializes it into the request content, and outside `prepare_request` so + # the closure doesn't treat `body` as an unbound local. + if is_json_model_type(builder.parameters): + retval.extend(_serialize_json_model_body(builder.parameters.body_parameter, builder.parameters.parameters)) + retval.extend(self._initialize_overloads(builder)) if builder.has_continuation_token: retval.append(f"def prepare_request({builder.next_variable_name}=None):") retval.extend([f" {line}" for line in self.call_request_builder(builder, is_paging=True)]) From 6d03d3474dd45273feedd0a5f060fe306be536cd Mon Sep 17 00:00:00 2001 From: Libba Lawrence Date: Mon, 15 Jun 2026 09:27:37 -0700 Subject: [PATCH 2/2] test: add unit tests for paging flattened JSON body placement Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../unit/test_paging_json_body_placement.py | 236 ++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 packages/http-client-python/tests/unit/test_paging_json_body_placement.py diff --git a/packages/http-client-python/tests/unit/test_paging_json_body_placement.py b/packages/http-client-python/tests/unit/test_paging_json_body_placement.py new file mode 100644 index 00000000000..c5e08820c6c --- /dev/null +++ b/packages/http-client-python/tests/unit/test_paging_json_body_placement.py @@ -0,0 +1,236 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +"""Tests for the placement of the flattened JSON model body of paging operations. + +Regression coverage for the bug where the ``if body is _Unset: ...`` body +construction was emitted *inside* the ``prepare_request`` closure of a paging +operation. Because the closure assigned to ``body``, Python treated ``body`` as +a function local for the whole closure and the earlier ``if body is _Unset`` +read raised ``UnboundLocalError`` at runtime. +""" + +import pytest + +from pygen.codegen.models import ( + BodyParameter, + Client, + CodeModel, + JSONModelType, + PagingOperation, + Parameter, + ParameterList, + RequestBuilder, +) +from pygen.codegen.models.parameter_list import RequestBuilderParameterList +from pygen.codegen.models.primitive_types import StringType +from pygen.codegen.serializers.builder_serializer import ( + PagingOperationSerializer, + _serialize_json_model_body, + is_json_model_type, +) + + +@pytest.fixture +def code_model(): + return CodeModel( + { + "clients": [ + { + "name": "client", + "namespace": "blah", + "moduleName": "blah", + "parameters": [], + "url": "", + "operationGroups": [], + } + ], + "namespace": "namespace", + }, + options={ + "show-send-request": True, + "builders-visibility": "public", + "show-operations": True, + "models-mode": "dpg", + "version-tolerant": True, + }, + ) + + +@pytest.fixture +def client(code_model): + return Client( + { + "name": "client", + "namespace": "blah", + "moduleName": "blah", + "parameters": [], + "url": "", + "operationGroups": [], + }, + code_model, + parameters=[], + ) + + +def _json_model_type(code_model): + return JSONModelType( + {"type": "model", "name": "MyBody", "snakeCaseName": "my_body"}, + code_model, + ) + + +def _body_parameter(code_model): + return BodyParameter( + yaml_data={ + "clientName": "body", + "optional": False, + "location": "body", + "wireName": "body", + "contentTypes": ["application/json"], + "defaultContentType": "application/json", + "propertyToParameterName": {"filter": "filter", "maxCount": "max_count"}, + }, + code_model=code_model, + type=_json_model_type(code_model), + ) + + +def _flattened_parameters(code_model): + required = Parameter( + yaml_data={ + "clientName": "filter", + "optional": False, + "location": "other", + "wireName": "filter", + "inFlattenedBody": True, + "defaultToUnsetSentinel": True, + }, + code_model=code_model, + type=StringType({"type": "string"}, code_model), + ) + optional = Parameter( + yaml_data={ + "clientName": "max_count", + "optional": True, + "location": "other", + "wireName": "maxCount", + "inFlattenedBody": True, + "defaultToUnsetSentinel": True, + }, + code_model=code_model, + type=StringType({"type": "string"}, code_model), + ) + return [required, optional] + + +@pytest.fixture +def json_body_parameter_list(code_model): + return ParameterList( + {}, + code_model, + _flattened_parameters(code_model), + body_parameter=_body_parameter(code_model), + ) + + +@pytest.fixture +def request_builder(code_model, client): + return RequestBuilder( + yaml_data={ + "url": "http://fake.com", + "method": "GET", + "groupName": "blah", + "isOverload": False, + "apiVersions": [], + }, + client=client, + code_model=code_model, + name="paging_json_body_test_request", + parameters=RequestBuilderParameterList({}, code_model, parameters=[]), + ) + + +@pytest.fixture +def paging_operation(code_model, client, request_builder, json_body_parameter_list): + return PagingOperation( + yaml_data={ + "url": "http://fake.com", + "method": "GET", + "groupName": "blah", + "isOverload": False, + "apiVersions": [], + "pagerSync": "blah", + "pagerAsync": "blah", + }, + client=client, + code_model=code_model, + request_builder=request_builder, + name="paging_json_body_test", + parameters=json_body_parameter_list, + responses=[], + exceptions=[], + ) + + +class _TestPagingOperationSerializer(PagingOperationSerializer): + """Stubs out the heavy request-builder calls so we can assert on placement.""" + + def call_request_builder(self, builder, is_paging=False): + return ["_request = build_request()"] + + def call_next_link_request_builder(self, builder): + return ["_request = build_next_request()"] + + +def test_is_json_model_type(json_body_parameter_list): + assert is_json_model_type(json_body_parameter_list) + + +def test_serialize_json_model_body_emits_unset_guard( + code_model, json_body_parameter_list +): + lines = _serialize_json_model_body( + json_body_parameter_list.body_parameter, json_body_parameter_list.parameters + ) + text = "\n".join(lines) + + # The body is only constructed when the caller did not pass it explicitly. + assert "if body is _Unset:" in lines + + # The required flattened parameter gets a missing-argument guard... + assert any("if filter is _Unset:" in line for line in lines) + assert "raise TypeError('missing required argument: filter')" in text + + # ...but the optional one does not. + assert all("max_count is _Unset" not in line for line in lines) + + # The body dict is constructed from the flattened parameters and then pruned + # of None values. + assert any('"filter": filter' in line for line in lines) + assert any('"maxCount": max_count' in line for line in lines) + assert any("if v is not None" in line for line in lines) + + +def test_paging_body_constructed_outside_prepare_request(code_model, paging_operation): + serializer = _TestPagingOperationSerializer( + code_model, async_mode=False, client_namespace="blah" + ) + lines = serializer._prepare_request_callback(paging_operation) + + unset_index = next( + i for i, line in enumerate(lines) if "if body is _Unset:" in line + ) + prepare_request_index = next( + i for i, line in enumerate(lines) if line.startswith("def prepare_request(") + ) + + # The regression: body construction must happen before (and therefore + # outside) the prepare_request closure. + assert unset_index < prepare_request_index + + # The body block must not be indented under the closure body. + body_line = lines[unset_index] + assert body_line == "if body is _Unset:"