Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

c# - Autorest Failed resolving swagger.json against file when swagger.json is served by localhost

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:Userskirst.autorest@[email protected]
ode_modules@autorestcoredist' (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:Userskirstsource
eposDogssrcswagger.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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...