Skip to content
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

[http-server-csharp]: Fix routing issues with MFD requests #5505

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .chronus/changes/mfd-01-2025-0-7-2-3-57.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/http-server-csharp"
---

[http-server-csharp]: Fix routing issues with MFD requests
20 changes: 12 additions & 8 deletions packages/http-server-csharp/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
const multipart: boolean = this.#isMultipartRequest(httpOperation);
const declParams = !multipart
? this.#emitHttpOperationParameters(httpOperation)
: this.#emitHttpOperationParameters(httpOperation, "HttpRequest request, Stream body");
: this.#emitHttpOperationParameters(httpOperation, true);

if (multipart) {
const context = this.emitter.getContext();
Expand Down Expand Up @@ -720,14 +720,14 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
${this.emitter.emitOperationReturnType(operation)}
public virtual async Task<IActionResult> ${operationName}(${declParams})
{
var boundary = request.GetMultipartBoundary();
var boundary = Request.GetMultipartBoundary();
if (boundary == null)
{
return BadRequest("Request missing multipart boundary");
}


var reader = new MultipartReader(boundary, body);
var reader = new MultipartReader(boundary, Request.Body);
${
hasResponseValue
? `var result = await ${this.emitter.getContext().resourceName}Impl.${operationName}Async(${this.#emitOperationCallParameters(httpOperation, "reader")});
Expand Down Expand Up @@ -912,7 +912,7 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)

#emitHttpOperationParameters(
operation: HttpOperation,
bodyParameter?: string,
bodyParameter?: boolean,
): EmitterOutput<string> {
const signature = new StringBuilder();
const bodyParam = operation.parameters.body;
Expand All @@ -930,10 +930,16 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
for (const parameter of requiredParams) {
signature.push(
code`${this.#emitOperationSignatureParameter(operation, parameter)}${
++i < requiredParams.length || bodyParam !== undefined ? ", " : ""
++i < requiredParams.length ? ", " : ""
}`,
);
}
if (
requiredParams.length > 0 &&
(optionalParams.length > 0 || (bodyParameter === undefined && bodyParam !== undefined))
) {
signature.push(code`, `);
}
if (bodyParameter === undefined) {
if (bodyParam !== undefined) {
signature.push(
Expand All @@ -942,11 +948,9 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
bodyParam.type,
Visibility.Create || Visibility.Update,
),
)} body${optionalParams.length > 0 ? ", " : ""}`,
)} body${requiredParams.length > 0 && optionalParams.length > 0 ? ", " : ""}`,
);
}
} else {
signature.push(code`${bodyParameter}${optionalParams.length > 0 ? ", " : ""}`);
}
i = 0;
for (const parameter of optionalParams) {
Expand Down
18 changes: 10 additions & 8 deletions packages/http-server-csharp/test/generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,17 +1123,19 @@ it("handles multipartBody requests and shared routes", async () => {
}

@sharedRoute
@route("/foo")
@route("/foo/{id}")
@post
op fooBinary(
@path id: string,
@header("content-type") contentType: "multipart/form-data",
@multipartBody body: FooRequest
): void;

@sharedRoute
@route("/foo")
@route("/foo/{id}")
@post
op fooJson(
@path id: string,
@header("content-type") contentType: "application/json",
@body body: FooJsonRequest
): void;
Expand All @@ -1154,18 +1156,18 @@ it("handles multipartBody requests and shared routes", async () => {
"using Microsoft.AspNetCore.WebUtilities;",
"using Microsoft.AspNetCore.Http.Extensions;",
`[Consumes("multipart/form-data")]`,
"public virtual async Task<IActionResult> FooBinary(HttpRequest request, Stream body)",
".FooBinaryAsync(reader)",
"public virtual async Task<IActionResult> FooJson(FooJsonRequest body)",
".FooJsonAsync(body)",
"public virtual async Task<IActionResult> FooBinary(string id)",
".FooBinaryAsync(id, reader)",
"public virtual async Task<IActionResult> FooJson(string id, FooJsonRequest body)",
".FooJsonAsync(id, body)",
],
],
[
"IContosoOperations.cs",
[
"using Microsoft.AspNetCore.WebUtilities;",
"Task FooBinaryAsync( MultipartReader reader);",
"Task FooJsonAsync( FooJsonRequest body);",
"Task FooBinaryAsync( string id, MultipartReader reader);",
"Task FooJsonAsync( string id, FooJsonRequest body);",
],
],
[
Expand Down
Loading