Azure / autorest.csharp

Extension for AutoRest (https://github.com/Azure/autorest) that generates C# code
MIT License
142 stars 166 forks source link

csharp generator v3 does not generate .Net clients #1044

Closed borota closed 3 years ago

borota commented 3 years ago

I was prompted to upgrade AutoRest to latest. Generated code now targets Azure only

autorest readme.md

contents of readme.md

input-file: https://internal.site/api/swagger/docs/v0
output-folder: ..\..\OpenApi\Generated
add-credentials: true
use-internal-constructors: true
sync-methods: none
csharp:
  namespace: OpenApi

Expected behavior Auto-generated code targeting .NET

Additional context I didn't see anything anywhere about support for generating clients targeting .NET having been dropped. The only pointer I found was about using version=previous, if having problems with current version. What do I need to add to make sure vanilla .NET clients are being generated, using V3?

timotheeguerin commented 3 years ago

Hi @borota, not exactly sure what you mean by the code generate Azure only, could you detail more on that?

Chsarp generator was updated to using v3 recently, if you want to stick with v2 you can specify the --v2 flag when running or in your configuration file v2: true

borota commented 3 years ago

@timotheeguerin the generated code targets azure, not plain .net. The client generated depends on Azure libraries, not on Microsoft.Rest.ClientRuntime. Meanwhile I headed over to csharp generator issues https://github.com/Azure/autorest.csharp/issues/1029 and it is admitted V3 is missing vanilla .NET support, if I get that epic right.

namespace ServiceClient.OpenApi { internal partial class AuthRestClient { private Uri endpoint; private ClientDiagnostics _clientDiagnostics; private HttpPipeline _pipeline;

    /// <summary> Initializes a new instance of AuthenticationRestClient. </summary>
    /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param>
    /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param>
    /// <param name="endpoint"> server parameter. </param>
    public AuthRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null)

There doesn't seem to be any way out of generating that Azure targeting code.
timotheeguerin commented 3 years ago

hhm, I see, I believe this is expected as v3 use a rewrite of all the core libraries and I don't think there is any non Azure planned. I'll transfer this issue to the autorest.csharp repo where they can confirm this.

aanufriyev commented 3 years ago

Any updates on this? We are actively using AutoRest to generate internal clients and if it is confirmed that non-Azure template will be dropped we will have to plan switch now, instead of using --v2 flag for some time.

ShivangiReja commented 3 years ago

@timotheeguerin is correct. V3 generator depends on Azure.Core and we do not have any plan to support non Azure template.

NelsonLamprecht commented 3 years ago

@timotheeguerin is correct. V3 generator depends on Azure.Core and we do not have any plan to support non Azure template.

Confirming that this package is now only for consuming azure rest services and not any rest services? If so, I think it would be helpful for users to remove the section in the readme about "Use outside of the azure-sdk-net repo" and boldly proclaim autorest.csharp is not to be used to generate clients for non azure resources which appears to be a deviation from the proclaimed goals on the base autorest page: "The AutoRest tool generates client libraries for accessing RESTful web services".

Maybe this package should be renamed autorest.csharp-azure since they are comingled?

lmazuel commented 3 years ago

Hi @NelsonLamprecht

If you put aside the name of the runtime library, that indeed is called Azure.Core, what are the generated things you see that seems Azure specific to you? I want to separate the name of the runtime, with what actual code that is generated that you don't want to see.

@aanufriyev on this:

We are actively using AutoRest to generate internal clients and if it is confirmed that non-Azure template will be dropped we will have to plan switch now, instead of using --v2 flag for some time.

Same question for you, what do you see in the generated code that feels Azure specific to you (runtime naming put aside)

ShivangiReja commented 3 years ago

@aanufriyev and @NelsonLamprecht:

V3 generator depends on Azure.Core and we do not have any plan to support non Azure template.

I meant we depend on Azure.Core and we don't have any plan to support Microsoft.Rest.ClientRuntime(non Azure) library.

borota commented 3 years ago

@ShivangiReja @lmazuel Could you please create a sample console application running under Windows 10 (or Windows 2016) which uses a Rest client generated with V3? I am not able to.

borota commented 3 years ago
public ProductsRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null)
        {
            endpoint ??= new Uri("https://website/api");

            this.endpoint = endpoint;
            _clientDiagnostics = clientDiagnostics;
            _pipeline = pipeline;
        }

My guess is that a constructor like this can only be instantiated in an environment that either simulates Azure or it's some sort of Azure 'service'. My exposure to Azure is extremely limited so I may not have the right lingo. From a console app, where do I get clientDiagnostics or pipeline from? My guess is, this is something that can only be provided if run inside of an Azure function/worker/service, something like that. See what I am getting at? This client can't be used outside of an Azure environment, can it?

ShivangiReja commented 3 years ago

@borota Here I created a console application using autorest c# V3 generator. Please take a look.

borota commented 3 years ago

How interesting @ShivangiReja, thank you. I really like the way the generated code looks now in V3.

TheBoss3433 commented 3 years ago

@borota Here I created a console application using autorest c# V3 generator. Please take a look.

Where is the console? I just see a "console" being initialized but that does not tell us where the parameters come from...

ShivangiReja commented 3 years ago

V3 generator also generates .csproj check here, it references Azure.Core and Microsoft.Azure.AutoRest.CSharp. That's where these parameters come from.

TheBoss3433 commented 3 years ago

V3 generator also generates .csproj check here, it references Azure.Core and Microsoft.Azure.AutoRest.CSharp. That's where these parameters come from.

Thanks for your reply, I did more digging and the issue is with blazon

Saegusae commented 2 years ago

Hey, just checking in one year later, I am having the exact same issues, using this tool looks impossible to me outside an Azure environment. The documentation only shows azure-arm flavor of client initialization and nothing on how to provide those constructor parameters, namely ClientDiagnostics and HttpPipeline

Would appreciate input on this

PS. @ShivangiReja the repo you provided, does not initialize and use the generated client, or am I missing something?

simeyla commented 1 year ago

I tried to generate clients for Fedex API yesterday (obviously not an Azure service). The Typescript client generated interfaces but the csharp version just made a useless stub for me with no entities.

If anyone is interested in looking into it, the API is at https://developer.fedex.com/api/en-us/catalog/rate/v1/docs.html

The schema download link is top right of the page.

I used:

autorest --input-file=.\rate.json --csharp

I'm going back to NSwag, just wanted to try something new.