vitalybibikov/AzureExtensions.Swashbuckle

"Failed to generate Schema for type -" when upgrading to 3.3.2

marc-lauper opened this issue · 1 comments

I've just spent a long time trying to upgrade an existing project from using version 3.0.2 to 3.3.2, and I thought I'd post the problem & solution here in case it helps other people, and in case you'd like to improve this directly in the library.

The problem
After upgrading AzureExtensions.Swashbuckle in a project with dozens of Rest APIs, the Swagger generation started failing with the following error:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Swagger
 ---> Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Failed to generate Operation for action - MyMethodName. See inner exception
 ---> Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorException: Failed to generate Schema for type - . See inner exception
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'key')
   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.TryGetCustomTypeMapping(Type type, Func`1& mapping)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository, MemberInfo memberInfo, ParameterInfo parameterInfo)
   --- End of inner exception stack trace ---
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository, MemberInfo memberInfo, ParameterInfo parameterInfo)
   at AzureFunctions.Extensions.Swashbuckle.SwashBuckle.Filters.QueryStringParameterAttributeFilter.Apply(OpenApiOperation operation, OperationFilterContext context)

The solution
Turns out this issue happened due to using [QueryStringParameter] annotations, like this one:

[QueryStringParameter("TimeZone", "The timezone associated to the user", Required = false)]

In order to fix it, I had to make sure the DataType associated to the annotation was explicitly defined. The two following changes turned out to be valid solutions:

[QueryStringParameter("TimeZone", "The timezone associated to the user", "Europe/Amsterdam", Required = false)]
[QueryStringParameter("TimeZone", "The timezone associated to the user", DataType  = typeof(string), Required = false)]

By the way, thanks a lot for writing this library, it's been a huge help. 😃