Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/http-client-python/emitter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ const sdkScalarKindToPythonKind: Record<string, string> = {
const supportedPythonEncodings = new Map<string, ReadonlySet<string>>([
["boolean", new Set(["string"])],
["integer", new Set(["string"])],
["bytes", new Set(["base64", "base64url"])],
["bytes", new Set(["bytes", "base64", "base64url"])],
]);

function emitBuiltInType(
Expand Down
18 changes: 18 additions & 0 deletions packages/http-client-python/emitter/test/emitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/http-client-python/emitter/test/test-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion packages/http-client-python/emitter/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"))),
{
Expand All @@ -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"))),
Expand Down
123 changes: 123 additions & 0 deletions packages/http-client-python/tests/unit/test_raw_bytes_xml.py
Original file line number Diff line number Diff line change
@@ -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, "<generated raw XML operation>", "exec")


@pytest.mark.parametrize("async_mode", [False, True])
@pytest.mark.parametrize("decompress", [False, True])
@pytest.mark.parametrize(
"body",
[
b'<publishData><publishProfile profileName="example" publishMethod="MSDeploy" /></publishData>',
b'<?xml version="1.0"?>\n<publishData>\n <publishProfile profileName="example" publishMethod="MSDeploy" />\n</publishData>',
],
)
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, "<generated raw XML deserialization>", "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
Loading