Fix */* content type and bytes body treated as binary#3918
Fix */* content type and bytes body treated as binary#3918
*/* content type and bytes body treated as binary#3918Conversation
Agent-Logs-Url: https://github.com/Azure/autorest.typescript/sessions/dcd8e921-7e25-42ff-ae62-ab1ac46f3ca2 Co-authored-by: kazrael2119 <[email protected]>
*/* content type treated as binary when body is bytes
Agent-Logs-Url: https://github.com/Azure/autorest.typescript/sessions/30bcfa21-da06-4956-8647-a3372e0e848d Co-authored-by: kazrael2119 <[email protected]>
|
@copilot changes from |
…nit scenario test Agent-Logs-Url: https://github.com/Azure/autorest.typescript/sessions/e6e4c70e-0694-45e4-9a47-b449e0e7f6b4 Co-authored-by: kazrael2119 <[email protected]>
Done in commit
|
…serialization Agent-Logs-Url: https://github.com/Azure/autorest.typescript/sessions/91d71912-6e66-4c90-a423-3556edeb1ce8 Co-authored-by: kazrael2119 <[email protected]>
…to expect binary Agent-Logs-Url: https://github.com/Azure/autorest.typescript/sessions/742994b9-97a4-44ba-b546-564f2df98261 Co-authored-by: kazrael2119 <[email protected]>
Both |
*/* content type treated as binary when body is bytes*/* content type and bytes body treated as binary
| ); | ||
| }); | ||
| 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 () => { |
There was a problem hiding this comment.
isBinaryPayload is shared between the RLC (transformResponses.ts:176) and modular code paths. With ||, the loop condition becomes type === KnownMediaType.Binary || isBytes — so for text/plain + bytes body, knownMediaType("text/plain") returns KnownMediaType.Text (not Binary), but isBytes is true, making the whole expression true. That causes text/plain + bytes to be treated as binary (producing Uint8Array) in both RLC and modular paths, which is why the existing RLC test needed to be updated.
This is intentional and consistent with the reviewer's original || suggestion — when the body is declared as bytes, it should be treated as binary data regardless of the content type label.
|
@copilot fix ci failures |
…o avoid breaking encode/bytes CI Agent-Logs-Url: https://github.com/Azure/autorest.typescript/sessions/9d4ced5c-700f-4b59-a86e-fd6be759f54d Co-authored-by: kazrael2119 <[email protected]>
Fixed in commit The fix narrows the // Before: every bytes body regardless of content type → binary
type === KnownMediaType.Binary || isBytes
// After: bytes are binary for non-JSON/non-XML content types
type === KnownMediaType.Binary ||
(isBytes && type !== KnownMediaType.Json && type !== KnownMediaType.Xml)This preserves the intended behavior:
|
…out Json/Xml exclusion Agent-Logs-Url: https://github.com/Azure/autorest.typescript/sessions/f007bbff-dc83-4798-a3c0-78d64c39960c Co-authored-by: kazrael2119 <[email protected]>
parseMediaType("*/*")returnsundefined(wildcard not in the regex), soknownMediaType("*/*")falls through toKnownMediaType.Unknown. For operations with*/*content type and abytesbody, this caused the body to be serialized as JSON/string instead of raw binary.Changes
operationUtil.ts—isBinaryPayload: Adds an explicit early return for*/*+ bytes, treating it as binary. The loop condition uses||so that a payload is treated as binary if the content type is binary or if the body is a bytes type — hoists theisByteOrByteUnioncheck outside the loop with a null guard forundefinedbody.transformParameters.ts—transformRequestBody: When*/*is present in raw content types and body is bytes, injectsKnownMediaType.Binaryinto the mapped content types before passing togetSchemaForType. This fixes the RLC request body path, which doesn't go throughisBinaryPayload.test/modularUnit/scenarios/operations/bodyParam/bytesWithWildcardContentType.md: Added modularUnit scenario test verifying that a response with*/*content type and abytesbody is deserialized asUint8Array(binary) viagetBinaryResponse.test/unit/responsesGenerator.spec.ts: Updated thetext/plain + bytesresponse test to expectUint8Array(binary), consistent with the new||behavior where a bytes body is always treated as binary regardless of content type.