[英]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
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 中使用自签名证书:
NODE_TLS_REJECT_UNAUTHORIZED
system variable to 0将NODE_TLS_REJECT_UNAUTHORIZED
系统变量设置为 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.