2

We have a fairly sizable ASP.NET Boilerplate .NET Core 3.1 project and Swashbuckle appears to run out of memory when generating the Swagger JSON.

We encounter the following error:

RequestAborted: Exception of type 'System.OutOfMemoryException' was thrown.. Request Path: /swagger/v1/swagger.json
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Text.StringBuilder.ToString()
   at System.IO.StringWriter.ToString()
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
   at Abp.AspNetZeroCore.Web.Authentication.JwtBearer.JwtTokenMiddleware.<>c__DisplayClass0_0.<<UseJwtTokenMiddleware>b__0>d.MoveNext()

Naturally this indicates that the StringBuilder is not able to handle the size of JSON string. I'm not aware of any specific memory limitations of StringBuilder so not sure why this is failing.

André Haupt
  • 3,294
  • 5
  • 32
  • 57
  • 1
    StringBuilder is backed by an array chunks, when it calls `ToString` it tries to allocate into a string and is likely hitting the framework hard-limit of 2 gigs. Note that this is likely not the actual problem, as it would be hard to image Swashbuckle generating that much json. Id say this is a bug or a design issue, maybe you should take this to github – TheGeneral Sep 14 '21 at 11:04
  • @TheGeneral: I investigated that same angle and the whole JSON prior to encountering the issue was 1.7MB. Still troubleshooting this side. – André Haupt Sep 14 '21 at 11:18

1 Answers1

0

We tracked the issue down and it had to do with the following method on our controller / app service.

public MyEnum GetMyEnum(MyDto myDto, long? someValue = null)

It appears the default parameter value was the caused. We were able to remove this dependency by marking the method as private.

André Haupt
  • 3,294
  • 5
  • 32
  • 57