From 693b280269b227e8c801e4e61ed26f1fe240b6b2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 21 Oct 2026 08:14:54 +0000 Subject: [PATCH 1/5] Initial plan From 5e2b9fe20f12dc1c56f52381634f1db2d61d82a7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 21 Oct 2026 08:25:42 +0000 Subject: [PATCH 2/5] fix(http-client-python): preserve legacy raw XML byte streams Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com> --- .../http-client-python/emitter/src/types.ts | 2 +- .../emitter/test/emitter.test.ts | 18 +++ .../emitter/test/test-host.ts | 2 +- .../emitter/test/types.test.ts | 19 ++- .../tests/unit/test_raw_bytes_xml.py | 123 ++++++++++++++++++ 5 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 packages/http-client-python/tests/unit/test_raw_bytes_xml.py diff --git a/packages/http-client-python/emitter/src/types.ts b/packages/http-client-python/emitter/src/types.ts index d3f1d6778de..1c56325afab 100644 --- a/packages/http-client-python/emitter/src/types.ts +++ b/packages/http-client-python/emitter/src/types.ts @@ -477,7 +477,7 @@ const sdkScalarKindToPythonKind: Record = { const supportedPythonEncodings = new Map>([ ["boolean", new Set(["string"])], ["integer", new Set(["string"])], - ["bytes", new Set(["base64", "base64url"])], + ["bytes", new Set(["bytes", "base64", "base64url"])], ]); function emitBuiltInType( diff --git a/packages/http-client-python/emitter/test/emitter.test.ts b/packages/http-client-python/emitter/test/emitter.test.ts index fbbc7a37690..857dc01bc02 100644 --- a/packages/http-client-python/emitter/test/emitter.test.ts +++ b/packages/http-client-python/emitter/test/emitter.test.ts @@ -2,6 +2,24 @@ import { expectDiagnostics, t } from "@typespec/compiler/testing"; import { expect, it } from "vitest"; import { EmitterTester, emitCodeModel } from "./test-host.js"; +it("preserves an XML response body with the legacy bytes encoding", async () => { + const { codeModel, diagnostics } = await emitCodeModel(` + import "@typespec/http"; + using TypeSpec.Http; + + @service namespace Test; + @get op publishingProfile(): { + @header("Content-Type") contentType: "application/xml"; + @bodyRoot @encode("bytes") body: bytes; + }; + `); + + expectDiagnostics(diagnostics, []); + expect(codeModel.types).toContainEqual( + expect.objectContaining({ type: "bytes", encode: "bytes" }), + ); +}); + it.each([undefined, false, true])( "propagates enable-sse-reconnect=%s to the Python generator", async (enabled) => { diff --git a/packages/http-client-python/emitter/test/test-host.ts b/packages/http-client-python/emitter/test/test-host.ts index 7e93659a4e1..2b5a087881c 100644 --- a/packages/http-client-python/emitter/test/test-host.ts +++ b/packages/http-client-python/emitter/test/test-host.ts @@ -9,7 +9,7 @@ import { $onEmit } from "../src/emitter.js"; import { $lib, type PythonEmitterOptions } from "../src/lib.js"; const PythonTester = createTester(resolvePath(import.meta.dirname, "../.."), { - libraries: ["@azure-tools/typespec-client-generator-core"], + libraries: ["@azure-tools/typespec-client-generator-core", "@typespec/http"], }); export const Tester = PythonTester; diff --git a/packages/http-client-python/emitter/test/types.test.ts b/packages/http-client-python/emitter/test/types.test.ts index 750c357a670..2dd84595c8a 100644 --- a/packages/http-client-python/emitter/test/types.test.ts +++ b/packages/http-client-python/emitter/test/types.test.ts @@ -50,7 +50,7 @@ describe("typespec-python: built-in types", () => { }, ); - it.each(["base64", "base64url"] as const)("preserves bytes encoded as %s", (encode) => { + it.each(["bytes", "base64", "base64url"] as const)("preserves bytes encoded as %s", (encode) => { deepStrictEqual( emitBuiltInType(createBuiltInType("bytes", encode, createBuiltInType("string"))), { @@ -60,6 +60,23 @@ describe("typespec-python: built-in types", () => { ); }); + it("preserves raw bytes without a wire type", () => { + deepStrictEqual(emitBuiltInType(createBuiltInType("bytes", "bytes")), { + type: "bytes", + encode: "bytes", + }); + }); + + it("falls back to string for a custom bytes encoding", () => { + deepStrictEqual( + emitBuiltInType(createBuiltInType("bytes", "custom", createBuiltInType("string"))), + { + type: "string", + encode: "custom", + }, + ); + }); + it("falls back to the wire type for a custom encoding", () => { deepStrictEqual( emitBuiltInType(createBuiltInType("string", "abc", createBuiltInType("int32"))), diff --git a/packages/http-client-python/tests/unit/test_raw_bytes_xml.py b/packages/http-client-python/tests/unit/test_raw_bytes_xml.py new file mode 100644 index 00000000000..017cacd8e1b --- /dev/null +++ b/packages/http-client-python/tests/unit/test_raw_bytes_xml.py @@ -0,0 +1,123 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- +import asyncio +from types import SimpleNamespace + +import pytest +from jinja2 import Environment, PackageLoader + +from pygen import OptionsDict +from pygen.codegen.models import CodeModel, Operation, ParameterList, RequestBuilder, Response +from pygen.codegen.models.primitive_types import ByteArraySchema +from pygen.codegen.models.parameter_list import RequestBuilderParameterList +from pygen.codegen.serializers.builder_serializer import OperationSerializer + + +@pytest.fixture(scope="session", autouse=True) +def testserver(): + yield + + +@pytest.fixture(params=["azure", "unbranded"]) +def operation(request): + code_model = CodeModel( + { + "namespace": "test", + "clients": [{"name": "Client", "namespace": "test", "parameters": [], "url": "", "operationGroups": []}], + }, + options=OptionsDict( + {"flavor": request.param, "models-mode": "dpg", "version-tolerant": True, "builders-visibility": "public"} + ), + ) + client = code_model.clients[0] + yaml_data = { + "url": "/profile", + "method": "POST", + "groupName": "WebApps", + "apiVersions": [], + "isOverload": False, + "exposeStreamKeyword": True, + } + bytes_type = {"type": "bytes", "encode": "bytes"} + code_model.types_map[id(bytes_type)] = ByteArraySchema(bytes_type, code_model) + response = Response.from_yaml( + {"statusCodes": [200], "defaultContentType": "application/xml", "headers": [], "type": bytes_type}, + code_model, + ) + return Operation( + yaml_data, + code_model, + client, + "list_publishing_profile_xml_with_secrets", + RequestBuilder( + yaml_data, + code_model, + client, + "build_profile_request", + RequestBuilderParameterList({}, code_model, parameters=[]), + ), + ParameterList({}, code_model, parameters=[]), + responses=[response], + exceptions=[], + ) + + +@pytest.mark.parametrize("async_mode", [False, True]) +def test_raw_bytes_xml_signature_and_default_streaming(operation, async_mode): + operation.include_documentation = False + serializer = OperationSerializer(operation.code_model, async_mode=async_mode, client_namespace="test") + env = Environment(loader=PackageLoader("pygen.codegen", "templates"), trim_blocks=True, lstrip_blocks=True) + generated = env.get_template("operation.py.jinja2").render(operation=operation, operation_serializer=serializer) + + assert f"-> {'AsyncIterator' if async_mode else 'Iterator'}[bytes]:" in generated + assert ("async def list_publishing_profile_xml_with_secrets" in generated) == async_mode + assert '_stream = kwargs.pop("stream", True)' in generated + assert "deserialized = response.iter_bytes() if _decompress else response.iter_raw()" in generated + assert "_deserialize_xml" not in generated + compile(generated, "", "exec") + + +@pytest.mark.parametrize("async_mode", [False, True]) +@pytest.mark.parametrize("decompress", [False, True]) +@pytest.mark.parametrize( + "body", + [ + b'', + b'\n\n \n', + ], +) +def test_raw_bytes_xml_preserves_document(operation, async_mode, decompress, body): + serializer = OperationSerializer(operation.code_model, async_mode=async_mode, client_namespace="test") + deserialization = serializer.response_deserialization(operation, operation.responses[0]) + source = "\n".join( + [ + f"{'async ' if async_mode else ''}def deserialize(response, **kwargs):", + f" _stream = {operation.stream_value}", + f" _decompress = {decompress}", + *[f" {line}" for line in deserialization], + " return deserialized", + ] + ) + namespace = {} + exec(compile(source, "", "exec"), namespace) + + chunks = [body[index : index + 7] for index in range(0, len(body), 7)] + + async def async_chunks(): + for chunk in chunks: + yield chunk + + def stream(): + return async_chunks() if async_mode else iter(chunks) + + response = SimpleNamespace(iter_bytes=stream, iter_raw=stream) + + async def consume(): + result = await namespace["deserialize"](response) + return b"".join([chunk async for chunk in result]) + + result = asyncio.run(consume()) if async_mode else b"".join(namespace["deserialize"](response)) + assert result == body From 4981bb9036a12fc9e0bcbc3ebddf4791ae590bd8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 21 Oct 2026 08:26:18 +0000 Subject: [PATCH 3/5] chore(http-client-python): add raw XML streaming fix changelog Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com> --- .../copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md diff --git a/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md b/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md new file mode 100644 index 00000000000..f0e37c6efbc --- /dev/null +++ b/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Preserve raw byte streaming for XML responses using the legacy bytes encoding instead of deserializing them as XML string scalars. \ No newline at end of file From 66f4f36e746f98ca1f336cf80d78aa9d1a666ab4 Mon Sep 17 00:00:00 2001 From: Yuchao Yan Date: Wed, 21 Oct 2026 16:40:10 +0800 Subject: [PATCH 4/5] Update copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md --- .../copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md b/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md index f0e37c6efbc..355b8fcd447 100644 --- a/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md +++ b/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md @@ -4,4 +4,4 @@ packages: - "@typespec/http-client-python" --- -Preserve raw byte streaming for XML responses using the legacy bytes encoding instead of deserializing them as XML string scalars. \ No newline at end of file +Preserve raw byte streaming for XML responses using the legacy bytes encoding instead of deserializing them as XML string scalars From 36fcfd3575eab024f2d160ff2b700cb9bf7f001d Mon Sep 17 00:00:00 2001 From: Yuchao Yan Date: Wed, 21 Oct 2026 16:40:28 +0800 Subject: [PATCH 5/5] Fix XML response handling in Python emitter --- .../copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md b/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md index 355b8fcd447..831b89593c3 100644 --- a/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md +++ b/.chronus/changes/copilot-fix-python-emitter-xml-bug-2026-9-21-8-26-3.md @@ -4,4 +4,4 @@ packages: - "@typespec/http-client-python" --- -Preserve raw byte streaming for XML responses using the legacy bytes encoding instead of deserializing them as XML string scalars +Preserve raw byte streaming for XML responses using the legacy bytes encoding instead of deserializing them as XML string scalars.