-
Notifications
You must be signed in to change notification settings - Fork 80
Fix */* content type and bytes body treated as binary
#3918
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
base: main
Are you sure you want to change the base?
Changes from all commits
2fca779
5dc3da0
0ca04e0
e1c5f7c
9fd665b
4257998
471b877
771f68e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # bytes response with */* content type should be treated as binary | ||
|
|
||
| When a response has `*/*` content type and a `bytes` body, `isBinaryPayload` should return | ||
| `true` so the response is deserialized as `Uint8Array` (binary) rather than `string` (base64). | ||
|
|
||
| ## TypeSpec | ||
|
|
||
| ```tsp | ||
| @post op uploadFile( | ||
| @body body: string | ||
| ): { | ||
| @header contentType: "*/*", | ||
| @body body: bytes | ||
| }; | ||
| ``` | ||
|
|
||
| ## Operations | ||
|
|
||
| ```ts operations | ||
| import { TestingContext as Client } from "./index.js"; | ||
| import { getBinaryResponse } from "../static-helpers/serialization/get-binary-response.js"; | ||
| import { UploadFileOptionalParams } from "./options.js"; | ||
| import { | ||
| StreamableMethod, | ||
| PathUncheckedResponse, | ||
| createRestError, | ||
| operationOptionsToRequestParameters, | ||
| } from "@azure-rest/core-client"; | ||
|
|
||
| export function _uploadFileSend( | ||
| context: Client, | ||
| body: string, | ||
| options: UploadFileOptionalParams = { requestOptions: {} }, | ||
| ): StreamableMethod { | ||
| return context | ||
| .path("/") | ||
| .post({ | ||
| ...operationOptionsToRequestParameters(options), | ||
| contentType: "text/plain", | ||
| headers: { accept: "*/*", ...options.requestOptions?.headers }, | ||
| body: body, | ||
| }); | ||
| } | ||
|
|
||
| export async function _uploadFileDeserialize(result: PathUncheckedResponse): Promise<Uint8Array> { | ||
| const expectedStatuses = ["200"]; | ||
| if (!expectedStatuses.includes(result.status)) { | ||
| throw createRestError(result); | ||
| } | ||
|
|
||
| return result.body; | ||
| } | ||
|
|
||
| export async function uploadFile( | ||
| context: Client, | ||
| body: string, | ||
| options: UploadFileOptionalParams = { requestOptions: {} }, | ||
| ): Promise<Uint8Array> { | ||
| const streamableMethod = _uploadFileSend(context, body, options); | ||
| const result = await getBinaryResponse(streamableMethod); | ||
| return _uploadFileDeserialize(result); | ||
| } | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -214,7 +214,7 @@ describe("Responses.ts", () => { | |
| ` | ||
| ); | ||
| }); | ||
| it("@header contentType text/plain should keep format to byte(finally string)", async () => { | ||
| it("@header contentType text/plain with bytes body should be treated as binary", async () => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot why current change will impact rlc ut?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is intentional and consistent with the reviewer's original |
||
| const responses = await emitResponsesFromTypeSpec(` | ||
| @get op read(): {@header contentType: "text/plain", @body body: bytes}; | ||
| `); | ||
|
|
@@ -232,7 +232,8 @@ describe("Responses.ts", () => { | |
| /** The request has succeeded. */ | ||
| export interface Read200Response extends HttpResponse { | ||
| status: "200"; | ||
| body: string; | ||
| /** Value may contain any sequence of octets */ | ||
| body: Uint8Array; | ||
| headers: RawHttpHeaders & Read200Headers; | ||
| } | ||
| ` | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.