Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-client-python"
---

Add an `IO[bytes]` overload alongside `bytes` for binary `bytes` bodies, keeping backward compatibility for services migrating from swagger whose binary bodies were typed as `IO`.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
)
from .parameter_list import ParameterList
from .model_type import ModelType
from .primitive_types import BinaryIteratorType, BinaryType
from .primitive_types import BinaryIteratorType, BinaryType, ByteArraySchema
from .base import BaseType
from .combined_type import CombinedType
from .request_builder import OverloadedRequestBuilder, RequestBuilder
Expand Down Expand Up @@ -224,7 +224,15 @@ def non_default_errors(self) -> list[Response]:

@property
def need_import_iobase(self) -> bool:
return self.parameters.has_body and isinstance(self.parameters.body_parameter.type, CombinedType)
if not (self.parameters.has_body and isinstance(self.parameters.body_parameter.type, CombinedType)):
return False
# For a binary `bytes` body the combined type is `bytes` + `IO[bytes]`. Both overloads
# serialize to raw content, so the body serialization collapses to a single assignment
# without an `isinstance` branch. In that case `IOBase` is never referenced, so avoid
# importing it to prevent an unused-import lint error in the generated code.
if any(isinstance(t, ByteArraySchema) for t in self.parameters.body_parameter.type.types):
return False
return True

