Skip to content

Commit

Permalink
fix-default-byte-as-binary-payload-in-modular (#2088)
Browse files Browse the repository at this point in the history
* fix-default-byte-as-binary-in-modular

* fix ci

* resolve comments and fix ci

* add response case and fix isBinaryPayload

* fix linter error

* fix binary response
  • Loading branch information
qiaozha authored Oct 30, 2023
1 parent d913c84 commit da0589a
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import {
StreamableMethod,
operationOptionsToRequestParameters,
} from "@azure-rest/core-client";
import { uint8ArrayToString } from "@azure/core-util";
import {
CreateOrUpdateTestOptions,
CreateOrUpdateAppComponentsOptions,
Expand Down Expand Up @@ -980,7 +979,7 @@ export function _uploadTestFileSend(
...operationOptionsToRequestParameters(options),
contentType: (options.contentType as any) ?? "application/octet-stream",
queryParameters: { fileType: options?.fileType },
body: uint8ArrayToString(body, "base64"),
body: body,
});
}

Expand Down
21 changes: 16 additions & 5 deletions packages/typespec-ts/src/modular/buildCodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import {
import {
getOperationGroupName,
getOperationName,
isBinaryPayload,
isIgnoredHeaderParam,
isLongRunningOperation
} from "../utils/operationUtil.js";
Expand Down Expand Up @@ -390,6 +391,7 @@ type BodyParameter = ParamBase & {
restApiName: string;
location: "body";
defaultContentType: string;
isBinaryPayload: boolean;
};

function getBodyType(program: Program, route: HttpOperation): Type {
Expand Down Expand Up @@ -449,16 +451,18 @@ function emitBodyParameter(
disableEffectiveModel: true
});

const defaultContentType =
body.parameter?.default ?? contentTypes.includes("application/json")
? "application/json"
: contentTypes[0]!;
return {
contentTypes,
type,
restApiName: body.parameter?.name ?? "body",
location: "body",
...base,
defaultContentType:
body.parameter?.default ?? contentTypes.includes("application/json")
? "application/json"
: contentTypes[0]!
defaultContentType,
isBinaryPayload: isBinaryPayload(context, body.type, defaultContentType)
};
}

Expand Down Expand Up @@ -612,7 +616,14 @@ function emitResponse(
statusCodes: statusCodes ?? [],
addedOn: getAddedOnVersion(context.program, response.type),
discriminator: "basic",
type: type
type: type,
isBinaryPayload: innerResponse.body?.type
? isBinaryPayload(
context,
innerResponse.body?.type,
innerResponse.body?.contentTypes![0] ?? "application/json"
)
: false
};
}

Expand Down
9 changes: 3 additions & 6 deletions packages/typespec-ts/src/modular/helpers/operationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function getDeserializePrivateFunction(
}
}

if (response?.type?.type === "any") {
if (response?.type?.type === "any" || response.isBinaryPayload) {
statements.push(`return result.body`);
} else if (getAllProperties(response?.type).length > 0) {
statements.push(
Expand Down Expand Up @@ -386,7 +386,7 @@ function buildBodyParameter(

if (
bodyParameter.type.type === "byte-array" &&
bodyParameter.type.format !== "binary"
!bodyParameter.isBinaryPayload
) {
const coreUtilSet = importSet.get("@azure/core-util");
if (!coreUtilSet) {
Expand All @@ -406,10 +406,7 @@ function buildBodyParameter(
: `body: uint8ArrayToString(${
bodyParameter.clientName
}, "${getEncodingFormat(bodyParameter.type)}")`;
} else if (
bodyParameter.type.type === "byte-array" &&
bodyParameter.type.format === "binary"
) {
} else if (bodyParameter.isBinaryPayload) {
return `\nbody: ${bodyParameter.clientName},`;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/typespec-ts/src/modular/modularCodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface BodyParameter {
clientName: string;
inOverload: boolean;
defaultContentType: string;
isBinaryPayload: boolean;
}

export interface OperationGroup {
Expand Down Expand Up @@ -144,6 +145,7 @@ export interface Response {
discriminator: string;
type: Type;
addedOn?: string;
isBinaryPayload?: boolean;
}

export interface Operation {
Expand Down
5 changes: 4 additions & 1 deletion packages/typespec-ts/src/utils/modelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ export function isByteOrByteUnion(dpgContext: SdkContext, type: Type) {
}

function isByteType(schema: any) {
return schema.type === "string" && schema.format === "byte";
return (
schema.type === "string" &&
(schema.format === "byte" || schema.format === "binary")
);
}

function isByteUnion(schema: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function _requestBodyOctetStreamSend(
.post({
...operationOptionsToRequestParameters(options),
contentType: (options.contentType as any) ?? "application/octet-stream",
body: uint8ArrayToString(value, "base64"),
body: value,
});
}

Expand Down Expand Up @@ -97,7 +97,7 @@ export function _requestBodyCustomContentTypeSend(
.post({
...operationOptionsToRequestParameters(options),
contentType: (options.contentType as any) ?? "image/png",
body: uint8ArrayToString(value, "base64"),
body: value,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ export async function _responseBodyOctetStreamDeserialize(
throw result.body;
}

return typeof result.body === "string"
? stringToUint8Array(result.body, "base64")
: result.body;
return result.body;
}

export async function responseBodyOctetStream(
Expand All @@ -96,9 +94,7 @@ export async function _responseBodyCustomContentTypeDeserialize(
throw result.body;
}

return typeof result.body === "string"
? stringToUint8Array(result.body, "base64")
: result.body;
return result.body;
}

export async function responseBodyCustomContentType(
Expand Down
Loading

0 comments on commit da0589a

Please sign in to comment.