Search code examples
openapiautorest

AutoRest generates the client ctor as internal + ignores the credentials flag


I am generating a C# REST service client via AutoRest (OpenAPI v3). I'm generating the client by running the following command:

autorest --input-file="./Resources/swagger.json" --output-folder="./SomeService/Generated" --namespace="SomeService.Client" --override-client-name="SomeServiceClient" --skip-csproj --public-clients=true --add-credential --csharp

The generated client I get seems to have two issues:

  1. The ctor is generated as internal, even though I specified that the client should be public:
internal SomeServiceClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null)
{
    RestClient = new SomeServiceRestClient(clientDiagnostics, pipeline, endpoint);
    _clientDiagnostics = clientDiagnostics;
    _pipeline = pipeline;
}
  1. The generated client ctor does not accept credentials, even though I specified the --add-credential flag, why's that?

I followed the instructions mentioned on https://github.com/Azure/autorest . What am I doing wrong?


Solution

    1. Looking into the AutoRest source code, it will only create public constructors if the security schema in the OpenAPI spec is set to AzureKey or AADtoken. This is verified in their docs AutoRest source code
      AutoRest Security Schemes documentation

    2. The table for AutoRest flags says that the --add-credential does nothing for the .NET clients (4th column from the left).
      https://github.com/Azure/autorest/blob/main/docs/generate/flags.md#shared-flags

    It appears that AutoRest v3 is only for APIs hosted on Azure itself. With the evidence above, as well as the fact that required parameters for constructors of generated clients, like ClientDiagnostics, are declared internal, I think we're going to have to find another API client generator.

    UPDATE 6/13/2023

    The MSAL library has added support for non-Azure authentication with the addition of WithGenericAuthority() configuration option. Unfortunately it's not straightforward. Because of other necessary classes in Azure.Identity and Microsoft.Identity.Client being marked internal, you have to do a little bit of re-creation. I have posted an example repository that shows how to get it working. I currently am using it in production code for an internally-hosted API utilizing AWS Cognito for authentication.