3

I am able to use Autorest_core 3 to generate the client when swagger.json is hosted on a website but not when it is hosted on localhost.

However if I cut and paste the swagger.json from local host into a file then I can generate the client.

In startup.ConfigureServices I have

services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });

            c.CustomOperationIds(  d => (d.ActionDescriptor as ControllerActionDescriptor)?.ActionName);

And in Startup.Configure I have

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();
        app.UseSwagger(c =>
        {
            c.RouteTemplate =
                "api-docs/{documentName}/swagger.json";
        });

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("./v1/swagger.json", "My API V1");
        });

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }   

When I start the api and try to generate the client

autorest --v3 --input-file=localhost:44338/swagger/v1/swagger.json --csharp --output-folder=generated --namespace=Connector

I get the following output

https://aka.ms/autorest
   Loading AutoRest core      'C:\Users\kirst\.autorest\@autorest_core@3.0.6262\node_modules\@autorest\core\dist' (3.0.6262)
   Loading AutoRest extension '@microsoft.azure/autorest.csharp' (~2.3.79->2.3.84)
   Loading AutoRest extension '@microsoft.azure/autorest.modeler' (2.3.55->2.3.55)
  Error: Failed resolving 'localhost:44338/swagger/v1/swagger.json' against 'file:///D:/Users/kirst/source/repos/Dogs/'

However the following does work

autorest --v3 --input-file=D:\Users\kirst\source\repos\Dogs\src\swagger.json --csharp --output-folder=generated --namespace=Connector

[Edit note]

I have edited this question extensively as I earlier on I thought my issue could be to do with which version of autorest I was using. I am not actually clear whether I could generate from localhost swagger.json using autorest v2

I only just discovered that I can generate from local host if I cut and paste swagger.json to a file. I would prefer not to have to do that.

Sadly the https://aka.ms/autorest that is output gives a 404

[Update]

I tried prefixing with http

Error: Could not read 'http://localhost:44338/swagger/v1/swagger.json'

similar with https

If I browse to http://localhost:44338/swagger/v1/swagger.json I get an error

This site can't be reached 

If I browse to https://localhost:44338/swagger/v1/swagger.json it redirects to localhost:44338/swagger/v1/swagger.json

I tried changing Configure as follows but it made no difference

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHttpsRedirection();
        }

In the project debug tab I have

app url

[Update]

I unchecked ssl and ran the following successfully.

autorest --v3 --input-file=http://localhost:60705/swagger/v1/swagger.json --csharp --output-folder=generated --namespace=Connector

if I click in the url I see an insecure url message

Kirsten
  • 15,730
  • 41
  • 179
  • 318

1 Answers1

3

After adding http to --input-file, the issue solved for me:

autorest --v3 --input-file=http://localhost:5000/swagger/v1/swagger.json --csharp

UPDATE

In terms of HTTPs / TLS, autorest will automatically work if the HTTPs / TLS is configured correctly as well as the certificate is from a trusted CA.

If using a self-signed certificate for development, extra steps are required to allow using self-signed certificate in NodeJS:

  1. Installed and trusted development certificate
  2. Set NODE_TLS_REJECT_UNAUTHORIZED system variable to 0
  3. Close and restart all consoles
weichch
  • 9,306
  • 1
  • 13
  • 25
  • 1
    @KirstenGreed it wouldn’t redirect you to a web address without http or https. You might see the address like that in your browsers address bar simply because the protocol is omitted. If you click into the address bar it would show you the full address with the protocol. In your case the site is hosted over https by the looks of the error and the port number. The http version if you have one could be found in launchSettings.json, under Properties folder. – weichch Mar 28 '20 at 06:14
  • Yes. I updated the question. I wonder if there is a way to get it working with ssl. – Kirsten Mar 28 '20 at 06:22
  • @KirstenGreed See updated. Not sure if that would work you as well. – weichch Mar 28 '20 at 12:21
  • I dont know how to try them. plus seems insecure. – Kirsten Mar 29 '20 at 00:17
  • 1
    @KirstenGreed The reason you need to install and trust the certificate is because by default asp.net core uses a self signed certificate which should not be trusted by anyone else apart from yourself. The system variable is to make sure nodejs also can trust the self signed certificate. If you have an officially signed certificate by a trusted CA (you need to pay for that), you don’t need any of the steps listed and autorest should automatically work. – weichch Mar 29 '20 at 00:44
  • @KirstenGreed or if you deploy your app to a server which has SSL provided like Azure. You can use autorest against that remote address without problem. And you don’t need any of those steps. – weichch Mar 29 '20 at 00:47
  • Thanks. I included Set NODE_TLS_REJECT_UNAUTHORIZED=0 in my batch file and it works now. I must have already set up a certificate for localhost – Kirsten Mar 29 '20 at 00:59
  • Further question at https://stackoverflow.com/questions/61022795/generating-swagger-json-with-backwardly-compatible-enum – Kirsten Apr 04 '20 at 07:09
  • https://www.hanselman.com/blog/DevelopingLocallyWithASPNETCoreUnderHTTPSSSLAndSelfSignedCerts.aspx – Kirsten Sep 07 '20 at 21:51