question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swagger file generated in cli missing paths, components (Swashbuckle.AspNetCore.Cli 5.1.0)

See original GitHub issue

During the build process we are trying to generate the swagger.json file. We are using the following command:

dotnet swagger tofile .\bin\Debug\netcoreapp3.1\myWebService.dll v1

The output that gets generated:

{
  "openapi": "3.0.1",
  "info": {
    "title": "myWebService",
    "description": "myWebService",
    "contact": {
      "name": "John Doe",
      "email": "john.doe@example.com"
    },
    "license": {
      "name": "MIT",
      "url": "https://opensource.org/licenses/MIT"
    },
    "version": "1.0"
  },
  "paths": { },
  "components": { }
}

As you can see, the paths and components are empty. There are no errors or warnings shown from the CLI tool.

However, if we take the swagger.json from the service (http://localhost:57016/swagger/v1/swagger.json), it includes all the relevant paths and components. image

Can you please clarify how can we achieve the same output in CLI as from the web service?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
RealGeckocommented, Oct 26, 2022

Hello,

I’m not sure if it will help, but let’s give it a try.

I had a similar problem, but with custom SwaggerHostFactory.CreateHost. The dotnet swagger tofile (...) returned incomplete swagger definition:

{                                                                                     
  "openapi": "3.0.1",                                                                 
  "info": {                                                                           
    "title": "(...)",                                         
    "version": "v1"                                                                   
  },                                                                                  
  "paths": { },                                                                       
  "components": {                                                                     
    "securitySchemes": {                                                              
      "Bearer": {                                                                     
        "type": "http",                                                               
        "description": "Bearer token for the API to authenticate incoming requests.", 
        "scheme": "bearer"                                                            
      }                                                                               
    }                                                                                 
  },                                                                                  
  "security": [                                                                       
    {                                                                                 
      "Bearer": [ ]                                                                   
    }                                                                                 
  ]                                                                                   
}

Everything worked just fine:

  • when I’ve accessed swagger-ui page
  • when I’ve manually called SwaggerHostFactory.CreateHost during application startup:
(...)
var host = SwaggerHostFactory.CreateHost();
var swaggerProvider = host.Services.GetRequiredService<ISwaggerProvider>();
(...)

To resolve this issue I had to set a value for application name, so finally my SwaggerHostFactory.CreateHost looks like that:

/// <summary>
/// Custom <see cref="IHost"/> factory used for API client generation.
/// </summary>
public class SwaggerHostFactory
{
    public static IHost CreateHost()
    {
        var builder = WebApplication.CreateBuilder(new WebApplicationOptions
        {
            ApplicationName = typeof(SwaggerHostFactory).Namespace // without it, actions won't be added to swagger file
        });

        builder.Services.AddControllers();
        builder.Services.AddEndpointsApiExplorer();
        
        builder.Services.AddSynergySwaggerGen(options =>
        {
            options.SwaggerDoc("v1", new() { Title = "...", Version = "v1" });
            (...)
        });
            
        var app = builder.Build();
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint(...);
            (...)
        });
        app.MapControllers();

        return app;
    }
}

Problem might be related to Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager which initializes Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager and calls void PopulateDefaultParts(string entryAssemblyName), where entryAssemblyName is set to environment?.ApplicationName. It is called only for not empty, or null values.

private static ApplicationPartManager GetApplicationPartManager(IServiceCollection services, IWebHostEnvironment? environment)
{
    var manager = GetServiceFromCollection<ApplicationPartManager>(services);
    if (manager == null)
    {
        manager = new ApplicationPartManager();

        var entryAssemblyName = environment?.ApplicationName;
        if (string.IsNullOrEmpty(entryAssemblyName))
        {
            return manager;
        }

        manager.PopulateDefaultParts(entryAssemblyName);
    }

    return manager;
}

Why do I think so?

var partManager = app.Services.GetRequiredService<ApplicationPartManager>();
var feature = new ControllerFeature();
partManager.PopulateFeature(feature);

During normal runtime, feature.Controllers has records for each controller from my project, and during dotnet swagger tofile (...) it is empty,

Cheers,

2reactions
mroberts91commented, Jan 2, 2023

I ran across this when using Swashbuckle.AspNetCore.Cli v6.4.0, in a net7.0 API project. When using var builder = WebApplication.CreateBuilder();, this results in the following empty Open API document.

{
  "openapi": "3.0.1",
  "info": {
    "title": "My API",
    "version": "V1"
  },
  "paths": { },
  "components": { }
}

But to resolve this, simply pass args to the CreateBuilder method. I found that this results in a fully built Open API file.

var builder = WebApplication.CreateBuilder(args);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error while generating swagger.json with Swashbuckle. ...
and my Program.cs file is: using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.Extensions.
Read more >
Aspnetcore 3.0 使用Swagger - microestc
The Swashbuckle CLI tool can retrieve Swagger JSON directly from your application startup assembly, and write it to file. This can be useful...
Read more >
Get started with Swashbuckle and ASP.NET Core
SwaggerGen : a Swagger generator that builds SwaggerDocument objects directly from your routes, controllers, and models. It's typically combined ...
Read more >
Swashbuckle CLI: Automating ASP.NET Core API Swagger ...
In this article, we will discuss automatically generating Swagger JSON files in ASP.NET Core via the Swashbuckle CLI. If you are an ASP....
Read more >
Swagger in ASP.Net Core (Using Swashbuckle.AspNetCore ...
Steps to follow: NuGet Package Manager: Install-Package Swashbuckle. AspNetCore -Version 5.1.0 Startup.cs Inside of ConfigureServices method, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found