问题
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
[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
回答1:
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:
- Installed and trusted development certificate
- Set
NODE_TLS_REJECT_UNAUTHORIZED
system variable to 0 - Close and restart all consoles
来源:https://stackoverflow.com/questions/60861020/autorest-failed-resolving-swagger-json-against-file-when-swagger-json-is-served