@staticmethod
def has_kwargs_to_pop_with_default(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,34 @@ def _create_body_parameter(
retval.extend(self._serialize_body_parameter(builder))
return retval

@staticmethod
def _collapsed_binary_bytes_overload(builder: OperationType) -> Optional[OperationType]:
"""Return the binary overload of a binary ``bytes`` body, or ``None``.

A binary ``bytes`` body pairs a ``bytes`` overload (binary content type) with the added
``IO[bytes]`` overload. Both serialize to raw content on the same content kwarg, so body
serialization collapses to a single unconditional assignment. This returns the binary
overload to serialize in that case, or ``None`` when the isinstance branch is needed.
"""
if not builder.overloads:
return None
binary_ov = next(
(o for o in builder.overloads if isinstance(o.parameters.body_parameter.type, BinaryType)), None
)
other_ov = next(
(o for o in builder.overloads if not isinstance(o.parameters.body_parameter.type, BinaryType)), None
)
if (
binary_ov is not None
and other_ov is not None
and isinstance(other_ov.parameters.body_parameter.type, ByteArraySchema)
and other_ov.parameters.body_parameter.default_content_type != "application/json"
and binary_ov.request_builder.parameters.body_parameter.client_name
== other_ov.request_builder.parameters.body_parameter.client_name
):
return cast(OperationType, binary_ov)
return None

def _initialize_overloads(self, builder: OperationType, is_paging: bool = False) -> list[str]:
retval: list[str] = []
# For paging, we put body parameter in local place outside `prepare_request`
Expand All @@ -817,27 +845,44 @@ def _initialize_overloads(self, builder: OperationType, is_paging: bool = False)
all_dpg_model_overloads = all(
_is_dpg_or_typeddict_body(o.parameters.body_parameter) for o in builder.overloads
)
if not all_dpg_model_overloads:
# A binary `bytes` body pairs a `bytes` overload (binary content type) with the added
# `IO[bytes]` overload. Both serialize to raw content on the same content kwarg, so we
# emit a single unconditional assignment instead of an `isinstance` branch (which would be
# redundant and confuse mypy's type narrowing). Since the assignment is unconditional, the
# `_<body> = None` pre-init below is skipped for this case as well.
collapsed_binary_bytes_overload = self._collapsed_binary_bytes_overload(builder)

if not all_dpg_model_overloads and collapsed_binary_bytes_overload is None:
for v in sorted(set(client_names), key=client_names.index):
retval.append(f"_{v} = None")

if collapsed_binary_bytes_overload is not None:
collapsed_body_param = collapsed_binary_bytes_overload.parameters.body_parameter
if collapsed_body_param.default_content_type and not same_content_type:
retval.append(
f'content_type = content_type or "{collapsed_body_param.default_content_type}"{check_body_suffix}'
)
retval.extend(self._create_body_parameter(collapsed_binary_bytes_overload))
return retval

try:
# if there is a binary overload, we do a binary check first.
binary_overload = cast(
OperationType,
next((o for o in builder.overloads if isinstance(o.parameters.body_parameter.type, BinaryType))),
)
binary_body_param = binary_overload.parameters.body_parameter
other_overload = cast(
OperationType,
next((o for o in builder.overloads if not isinstance(o.parameters.body_parameter.type, BinaryType))),
)
retval.append(f"if {binary_body_param.type.instance_check_template.format(binary_body_param.client_name)}:")
if binary_body_param.default_content_type and not same_content_type:
retval.append(
f' content_type = content_type or "{binary_body_param.default_content_type}"{check_body_suffix}'
)
retval.extend(f" {l}" for l in self._create_body_parameter(binary_overload))
retval.append("else:")
other_overload = cast(
OperationType,
next((o for o in builder.overloads if not isinstance(o.parameters.body_parameter.type, BinaryType))),
)
retval.extend(f" {l}" for l in self._create_body_parameter(other_overload))
if other_overload.parameters.body_parameter.default_content_type and not same_content_type:
retval.append(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,25 @@ def add_body_param_type(
code_model: dict[str, Any],
body_parameter: dict[str, Any],
):
# For a binary `bytes` body (e.g. content type application/octet-stream or a custom
# binary media type), add an IO overload alongside the `bytes` one. This keeps backward
# compatibility for services migrating from swagger, whose binary bodies were typed as IO.
is_multipart = self.is_tsp and has_multi_part_content_type(body_parameter)
is_binary_body = not has_json_content_type(body_parameter) and not is_multipart
if (
body_parameter
and body_parameter["type"]["type"] == "bytes"
and is_binary_body
and self.options["models-mode"] != "typeddict"
and not any(t for t in ["flattened", "groupedBy"] if body_parameter.get(t))
):
body_parameter["type"] = {
"type": "combined",
"types": [body_parameter["type"], KNOWN_TYPES["binary"]],
}
code_model["types"].append(body_parameter["type"])
return

# only add overload for special content type
if ( # pylint: disable=too-many-boolean-expressions
body_parameter
Expand Down
2 changes: 1 addition & 1 deletion packages/http-client-python/generator/pygen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _get_value(value: Any) -> Any:


def get_body_type_for_description(body_parameter: dict[str, Any]) -> str:
if body_parameter["type"]["type"] == "binary":
if body_parameter["type"]["type"] in ("binary", "bytes"):
return "binary"
if body_parameter["type"]["type"] == "string":
return "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import io
import pytest
import pytest_asyncio
from pathlib import Path
Expand Down Expand Up @@ -123,6 +124,20 @@ async def test_request_body(client: BytesClient, png_data: bytes):
)


@pytest.mark.asyncio
async def test_request_body_io(client: BytesClient, png_data: bytes):
# binary bodies also accept an IO input via the generated overload
await client.request_body.default(
value=io.BytesIO(png_data),
)
await client.request_body.octet_stream(
value=io.BytesIO(png_data),
)
await client.request_body.custom_content_type(
value=io.BytesIO(png_data),
)


@pytest.mark.asyncio
async def test_response_body(client: BytesClient, png_data: bytes):
expected = b"test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import io
from pathlib import Path

import pytest
Expand Down Expand Up @@ -119,6 +120,19 @@ def test_request_body(client: BytesClient, png_data: bytes):
)


def test_request_body_io(client: BytesClient, png_data: bytes):
# binary bodies also accept an IO input via the generated overload
client.request_body.default(
value=io.BytesIO(png_data),
)
client.request_body.octet_stream(
value=io.BytesIO(png_data),
)
client.request_body.custom_content_type(
value=io.BytesIO(png_data),
)


def test_response_body(client: BytesClient, png_data: bytes):
expected = b"test"
assert b"".join(client.response_body.default()) == png_data
Expand Down
103 changes: 103 additions & 0 deletions packages/http-client-python/tests/unit/test_bytes_body_overloads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# -------------------------------------------------------------------------
# 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 IO overload added to binary ``bytes`` bodies.

TypeSpec ``bytes`` used with a binary content type (e.g. application/octet-stream
or a custom media type) is emitted as ``bytes``. To keep backward compatibility for
services migrating from swagger, whose binary bodies were typed as ``IO``, the
preprocess plugin adds an ``IO[bytes]`` overload alongside the ``bytes`` one.
"""
from pygen.preprocess import PreProcessPlugin, add_overloads_for_body_param


def _plugin(models_mode: str = "dpg") -> PreProcessPlugin:
return PreProcessPlugin(
output_folder="",
**{
"version-tolerant": True,
"models-mode": models_mode,
"tsp_file": True,
"show-operations": True,
"show-send-request": True,
"builders-visibility": "public",
},
)


def _content_type_param() -> dict:
return {
"wireName": "Content-Type",
"clientName": "content_type",
"location": "header",
"optional": True,
"implementation": "Method",
"inOverload": False,
"inDocstring": True,
"type": {"type": "string"},
}


def _bytes_body_operation(content_types: list) -> tuple[dict, dict]:
body_parameter = {
"wireName": "value",
"clientName": "value",
"location": "body",
"optional": False,
"implementation": "Method",
"contentTypes": content_types,
"type": {"type": "bytes"},
}
yaml_data = {
"name": "upload",
"bodyParameter": body_parameter,
"parameters": [_content_type_param()],
"overloads": [],
"responses": [],
"exceptions": [],
}
code_model = {"types": []}
return code_model, yaml_data


def test_binary_bytes_body_adds_io_overload():
"""A binary ``bytes`` body gets a combined ``bytes``/``IO[bytes]`` type with two overloads."""
plugin = _plugin()
code_model, yaml_data = _bytes_body_operation(["application/octet-stream"])
body_parameter = yaml_data["bodyParameter"]

plugin.add_body_param_type(code_model, body_parameter)
add_overloads_for_body_param(yaml_data)

assert body_parameter["type"]["type"] == "combined"
member_types = [t["type"] for t in body_parameter["type"]["types"]]
assert member_types == ["bytes", "binary"]
assert len(yaml_data["overloads"]) == 2


def test_json_bytes_body_stays_plain_bytes():
"""A ``bytes`` body with a JSON content type stays a plain ``bytes`` type (no IO overload)."""
plugin = _plugin()
code_model, yaml_data = _bytes_body_operation(["application/json"])
body_parameter = yaml_data["bodyParameter"]

plugin.add_body_param_type(code_model, body_parameter)
add_overloads_for_body_param(yaml_data)

assert body_parameter["type"]["type"] == "bytes"
assert len(yaml_data["overloads"]) == 0


def test_typeddict_mode_binary_bytes_body_stays_plain_bytes():
"""In typeddict-only mode the IO overload is skipped for binary ``bytes`` bodies."""
plugin = _plugin("typeddict")
code_model, yaml_data = _bytes_body_operation(["application/octet-stream"])
body_parameter = yaml_data["bodyParameter"]

plugin.add_body_param_type(code_model, body_parameter)
add_overloads_for_body_param(yaml_data)

assert body_parameter["type"]["type"] == "bytes"
assert len(yaml_data["overloads"]) == 0
Loading