How do I generate the swagger file, swagger CLI isn't supported with .net 8

204 views Asked by At

Since swagger CLI (swashbuckle.aspnetcore.cli v6.5.0) isn't supported with .net 8 (yet), how do I generate the swagger file (Open API specifications file) in post build event in .net 8 project?

I've tried writing the swagger file programmatically using a custom helper method that reads the openAPI document using ISwaggerProvider service and then converts it to JSON.

internal static void SaveSwaggerJson(this IServiceProvider provider)
{
    ISwaggerProvider swaggerService = provider.GetRequiredService<ISwaggerProvider>();
    OpenApiDocument doc = swaggerService.GetSwagger("v1", null, "/");
    string swaggerFile = doc.SerializeAsJson(Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0);
    File.WriteAllText("swagger_v1.json", swaggerFile);
}

And then I called it in program.cs:

app.Services.SaveSwaggerJson();

But the issue associated with this is that during pipeline-build stage, I don't have the write access hence the generation of swagger.json fails/it doesn't appear in the artifacts library of the build pipeline.

I've gone through multiple stackoverflow threads and nothing seemed to work.

0

There are 0 answers