Search code examples
c#stringbuilderaspnetboilerplateswashbuckle.aspnetcore

Swashbuckle Swagger Generator: StringBuilder out of Memory


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.


Solution

  • 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 cause. We were able to remove this dependency by marking the method as private.