Skip to content

Commit e1c5f7c

Browse files
Copilotkazrael2119
andauthored
fix: revert RLC unit test changes, change || back to &&, add modularUnit scenario test
Agent-Logs-Url: https://github.com/Azure/autorest.typescript/sessions/e6e4c70e-0694-45e4-9a47-b449e0e7f6b4 Co-authored-by: kazrael2119 <98569699+kazrael2119@users.noreply.github.com>
1 parent 0ca04e0 commit e1c5f7c

4 files changed

Lines changed: 57 additions & 32 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export function isBinaryPayload(
207207
knownMediaType(ct)
208208
);
209209
for (const type of knownMediaTypes) {
210-
if (type === KnownMediaType.Binary || isBytes) {
210+
if (type === KnownMediaType.Binary && isBytes) {
211211
return true;
212212
}
213213
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# bytes body with */* content type should be treated as binary
2+
3+
When an operation has `*/* ` content type and a `bytes` body, `isBinaryPayload` should return
4+
`true` so the parameter is typed as `Uint8Array` (binary) rather than `string` (base64).
5+
6+
## TypeSpec
7+
8+
```tsp
9+
@post op uploadFile(
10+
@header contentType: "*/*",
11+
@body body: bytes
12+
): void;
13+
```
14+
15+
## Operations
16+
17+
```ts operations
18+
import { TestingContext as Client } from "./index.js";
19+
import { UploadFileOptionalParams } from "./options.js";
20+
import {
21+
StreamableMethod,
22+
PathUncheckedResponse,
23+
createRestError,
24+
operationOptionsToRequestParameters,
25+
} from "@azure-rest/core-client";
26+
27+
export function _uploadFileSend(
28+
context: Client,
29+
body: Uint8Array,
30+
options: UploadFileOptionalParams = { requestOptions: {} },
31+
): StreamableMethod {
32+
return context
33+
.path("/")
34+
.post({ ...operationOptionsToRequestParameters(options), contentType: "*/*", body: body });
35+
}
36+
37+
export async function _uploadFileDeserialize(result: PathUncheckedResponse): Promise<void> {
38+
const expectedStatuses = ["204"];
39+
if (!expectedStatuses.includes(result.status)) {
40+
throw createRestError(result);
41+
}
42+
43+
return;
44+
}
45+
46+
export async function uploadFile(
47+
context: Client,
48+
body: Uint8Array,
49+
options: UploadFileOptionalParams = { requestOptions: {} },
50+
): Promise<void> {
51+
const result = await _uploadFileSend(context, body, options);
52+
return _uploadFileDeserialize(result);
53+
}
54+
```

packages/typespec-ts/test/unit/bytesGenerator.spec.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -539,34 +539,6 @@ describe("bytes", () => {
539539
`
540540
);
541541
});
542-
it("*/* content type with bytes body - should be treated as binary", async () => {
543-
const parameters = await emitParameterFromTypeSpec(
544-
`
545-
@post op uploadFile(
546-
@header contentType: "*/*",
547-
@body body: bytes
548-
): void;
549-
`
550-
);
551-
assert.ok(parameters);
552-
await assertEqualContent(
553-
parameters?.content!,
554-
`
555-
import type { RequestParameters } from "@azure-rest/core-client";
556-
557-
export interface UploadFileBodyParam {
558-
/** Value may contain any sequence of octets */
559-
body: string | Uint8Array | ReadableStream<Uint8Array> | NodeJS.ReadableStream;
560-
}
561-
562-
export interface UploadFileMediaTypesParam {
563-
contentType: "*/*";
564-
}
565-
566-
export type UploadFileParameters = UploadFileMediaTypesParam & UploadFileBodyParam & RequestParameters;
567-
`
568-
);
569-
});
570542
// TODO: we need more discussions about current behavior
571543
// This case is not finalized yet and some validations would be added in tcgc
572544
it.skip("mixed non-binary and binary content types - should report errors?", async () => {

packages/typespec-ts/test/unit/responsesGenerator.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ describe("Responses.ts", () => {
214214
`
215215
);
216216
});
217-
it("@header contentType text/plain with bytes body - should be treated as binary (trust body type)", async () => {
217+
it("@header contentType text/plain should keep format to byte(finally string)", async () => {
218218
const responses = await emitResponsesFromTypeSpec(`
219219
@get op read(): {@header contentType: "text/plain", @body body: bytes};
220220
`);
@@ -232,8 +232,7 @@ describe("Responses.ts", () => {
232232
/** The request has succeeded. */
233233
export interface Read200Response extends HttpResponse {
234234
status: "200";
235-
/** Value may contain any sequence of octets */
236-
body: Uint8Array;
235+
body: string;
237236
headers: RawHttpHeaders & Read200Headers;
238237
}
239238
`

0 commit comments

Comments
 (0)