microsoft / microsoft/typespec

[Bug]: [http-server-csharp] Missing `using` directive for response type namespace when operation has no parameters

Open
#10,471 1 comment 0 reactions 1 assignee Claimed by @markcowl View on GitHub
bug emitter:service:csharp triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Describe the bug

When using `@typespec/http-server-csharp` to emit C# code for an HTTP GET operation that has **no parameters** and returns a model defined in a different namespace, the emitter fails to generate the required `using` directive for the response type's namespace in the controller and interface files.

This causes C# compilation errors because the response type cannot be resolved without the `using` statement.

For example, if a `ServerConfig` model is defined in `TestService.Models` namespace and a parameterless GET operation returns it from the `TestService.Routes` namespace, the generated controller file will be missing `using TestService.Models;`, resulting in a build error.

Operations **with** parameters do not exhibit this issue — the namespace import is generated correctly in those cases.

**Package version**: `@typespec/http-server-csharp@0.58.0-dev.3`

**Affected source file**: `packages/http-server-csharp/src/lib/service.ts`

### Reproduction

```typespec
// main.tsp
import "@typespec/http";
import "@typespec/rest";
import "@typespec/http-server-csharp";

using TypeSpec.Http;

@service(#{
title: "Test Service",
})
namespace TestService;

// Model in a sub-namespace
namespace TestService.Models {
model ServerConfig {
version: string;
}
}

// Route in a different sub-namespace
namespace TestService.Routes {
using TestService.Models;

@tag("ServerConfig")
@route("/server-configs")
interface ServerConfigs {
// Parameterless GET — triggers the bug
@get read(): ServerConfig;
}
}
```

Run `tsp compile .` with `@typespec/http-server-csharp` emitter configured.

**Expected behavior:**
The generated controller file (`ServerConfigsController.cs`) should include:

```csharp
using TestService.Models;
```

**Actual behavior:**
The `using TestService.Models;` directive is missing from the generated controller file, causing a C# compilation error:

```
error CS0246: The type or namespace name 'ServerConfig' could not be found (are you missing a using directive or an assembly reference?)
```

**Note:** Operations that have at least one parameter (e.g., `@get read(@path id: string): ServerConfig;`) correctly generate the `using` directive. The bug only occurs when the operation has zero parameters.

### Root Cause Analysis

In `service.ts`, there are two code paths that handle namespace resolution for response types:

1. **Operation response processing** (~line 588): When building return types from `httpOp.responses`, the namespace of the resolved response type is not added to the file's imports.
2. **Controller method generation** (~line 645): When generating the controller method body, the HTTP response model's namespace is not imported.

Both paths correctly resolve the response type but fail to register the namespace as an import on the emitted file. Operations with parameters happen to trigger a different code path that includes the namespace import as a side effect of parameter processing.

### Checklist

- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/Microsoft/typespec/discussions).
- [x] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.