-
Notifications
You must be signed in to change notification settings - Fork 80
Fix multipart/mixed serialization - only apply file descriptors for multipart/form-data #3778
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/typespec-ts/test/modularUnit/scenarios/multipart/plainBody.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # Plain body without HttpPart should use default serialization | ||
|
|
||
| When a model is used with multipart content type but properties don't use `HttpPart<T>`, the serializer should use default handling (return the item as-is) rather than array-of-parts serialization. | ||
|
|
||
| This scenario represents cases like Azure Storage's `submitBatch` where the entire multipart content is assembled as a plain string body. | ||
|
|
||
| ## TypeSpec | ||
|
|
||
| ```tsp | ||
| model BatchRequest { | ||
| body: string; | ||
| } | ||
|
|
||
| @route("/batch") | ||
| op submitBatch(@body body: BatchRequest): void; | ||
| ``` | ||
|
|
||
| ## Models | ||
|
|
||
| For a model without HttpPart properties, even if it's used with multipart, the serializer should just return the item unchanged: | ||
|
|
||
| ```ts models | ||
| /** | ||
| * This file contains only generated model types and their (de)serializers. | ||
| * Disable the following rules for internal models with '_' prefix and deserializers which require 'any' for raw JSON input. | ||
| */ | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
| /** model interface BatchRequest */ | ||
| export interface BatchRequest { | ||
| body: string; | ||
| } | ||
|
|
||
| export function batchRequestSerializer(item: BatchRequest): any { | ||
| return { body: item["body"] }; | ||
| } | ||
| ``` | ||
|
|
||
| ## Operations | ||
|
|
||
| ```ts operations | ||
| import { TestingContext as Client } from "./index.js"; | ||
| import { BatchRequest, batchRequestSerializer } from "../models/models.js"; | ||
| import { SubmitBatchOptionalParams } from "./options.js"; | ||
| import { | ||
| StreamableMethod, | ||
| PathUncheckedResponse, | ||
| createRestError, | ||
| operationOptionsToRequestParameters, | ||
| } from "@azure-rest/core-client"; | ||
|
|
||
| export function _submitBatchSend( | ||
| context: Client, | ||
| body: BatchRequest, | ||
| options: SubmitBatchOptionalParams = { requestOptions: {} }, | ||
| ): StreamableMethod { | ||
| return context | ||
| .path("/batch") | ||
| .post({ | ||
| ...operationOptionsToRequestParameters(options), | ||
| contentType: "application/json", | ||
| body: batchRequestSerializer(body), | ||
| }); | ||
| } | ||
|
|
||
| export async function _submitBatchDeserialize(result: PathUncheckedResponse): Promise<void> { | ||
| const expectedStatuses = ["204"]; | ||
| if (!expectedStatuses.includes(result.status)) { | ||
| throw createRestError(result); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| export async function submitBatch( | ||
| context: Client, | ||
| body: BatchRequest, | ||
| options: SubmitBatchOptionalParams = { requestOptions: {} }, | ||
| ): Promise<void> { | ||
| const result = await _submitBatchSend(context, body, options); | ||
| return _submitBatchDeserialize(result); | ||
| } | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot add a test case with multipart/mixed to verify the new behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added test case in
test/modularUnit/scenarios/multipart/plainBody.mdthat verifies models withoutHttpPart<T>properties use default serialization instead of array-of-parts serialization. The test demonstrates that plain body models generate{ body: item["body"] }rather than[createFilePartDescriptor(...)]. Commit: 29ade60