domaindrivendev / Swashbuckle.WebApi

Seamlessly adds a swagger to WebApi projects!
BSD 3-Clause "New" or "Revised" License
3.07k stars 679 forks source link

NullReferenceException on loading #1039

Open griggsrl opened 7 years ago

griggsrl commented 7 years ago

Hi,

We're currently encountering a problem when trying to access our documentation. We currently host a WebAPI2 project in a Webforms web application which has worked well for us for nearly a year now. We are using Swashbuckle 5.2.2 but I did update to the current version to see if it would address the issue. It didn't happen to magically fix the issue so it is now reverted back to 5.2.2 since we're about to make a release.

A number of weeks ago all of a sudden the documentation started throwing exceptions when attempting to open it. The only information we've been able to gather so far is the following which is output to the browser:

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Object reference not set to an instance of an object.</ExceptionMessage>
<ExceptionType>System.NullReferenceException</ExceptionType>
<StackTrace>   at ᜄ.ᜀ.<>c.ᜀ(Task`1 A_0)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Cors.CorsMessageHandler.<SendAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()
</StackTrace>
</Error>

We have identified the build at which it stopped working and have inspected the code commit that triggered the build. However, there were no commits to the WebAPI project at all, nor anything other than some CSS in the main webapp which wouldn't affect the Swagger documentation.

We have compared the web application folders between the two builds and the only differences other than the minor CSS differences are the project assemblies that got recompiled. Each build is built fresh from source control.

I've inspected the WebAPI2 project assembly and the custom swagger resource files (an image, html, and css file) are embedded correctly in both builds.

Here is our SwaggerConfig content (a few identifying names changed):

            const string webApiXmlDocFileName = "WebAPI2.xml";
            const string modelXmlDocFileName = "Core.xml";

            GlobalConfiguration.Configuration
                .EnableSwagger("api/docs/{apiVersion}", c =>
                {
                    c.IgnoreObsoleteActions();
                    c.IgnoreObsoleteProperties();
                    c.DescribeAllEnumsAsStrings();
                    c.IncludeXmlComments(webApiXmlDocFileName);
                    c.IncludeXmlComments(modelXmlDocFileName);
                    c.DocumentFilter<AlphabeticSortOperationFilter>();

                    c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                    c.SingleApiVersion("v1", "API Reference");

                    c.Schemes(new[] { "http", "https" });
                    c.OAuth2("oauth2")
                        .Description("OAuth2 Implicit Grant")
                        .TokenUrl("/api/oauth2/token")
                        .Flow("password")
                        .Scopes(scopes =>
                        {
                            scopes.Add("clientid", "try out simple api");
                        });
                    c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
                })
                .EnableSwaggerUi("api/docs/ui/{*assetPath}", c =>
                {
                    // Specify our custom templates isntead of defaults
                    c.InjectStylesheet(Assembly.GetExecutingAssembly(), "WebAPI2.SwashbuckleExtensions.index.css");
                    c.CustomAsset("index", Assembly.GetExecutingAssembly(), "WebAPI2.SwashbuckleExtensions.index.html");
                    c.CustomAsset("logo", Assembly.GetExecutingAssembly(), "WebAPI2.SwashbuckleExtensions.Logo-white.png");
                    c.EnableOAuth2Support("clientid", "realm", "project");
                    c.DisableValidator();
                });

We aren't getting anything in the logs and walking through all of our code doesn't expose the error on our end.

Does anyone have any suggestions on how to further dig into this? We're kind of at a loss here since we can't capture where the error is coming from in the request routing. Any help would be appreciated.

griggsrl commented 7 years ago

I finally tracked down a little more info that might shed some light on this issue.

We use a 3rd party control in our application that it looks like hosts its own WebAPI instance in order to handle its own RESTful calls. The error described above occurs when a specific method of theirs is called during initialization of the webapp. I have contacted them to see if this call is required or what the ramifications of not making it might be.

However, here's a question: is there a way to have Swashbuckle ignore an assembly or class entirely? Since it's 3rd party we can't modify it to add custom attributes and using filters won't work as the exception is already thrown at that point.

AnilCAE commented 7 years ago

Stackoflow link of the issue: http://stackoverflow.com/questions/43186510/swashbuckle-documentation-nullreferenceexception-on-load

heldersepu commented 7 years ago

Can someone please create a simple project reproducing this error?