Skip to content

Commit d219163

Browse files
Copilotkazrael2119JialinHuang803
authored
fix: simplify isBinaryPayload to use encode="bytes" as binary signal (#3958)
* Initial plan * update (cherry picked from commit c2f06b1) Co-authored-by: kazrael2119 <98569699+kazrael2119@users.noreply.github.com> * style: format operationHelpers after isolated commit cherry-pick Agent-Logs-Url: https://github.com/Azure/autorest.typescript/sessions/c5b9a83f-2fec-4bc7-b44b-2393f9d65a94 Co-authored-by: kazrael2119 <98569699+kazrael2119@users.noreply.github.com> * docs: clarify isolated binary payload scenarios Agent-Logs-Url: https://github.com/Azure/autorest.typescript/sessions/c5b9a83f-2fec-4bc7-b44b-2393f9d65a94 Co-authored-by: kazrael2119 <98569699+kazrael2119@users.noreply.github.com> * Update operationUtil.ts * Update operationUtil.ts * Update bytesWithDifferentContentType.md --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kazrael2119 <98569699+kazrael2119@users.noreply.github.com> Co-authored-by: JialinHuang803 <139532647+JialinHuang803@users.noreply.github.com>
1 parent d62d25b commit d219163

3 files changed

Lines changed: 664 additions & 4 deletions

File tree

packages/typespec-ts/src/modular/helpers/operationHelpers.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,12 @@ export function getDeserializePrivateFunction(
452452
deserializedType,
453453
deserializedRoot,
454454
true,
455-
isBinaryPayload(context, response.type!.__raw!, contentTypes)
455+
isBinaryPayload(
456+
context,
457+
response.type!.__raw!,
458+
contentTypes,
459+
getEncodeForType(response.type!)
460+
)
456461
? "binary"
457462
: getEncodeForType(deserializedType)
458463
)}${multipartCastSuffix}`
@@ -1760,7 +1765,12 @@ function buildBodyParameter(
17601765
bodyParameter.type,
17611766
bodyNameExpression,
17621767
!bodyParameter.optional,
1763-
isBinaryPayload(context, bodyParameter.__raw!, bodyParameter.contentTypes)
1768+
isBinaryPayload(
1769+
context,
1770+
bodyParameter.__raw!,
1771+
bodyParameter.contentTypes,
1772+
getEncodeForType(bodyParameter.type)
1773+
)
17641774
? "binary"
17651775
: getEncodeForType(bodyParameter.type),
17661776
undefined,
@@ -3266,7 +3276,10 @@ export function checkWrapNonModelReturn(
32663276

32673277
// bytes with binary content type → binary wrap (isBinary=true)
32683278
// HLC: bytes → binary payload → separate binary handling
3269-
if (type.__raw && isBinaryPayload(context, type.__raw, contentTypes)) {
3279+
if (
3280+
type.__raw &&
3281+
isBinaryPayload(context, type.__raw, contentTypes, getEncodeForType(type))
3282+
) {
32703283
return { shouldWrap: true, isBinary: true };
32713284
}
32723285

packages/typespec-ts/src/utils/operationUtil.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,18 @@ export function isDefinedStatusCode(statusCodes: HttpStatusCodesEntry) {
193193
export function isBinaryPayload(
194194
dpgContext: SdkContext,
195195
body: Type,
196-
contentType: string | string[]
196+
contentType: string | string[],
197+
encode?: string
197198
) {
198199
const knownMediaTypes: KnownMediaType[] = (
199200
Array.isArray(contentType) ? contentType : [contentType]
200201
).map((ct) => knownMediaType(ct));
202+
203+
// When encode is "bytes" treat as binary.
204+
if (encode === "bytes") {
205+
return true;
206+
}
207+
201208
for (const type of knownMediaTypes) {
202209
if (type === KnownMediaType.Binary && isByteOrByteUnion(dpgContext, body)) {
203210
return true;

0 commit comments

Comments
 (0)