Swashbuckle.aspnetcore: XML comments NullReferenceException

Created on 25 Mar 2017  路  10Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

First of all, sorry to not send a PR with its test.. I hope you can sniff the problem quickly than I clone the repo and so on.

There is just one method with XML comments.

NullReferenceException: Object reference not set to an instance of an object.
Swashbuckle.AspNetCore.SwaggerGen.XmlCommentsOperationFilter ApplyPropertiesXmlToPropertyParameters
ApiParameterDescription

The API has 2 versions, and all others methods, so far, has no comments.
The whole XML is something like this (I just blurs something)

Company.Feature.Api.txt

Most helpful comment

Until the fix is included in a published package, the following should workaround the issue by defaulting any unset model metadata to be the string model.

```c#
public class SwaggerApiDescriptionProvider : IApiDescriptionProvider
{
readonly ModelMetadata @string;

public SwaggerApiDescriptionProvider( IModelMetadataProvider metadadataProvider ) =>
    @string = metadadataProvider.GetMetadataForType( typeof( string ) );

public int Order => 0;

public void OnProvidersExecuted( ApiDescriptionProviderContext context )
{
    foreach ( var result in context.Results )
    {
        foreach ( var parameter in result.ParameterDescriptions )
        {
            if ( parameter.ModelMetadata == null )
            {
                parameter.ModelMetadata = @string;
            }
        }
    }
}

public void OnProvidersExecuting( ApiDescriptionProviderContext context ) { }

}

To wire this up in your application, just use:

```c#
builder.Services.TryAddEnumerable(
  ServiceDescriptor.Transient<IApiDescriptionProvider, SwaggerApiDescriptionProvider>() );

The new API description provider will be run in addition to the built-in provider and fix-up null model metadata before Swagger does it's thing.

I hope that helps.

All 10 comments

I'm also facing this, but it seems to have been fixed in #315. Unfortunately, that change is apparently not in RC3.

It seems that I also meet this problem and I'm using the version 1.0.0-rc3 ,error is as follows:

NullReferenceException: Object reference not set to an instance of an object.
Swashbuckle.AspNetCore.SwaggerGen.XmlCommentsOperationFilter+<>c.<ApplyPropertiesXmlToPropertyParameters>b__11_0(ApiParameterDescription p)

Until the fix is included in a published package, the following should workaround the issue by defaulting any unset model metadata to be the string model.

```c#
public class SwaggerApiDescriptionProvider : IApiDescriptionProvider
{
readonly ModelMetadata @string;

public SwaggerApiDescriptionProvider( IModelMetadataProvider metadadataProvider ) =>
    @string = metadadataProvider.GetMetadataForType( typeof( string ) );

public int Order => 0;

public void OnProvidersExecuted( ApiDescriptionProviderContext context )
{
    foreach ( var result in context.Results )
    {
        foreach ( var parameter in result.ParameterDescriptions )
        {
            if ( parameter.ModelMetadata == null )
            {
                parameter.ModelMetadata = @string;
            }
        }
    }
}

public void OnProvidersExecuting( ApiDescriptionProviderContext context ) { }

}

To wire this up in your application, just use:

```c#
builder.Services.TryAddEnumerable(
  ServiceDescriptor.Transient<IApiDescriptionProvider, SwaggerApiDescriptionProvider>() );

The new API description provider will be run in addition to the built-in provider and fix-up null model metadata before Swagger does it's thing.

I hope that helps.

When will a new release be available so we don't have to use the work around?

@commonsensesoftware I receive an error when I put your code into my application. In the SwaggerApiDescriptionProvider class I get an error on the constructor: "must declare a body because it is not marked abstract, extern, or partial".

Any suggestions?

Sounds like a compiler error. If I had to guess, you aren't using C# 7.0 [but should be ;)]. Just change the code from an _expression body_ to an actual body.

@commonsensesoftware I had figured out the I needed to change the code to an actual body. I have one other issue, no definition of Services within the IConfigurationBuilder (builder). Is this because I'm still using VS2015? Is there another solution for an earlier version of C#?

@commonsensesoftware I added:

services.TryAddEnumerable(ServiceDescriptor.Transient());

to the ConfigureServices method. I know see my comments!!!

But I still have an issue because the link to Swagger still fails.

Got same issue after updating from rc1 to rc3.

For me, parameter.ModelMetadata always not null so fix does not help.

I believe this was fixed some time ago so going to close it. If you're still experiencing this with the latest version (2.5.0 as of writing), feel free to re-open the issue (with clear repro steps)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brucewilkins picture brucewilkins  路  3Comments

flipchart picture flipchart  路  4Comments

rgelb picture rgelb  路  3Comments

michael-x picture michael-x  路  3Comments

TimmyGilissen picture TimmyGilissen  路  3Comments