简体   繁体   English

当本地主机提供 swagger.json 时,Autorest 无法针对文件解析 swagger.json

[英]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.当 swagger.json 托管在网站上时,我可以使用 Autorest_core 3 生成客户端,但当它托管在 localhost 上时则不能。

However if I cut and paste the swagger.json from local host into a file then I can generate the client.但是,如果我将本地主机中的 swagger.json 剪切并粘贴到文件中,则可以生成客户端。

In startup.ConfigureServices I have在 startup.ConfigureServices 我有

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在 Startup.Configure 我有

 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当我启动 api 并尝试生成客户端时

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.我已经广泛地编辑了这个问题,因为我之前认为我的问题可能与我使用的 autorest 版本有关。 I am not actually clear whether I could generate from localhost swagger.json using autorest v2我实际上不清楚是否可以使用 autorest v2 从 localhost swagger.json 生成

I only just discovered that I can generate from local host if I cut and paste swagger.json to a file.我只是发现如果我将 swagger.json 剪切并粘贴到文件中,我可以从本地主机生成。 I would prefer not to have to do that.我宁愿不必这样做。

Sadly the https://aka.ms/autorest that is output gives a 404可悲的是,输出的https://aka.ms/autorest给出了 404

[Update] [更新]

I tried prefixing with http我试过用 http 作为前缀

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

similar with https类似于 https

If I browse to http://localhost:44338/swagger/v1/swagger.json I get an error如果我浏览到http://localhost:44338/swagger/v1/swagger.json我会收到一个错误

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如果我浏览到https://localhost:44338/swagger/v1/swagger.json它会重定向到 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在项目调试选项卡中,我有

应用网址

[Update] [更新]

I unchecked ssl and ran the following successfully.我取消选中 ssl 并成功运行了以下内容。

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如果我点击我看到的网址不安全的 url 消息

After adding http to --input-file , the issue solved for me:http添加到--input-file ,问题为我解决了:

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.在 HTTPs / TLS 方面,如果 HTTPs / TLS 配置正确并且证书来自受信任的 CA,则 autorest 将自动工作。

If using a self-signed certificate for development, extra steps are required to allow using self-signed certificate in NodeJS:如果使用自签名证书进行开发,则需要额外的步骤来允许在 NodeJS 中使用自签名证书:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swagger 不生成 swagger.json - Swagger is not generating swagger.json 显示 Swagger.json 但 UI 不显示 - Swagger.json is displayed but the UI not Swagger 'swagger.json' 加载,但在 AspNet 项目中的 swagger UI '{localhost}/swagger' 上出现 404 错误 - Swagger 'swagger.json' loads, but 404 error on swagger UI '{localhost}/swagger' in AspNet project .net core/swagger - 生成 swagger.json 文件时如何绕过控制器文件? - .net core/swagger - How can I around a controller file when generating the swagger.json file? ASP.NET Core - Swashbuckle 未创建 swagger.json 文件 - ASP.NET Core - Swashbuckle not creating swagger.json file Swashbuckle 错误 - 在 IIS 中运行时出现“swagger.json”的 404 - Swashbuckle Error - 404 for 'swagger.json' when running in IIS Blazor Swagger:无法加载 API 定义。 获取错误:未定义 /swagger/v1/swagger.json - Blazor Swagger: Failed to load API definition. Fetch error: undefined /swagger/v1/swagger.json 从基础 controller 继承 controller 操作时生成 swagger.json 文件的问题 - Issue generating swagger.json file when inheriting controller actions from a base controller 使用 GenerateSchema 添加的类出现在 Swagger UI 中,但不在 swagger.json 中 - Classes Added Using GenerateSchema Appear in the Swagger UI But Not in swagger.json 未能加载 API 定义,获取错误:未定义 /swagger/v1/swagger.json - Failed to load API definition, Fetch error: undefined /swagger/v1/swagger.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM