Hi. Getting a NullReferenceException in SchemaGenerator.GenerateSchemaForMember when using AddSwaggerGenNewtonsoftSupport. I'm using version 6.4.0 of the following packages:
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.4.0" />
Here are repro steps (assumes you have .NET 6 SDK installed):
dotnet new webapi
dotnet add package Swashbuckle.AspNetCore (updates to 6.4.0)
dotnet add package Swashbuckle.AspNetCore.Newtonsoft (installs 6.4.0)
- Edit
Program.cs to add the line builder.Services.AddSwaggerGenNewtonsoftSupport();
- Edit
WeatherForecast.cs to add the line public Dictionary<DayOfWeek, DayOfWeek>? DayOfWeekMap { get; set; }
- Run the application which will launch a browser pointing to Swagger UI
- Observe a
NullReferenceException in SchemaGenerator.GenerateSchemaForMember
The exact pre-conditions to get this bug are:
- Need to use the
AddSwaggerGenNewtonsoftSupport
- Need to have a model that has a property with a
Dictionary type where the key is an Enum
I observed that if System.Text.Json is being used instead of Newtonsoft (essentially skipping step 4 above) then the issue does not exist since JsonSerializerDataContractResolver passes null for the keys parameter to DataContract.ForDictionary:
|
keys: null, // STJ doesn't currently support dictionaries with enum key types |
versus
NewtonsoftDataContractResolver which passes the set of
keys:
The Newtonsoft behavior (passing keys rather than keys being null) means that that the if branch in SchemaGenerator.CreateDictionarySchema is executed:
|
if (dataContract.DictionaryKeys != null) |
This results in an OpenApiSchema being created with AdditionalPropertiesAllowed set to false and AdditionalProperties left as null (the default). When control returns to SchemaGenerator.GenerateSchemaForMember this yields the NullReferenceException on line 95, since AdditionalProperties is null:
|
schema.AdditionalProperties.Nullable = !memberInfo.IsDictionaryValueNonNullable(); |
I believe the fix to be as simple as modifying line 93
|
if (modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(Dictionary<,>)) |
to be:
if (schema.AdditionalPropertiesAllowed && modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
that is adding the schema.AdditionalPropertiesAllowed conjunction.
I will submit a PR for this but wanted to create the issue first.
Thank you for building such a useful library.
Hi. Getting a
NullReferenceExceptioninSchemaGenerator.GenerateSchemaForMemberwhen usingAddSwaggerGenNewtonsoftSupport. I'm using version6.4.0of the following packages:Here are repro steps (assumes you have .NET 6 SDK installed):
dotnet new webapidotnet add package Swashbuckle.AspNetCore(updates to6.4.0)dotnet add package Swashbuckle.AspNetCore.Newtonsoft(installs6.4.0)Program.csto add the linebuilder.Services.AddSwaggerGenNewtonsoftSupport();WeatherForecast.csto add the linepublic Dictionary<DayOfWeek, DayOfWeek>? DayOfWeekMap { get; set; }NullReferenceExceptioninSchemaGenerator.GenerateSchemaForMemberThe exact pre-conditions to get this bug are:
AddSwaggerGenNewtonsoftSupportDictionarytype where the key is anEnumI observed that if
System.Text.Jsonis being used instead ofNewtonsoft(essentially skipping step 4 above) then the issue does not exist sinceJsonSerializerDataContractResolverpassesnullfor thekeysparameter toDataContract.ForDictionary:Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs
Line 64 in 4a28652
NewtonsoftDataContractResolverwhich passes the set ofkeys:Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs
Line 96 in 4a28652
The
Newtonsoftbehavior (passingkeysrather thankeysbeingnull) means that that theifbranch inSchemaGenerator.CreateDictionarySchemais executed:Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs
Line 318 in 4a28652
This results in an
OpenApiSchemabeing created withAdditionalPropertiesAllowedset tofalseandAdditionalPropertiesleft asnull(the default). When control returns toSchemaGenerator.GenerateSchemaForMemberthis yields theNullReferenceExceptionon line 95, sinceAdditionalPropertiesisnull:Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs
Line 95 in 4a28652
I believe the fix to be as simple as modifying line 93
Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs
Line 93 in 4a28652
to be:
if (schema.AdditionalPropertiesAllowed && modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(Dictionary<,>))that is adding the
schema.AdditionalPropertiesAllowedconjunction.I will submit a PR for this but wanted to create the issue first.
Thank you for building such a useful library.