Skip to content

[Bug]: NoAuth with OAuth2Auth emits Client that still requires credentials #9238

@pvaneck

Description

@pvaneck

Describe the bug

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

If I have a service namespace with the following:

@useAuth(
  NoAuth | OAuth2Auth<[
    {
      type: OAuth2FlowType.implicit,
      authorizationUrl: "https://login.microsoftonline.com/common/oauth2/authorize",
      scopes: ["https://monitor.azure.com/.default"],
    }
  ]>
)

The Python emitter will generate a Client class and Configuration Class that both have credential as a non-optional positional parameter.

Example generation Configuration:

class AzureMonitorExporterClientConfiguration:  

    def __init__(
        self, credential: "TokenCredential", host: str = "https://dc.services.visualstudio.com", **kwargs: Any
    ) -> None:
        api_version: str = kwargs.pop("api_version", "v2.1")

        if credential is None:
            raise ValueError("Parameter 'credential' must not be None.")

        ...

Ideally, credential should be typed as Optional with a default of None.

Reproduction

Using the Python emitter with a namespace like:

@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingemitter:client:pythonIssue for the Python client emitter: @typespec/http-client-python

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions