File tree Expand file tree Collapse file tree
modularUnit/scenarios/operations/bodyParam Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff 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 ( ) => {
Original file line number Diff line number Diff 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 `
You can’t perform that action at this time.
0 commit comments