fix(http-server-csharp): handle void success responses - #11905
fix(http-server-csharp): handle void success responses#11905sophia-ramsey wants to merge 3 commits into
Conversation
Co-authored-by: Copilot App <[email protected]>
commit: |
|
All changed packages have been documented.
Show changes
|
|
You can try these changes here
|
There was a problem hiding this comment.
🟢 Approval recommended
The change is a targeted emitter bug fix with an appropriate .chronus entry and added tests/snapshot updates that directly cover the new behavior.
Pull request overview
This PR fixes C# controller generation in @typespec/http-server-csharp so that operations whose success type is void but also include @error union variants are emitted as bodyless success responses (HTTP 204 No Content), rather than incorrectly generating Ok(result).
Changes:
- Updated success-response analysis to skip
@errorunion variants and prefer204/no-body when the only success variant isvoid. - Wired the controller action generator to pass
programinto response analysis for proper@errordetection. - Added/updated tests and snapshots to verify the generated controller code for
void | @errorand value-or-error unions.
File summaries
| File | Description |
|---|---|
| packages/http-server-csharp/test/snapshots/sample-service/generated/controllers/PetsController.cs | Snapshot update reflecting NoContent() + 204 for a void success union case. |
| packages/http-server-csharp/src/components/controller-action/response-analysis.ts | Adjusted getSuccessStatusCode to use isErrorModel(program, ...) and return 204/no-body for void success unions. |
| packages/http-server-csharp/src/components/controller-action/controller-action.tsx | Updated callsite to pass $.program into getSuccessStatusCode. |
| packages/http-server-csharp/src/components/controller-action/controller-action.test.tsx | Added coverage for `void |
| .chronus/changes/csharp-void-success-2026-09-08.md | Added a fix changelog entry for the user-visible emitter behavior change. |
Review details
- Files reviewed: 4/5 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
🟡 Changes recommended
Scalar success variants in unions may incorrectly produce 204, and one test does not exercise the new union path.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
packages/http-server-csharp/src/components/controller-action/controller-action.test.tsx:147
- This test instantiates
ServiceOperation<string>, so the operation has novoidsuccess variant andgetSuccessStatusCodeuses the unchanged fallback path; the expectation therefore also passes with the pre-change implementation. Instantiate a value-plus-void union (for exampleServiceOperation<string | void>) or add a separate case so the new union handling is actually exercised and the scalar regression is covered.
- Files reviewed: 4/5 changed files
- Comments generated: 1
- Review effort level: Lite
There was a problem hiding this comment.
🟡 Changes recommended
Response analysis still has unresolved cases that can produce incorrect metadata or non-compiling generated controllers.
Get a fresh assessment by requesting another Copilot review.
Review details
Suppressed comments (5)
packages/http-server-csharp/src/components/controller-action/response-analysis.ts:43
- This returns immediately for a bodyless success model even when another union variant is a value. For
NoContentResponse | string(or the reverse order),hasBodybecomes false whilegetSuccessReturnTypeproducesTask<string>, so the generated action discards the value and always returnsNoContent(); keep scanning and prefer a body/value success when one exists.
for (const variant of type.variants.values()) {
const result = analyzeVariant(variant.type);
if (result !== undefined) return result;
}
packages/http-server-csharp/src/components/controller-action/response-analysis.ts:49
getSuccessReturnTypestill treats a model named exactlyErroras an error (return-type-helpers.ts:26-34), but this branch only callsisErrorModel. Forvoid | Errorwithout an@errordecorator, the interface is generated asTaskwhilehasBodyis true, so the controller emitsvar result = await ...against a non-genericTaskand fails to compile; apply the same name-convention check here.
if (isErrorModel(program, type)) return undefined;
packages/http-server-csharp/src/components/controller-action/response-analysis.ts:67
- When every variant is an error, neither
hasValueSuccessnorhasVoidSuccessis set, so this falls through to the generic{ statusCode: 200, hasBody: true }result below.getSuccessReturnTypenow returnsundefinedfor the same union, causing aTaskbusiness method but controller code that assignsvar resultand callsOk(result), which does not compile; handle the no-success case consistently and add an error-only-union regression test.
if (hasValueSuccess) {
return { statusCode: 200, hasBody: true };
}
if (hasVoidSuccess) {
return { statusCode: 204, hasBody: false };
packages/http-server-csharp/src/components/controller-action/response-analysis.ts:51
- This filters
@errormodels for union analysis, but direct error return types still take the direct-model path above and are treated as body-bearing successes. BecausegetSuccessReturnTypenow returns no success type for a direct@errormodel, the generated interface isTaskwhile the controller still emits a result assignment/Ok(result); apply the same no-success handling to direct error models and cover that input.
if (type.kind === "Model") {
// Skip models with @error decorator or error-range status codes
if (isErrorModel(program, type)) return undefined;
const result = analyzeResponseModel(type);
if (result.statusCode !== undefined && result.statusCode >= 400) return undefined;
packages/http-server-csharp/src/utils/return-type-helpers.ts:29
- This now returns
undefinedfor a direct, body-bearing@errormodel, whilegetSuccessStatusCodestill treats a direct model as bodyful (response-analysis.ts:18-20). The interface therefore emitsTask, but the controller emitsvar result = await ..., which does not compile for an error-only operation. Keep direct-model response analysis consistent (or explicitly handle/reject error-only operations) and add a regression test.
if (type.kind === "Model") {
try {
if (isErrorModel(program, type)) return undefined;
- Files reviewed: 6/7 changed files
- Comments generated: 1
- Review effort level: Lite
| const result = analyzeVariant(returnType); | ||
| if (result !== undefined) { | ||
| return result; | ||
| } | ||
| if (hasValueSuccess) { |
This pull request improves the handling of union return types that include
voidand@errorresponses in generated C# controllers. The main fix ensures that when an operation can return eithervoid(success) or an error, the generated controller method will correctly treat the success case as a bodyless response (HTTP 204 No Content), aligning with C# and HTTP conventions. Tests are added to verify this behavior, and the response analysis logic is updated accordingly.C# Controller Generation Fixes:
void | @errorare generated as bodyless success responses (HTTP 204 No Content) in C# controllers. (.chronus/changes/csharp-void-success-2026-09-08.md)getSuccessStatusCodeto detect unions withvoidand error types, returning the correct status code and body handling. (response-analysis.ts)Testing Improvements:
void | @errorunions, the controller does not assign a result variable and returnsNoContent(), and for value unions, it preserves result handling and returns the value. (controller-action.test.tsx)Internal Refactoring:
getSuccessStatusCodefunction signature and its usage to accept theprogramparameter, supporting improved error model detection. (controller-action.tsx,response-analysis.ts) [1] [2]