microsoft / microsoft/typespec

[Bug][http-client-csharp] NoAuth with OAuth2Auth emits Client that still requires credentials

Open
#9,811 1 comment 0 reactions 0 assignees View on GitHub
bug emitter:client:csharp
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Describe the bug

In some scenarios, a service can support both authenticated and unauthenticated requests.

If I have a service namespace with the following:
```typespec
@useAuth(
NoAuth | OAuth2Auth<[
{
type: OAuth2FlowType.implicit,
authorizationUrl: "https://login.microsoftonline.com/common/oauth2/authorize",
scopes: ["https://monitor.azure.com/.default"],
}
]>
)
```

The `http-client-csharp` emitter will emit a client where TokenCredential is required. Example constructor:

```csharp
public ApplicationInsightsClient(Uri endpoint, TokenCredential credential, ApplicationInsightsClientOptions options)
{
Argument.AssertNotNull(endpoint, nameof(endpoint));
Argument.AssertNotNull(credential, nameof(credential));

options ??= new ApplicationInsightsClientOptions();

_endpoint = endpoint;
_tokenCredential = credential;
Pipeline = HttpPipelineBuilder.Build(options, new HttpPipelinePolicy[] { new BearerTokenAuthenticationPolicy(_tokenCredential, AuthorizationScopes) });
_apiVersion = options.Version;
ClientDiagnostics = new ClientDiagnostics(options, true);
}
```

The emitter will also give the warning: `main.tsp:28:11 - warning @typespec/http-client-csharp/unsupported-auth: un-supported authentication scheme noAuth`

Can we make it so that the credential doesn't need to be specified for unauthenticated requests? Similar issue in the python emitter: https://github.com/microsoft/typespec/issues/9238

### Reproduction

Using the `http-client-csharp` emitter with:

```typespec
@useAuth(
NoAuth | OAuth2Auth<[
{
type: OAuth2FlowType.implicit,
authorizationUrl: "https://login.microsoftonline.com/common/oauth2/authorize",
scopes: ["https://monitor.azure.com/.default"],
}
]>
)
@service(#{ title: "Azure Monitor Exporter" })
@versioned(Versions)
@server(
"{host}/{apiVersion}",
"Azure MonitorOpenTelemetry Exporter Service",
{
/** Application Insights' Breeze endpoint. */
host: url = "https://dc.services.visualstudio.com",

/** The service API version. */
apiVersion: Versions,
}
)
namespace AzureMonitorExporter;
```

### 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.