Skip to content

[Bug]: C# emitter: non‑200/204 status codes (e.g., 202 Accepted) are ignored — controller always returns Ok(...) / NoContent() #9187

@srushti-learner

Description

@srushti-learner

Describe the bug

When an operation in TypeSpec is annotated to return status codes other than 200 or 204 (e.g., 202 Accepted, 201 Created, 4xx/5xx), the generated ASP.NET Core controller method still returns Ok(result) (or NoContent() when the response type is void). This appears to be due to hardcoded logic in the emitter:

// emitter snippet (as seen in generated code path)
const hasResponseValue = response.name !== "void";
const resultString = ${status === 204 ? "NoContent" : "Ok"};
return this.emitter.result.declaration(
operation.name,
code${doc ?${formatComment(doc)}: ""} [${getOperationVerbDecorator(httpOperation)}] [Route("${httpOperation.path}")] ${this.emitter.emitOperationReturnType(operation)} public virtual async Task<IActionResult> ${operationName}(${declParams}) { ${ hasResponseValue ?var result = await ${this.emitter.getContext().resourceName}Impl.${operationName}Async(${getBusinessLogicCallParameters(parameters)});
return ${resultString}(result);:await ${this.emitter.getContext().resourceName}Impl.${operationName}Async(${getBusinessLogicCallParameters(parameters)});
return ${resultString}(); } }
);
Because resultString selects only Ok or NoContent, any other intended status code is effectively lost.
Impact

202 Accepted (typical for long‑running operations) becomes 200 OK.
201 Created (resource creation) becomes 200 OK and no Location header is set.
Other outcome codes (e.g., 301/302, 400, 409, 500) are not represented correctly, undermining API semantics and client behavior.

Reproduction

https://github.com/microsoft/typespec/blob/main/packages/http-server-csharp/src/lib/service.ts
line 874

Checklist

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions