当本地人为swagger

当本地人为swagger

本文介绍了当本地人为swagger.json提供服务时,Autorest无法针对文件解析swagger.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当swagger.json托管在网站上时,我可以使用Autorest_core 3生成客户端,但是当托管在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.

但是,如果我将本地主机中的swagger.json剪切并粘贴到文件中,则可以生成客户端.

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

在startup.ConfigureServices中,我拥有

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);

然后在Startup.Configure我有

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();
        });
    }

当我启动api并尝试生成客户端时

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

我得到以下输出

https://aka.ms/autorest
   Loading AutoRest core      'C:\Users\kirst\.autorest\@[email protected]\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/'

但是以下方法确实有效

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

我早些时候曾认为我的问题可能与我使用的哪个版本的autorest有关,因此我对该问题进行了广泛的编辑.我实际上不清楚是否可以使用autorest v2从localhost swagger.json生成

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

我只是发现,如果我将swagger.json剪切并粘贴到文件中,便可以从本地主机生成文件.我希望不必这样做.

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.

可悲的是,输出的 https://aka.ms/autorest 给出了404

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

[更新]

我尝试使用http前缀

I tried prefixing with http

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

类似于https

如果我浏览到 http://localhost:44338/swagger/v1/swagger.json 我收到错误消息

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

This site can't be reached

如果我浏览到 https://localhost:44338/swagger/v1/swagger.json 它将重定向到localhost:44338/swagger/v1/swagger.json

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

[更新]

我未选中ssl并成功运行了以下内容.

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

推荐答案

http添加到--input-file之后,该问题为我解决了:

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

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

更新

就HTTPs/TLS而言,如果正确配置了HTTPs/TLS且证书来自受信任的CA,则自动停止将自动工作.

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.

如果使用自签名证书进行开发,则需要执行额外的步骤才能在NodeJS中使用自签名证书:

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

  1. 已安装且受信任的开发证书
  2. NODE_TLS_REJECT_UNAUTHORIZED系统变量设置为0
  3. 关闭并重新启动所有控制台
  1. Installed and trusted development certificate
  2. Set NODE_TLS_REJECT_UNAUTHORIZED system variable to 0
  3. Close and restart all consoles

这篇关于当本地人为swagger.json提供服务时,Autorest无法针对文件解析swagger.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 09:08