Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix binary body #2739

Closed
wants to merge 4 commits into from
Closed
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
Expand Up @@ -12678,9 +12678,7 @@ export async function _getNodeRemoteDesktopFileDeserialize(
throw createRestError(result);
}

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

/**
Expand Down Expand Up @@ -13433,9 +13431,7 @@ export async function _getNodeFileDeserialize(
throw createRestError(result);
}

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

/** Returns the content of the specified Compute Node file. */
Expand Down
3 changes: 0 additions & 3 deletions packages/typespec-test/test/contoso/tspconfig.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
emit:
- "@azure-tools/typespec-ts"
- "@azure-tools/typespec-autorest"
options:
"@azure-tools/typespec-autorest":
"emitter-output-dir": "{project-root}/generated/openapi"
"@azure-tools/typespec-ts":
"emitter-output-dir": "{project-root}/generated/typespec-ts"
azureSdkForJs: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface AdministrationOperations {

#suppress "@azure-tools/typespec-azure-core/use-standard-operations"
@client({
name: "LoadTestRunClient",
name: "SubNamespace.LoadTestRunClient",
service: Microsoft.LoadTestService,
})
interface TestRunOperations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
PathUncheckedResponse,
createRestError,
} from "@azure-rest/core-client";
import { stringToUint8Array, uint8ArrayToString } from "@azure/core-util";
import {
PagedAsyncIterableIterator,
buildPagedAsyncIterator,
Expand Down Expand Up @@ -90,9 +89,7 @@ export async function _getSchemaByIdDeserialize(
throw createRestError(result);
}

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

/** Gets a registered schema by its unique ID. Azure Schema Registry guarantees that ID is unique within a namespace. Operation response type is based on serialization of schema requested. */
Expand Down Expand Up @@ -179,9 +176,7 @@ export async function _getSchemaByVersionDeserialize(
throw createRestError(result);
}

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

/** Gets one specific version of one schema. */
Expand Down Expand Up @@ -219,7 +214,7 @@ export function _getSchemaIdByContentSend(
.post({
...operationOptionsToRequestParameters(options),
contentType: contentType,
body: uint8ArrayToString(schemaContent, "base64"),
body: schemaContent,
});
}

Expand Down Expand Up @@ -271,7 +266,7 @@ export function _registerSchemaSend(
.put({
...operationOptionsToRequestParameters(options),
contentType: contentType,
body: uint8ArrayToString(content, "base64"),
body: content,
});
}

Expand Down
8 changes: 2 additions & 6 deletions packages/typespec-ts/src/modular/buildCodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ function emitBodyParameter(
type,
location: "body",
...base,
isBinaryPayload: isBinaryPayload(context, body.type, contentTypes)
isBinaryPayload: isBinaryPayload(context, body.type)
};
}
return undefined;
Expand Down Expand Up @@ -643,11 +643,7 @@ function emitResponse(
discriminator: "basic",
type: type,
isBinaryPayload: innerResponse.body?.type
? isBinaryPayload(
context,
innerResponse.body?.type,
innerResponse.body?.contentTypes![0] ?? "application/json"
)
? isBinaryPayload(context, innerResponse.body?.type)
: false
};

Expand Down
1 change: 0 additions & 1 deletion packages/typespec-ts/src/modular/modularCodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export interface ModularCodeModel {
options: RLCOptions;
modularOptions: ModularOptions;
namespace?: string;
subnamespaceToClients?: any;
clients: Client[];
types: Type[];
project: Project;
Expand Down
4 changes: 1 addition & 3 deletions packages/typespec-ts/src/transform/transformResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,7 @@ function transformBody(
if (!body || isVoidType(body.type)) {
continue;
}
const hasBinaryContent = body.contentTypes.some((contentType) =>
isBinaryPayload(dpgContext, body.type, contentType)
);
const hasBinaryContent = isBinaryPayload(dpgContext, body.type);
if (hasBinaryContent) {
typeSet.add(getBinaryType([SchemaContext.Output]));
descriptions.add("Value may contain any sequence of octets");
Expand Down
16 changes: 3 additions & 13 deletions packages/typespec-ts/src/utils/operationUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
HttpStatusCodesEntry
} from "@typespec/http";
import { SdkContext } from "./interfaces.js";
import { KnownMediaType, knownMediaType } from "./mediaTypes.js";
import { isByteOrByteUnion } from "./modelUtils.js";
import { getOperationNamespaceInterfaceName } from "./namespaceUtils.js";
import { resolveReference } from "../framework/reference.js";
Expand Down Expand Up @@ -171,18 +170,9 @@ export function isDefinedStatusCode(statusCodes: HttpStatusCodesEntry) {
return statusCodes !== "*";
}

export function isBinaryPayload(
dpgContext: SdkContext,
body: Type,
contentType: string | string[]
) {
const knownMediaTypes: KnownMediaType[] = (
Array.isArray(contentType) ? contentType : [contentType]
).map((ct) => knownMediaType(ct));
for (const type of knownMediaTypes) {
if (type === KnownMediaType.Binary && isByteOrByteUnion(dpgContext, body)) {
return true;
}
export function isBinaryPayload(dpgContext: SdkContext, body: Type) {
if (isByteOrByteUnion(dpgContext, body)) {
return true;
}
return false;
}
Expand Down
Loading