NSwag: [Regression] Swagger.json not generated after upgrade to 13.11.x in docker container

Hitting localhost:5000 when running locally (tried in windows, linux and macos) produces the proper output, but if packed in a docker container, it hangs on getting the swagger.json file. The sample works OK if using nswag 13.10.9. Not working w/ any of the 13.11.x.

It appears the problem is related to using an external (from nuget) class for a property type of the generated model. In the example we use nswag class, but we observed it with our own external assemblies.

test.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>Net5.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="NSwag.AspNetCore" Version="13.11.2" />
  </ItemGroup>
  <ItemGroup>
    <None Update="Dockerfile">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

Program.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Test
{
    [ApiController]
    [Route("/")]
    public class Program
    {
        [NonAction]
        public static async System.Threading.Tasks.Task Main(string[] args) => 
            await Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(a =>
            {
                a.UseStartup<Program>();
                a.UseUrls(string.Empty);
                a.UseKestrel(o => o.ListenAnyIP(5000));
            })
        .RunConsoleAsync();

        [NonAction]
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseRouting();
            app.UseOpenApi();
            app.UseSwaggerUi3();
            app.UseEndpoints(o => o.MapControllers());
        }

        [NonAction]
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddOpenApiDocument();
            services.AddMvcCore();
        }

        [HttpGet("{id}")]
        public TestDetail Get()
        {
            return new TestDetail();
        }

        public sealed class TestDetail
        {
            /// <summary>
            /// NSwag 13.11.x fails to generate the swagger.json when a reference to a type in another assembly is present in the model.
            /// This only occurs when hosting in a linux docker container.
            /// No issues when running locally or in Windows docker container.
            /// </summary>
            public NSwag.OpenApiContact Test { get; set; }

        }
    }
}

Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:5.0
EXPOSE 5000
COPY . .
ENTRYPOINT ["dotnet", "test.dll"]

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 19 (3 by maintainers)

Most upvoted comments

I still see this problem with v. 13.15.5. Ant idea how to more diagnose this? It is serious impediment for upgrading.

OK I think what is happening here. Because the app is in the root folder / and there’s a kind of a bug in Namotion.Reflection which is responsible for trying to find the external XML docs. The logic will make it try to find specific file pattern from program directory and all sub-directories, the container basically crashes because every single file inside the container will be searched.

Another workaround is to have the compiled binaries in separate folder like ´/app´ (which would be a good idea anyway).

@laya-alsh something goes terribly wrong when resolving external XML documentation. Following allows the generation to complete:

builder.Services.AddOpenApiDocument(options =>
{
     // local documentation should work, if needed
    options.UseXmlDocumentation = false;
     // probably only this is needed
    options.ResolveExternalXmlDocumentation = false;
});

@sloncho too late for you I guess, but I think you might have been experiencing the same thing.

@lahma This issue got hijacked. The original was about not being able to generate swagger at all if classes/models from referenced assemblies are used. This is still an issue.

Then someone added the OOM issue. I think this has to be a separate issue, as people get confused, and most probably the 2 issues are not related at all.

If you do the original docker thing, it’ll expose the original problem.

Btw - we solved the problem for ourselves by dropping the nswag all together and migrated to swash. This was showstopper for us.