unchase / Unchase.Swashbuckle.AspNetCore.Extensions

:hammer: A library contains a bunch of extensions (filters) for Swashbuckle.AspNetCore.
https://www.nuget.org/packages/Unchase.Swashbuckle.AspNetCore.Extensions
Apache License 2.0
116 stars 17 forks source link

MissingMethodException since Swashbuckle 6.0.x #15

Closed DaleyKD closed 3 years ago

DaleyKD commented 3 years ago

Just updated to Swashbuckle 6.0.1, and it seems like something may have changed on their end? I'm now getting an exception when trying to generate the .json file:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Failed to generate Operation for action - MyApp.Controllers.MyController.MyFunction(). See inner exception
       ---> Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Failed to generate schema for type - System.String. See inner exception
       ---> System.MissingMethodException: Method not found: 'System.Collections.Generic.IDictionary`2<System.String,Microsoft.OpenApi.Models.OpenApiSchema> Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository.get_Schemas()'.
         at Unchase.Swashbuckle.AspNetCore.Extensions.Filters.XEnumNamesSchemaFilter.Apply(OpenApiSchema schema, SchemaFilterContext context)
         at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.ApplyFilters(OpenApiSchema schema, Type type, SchemaRepository schemaRepository, MemberInfo memberInfo, ParameterInfo parameterInfo)
         at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchemaForParameter(ParameterInfo parameterInfo, SchemaRepository schemaRepository)
         at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository, MemberInfo memberInfo, ParameterInfo parameterInfo)
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository, PropertyInfo propertyInfo, ParameterInfo parameterInfo)
         --- End of inner exception stack trace ---
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository, PropertyInfo propertyInfo, ParameterInfo parameterInfo)
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateParameter(ApiParameterDescription apiParameter, SchemaRepository schemaRepository)
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.<>c__DisplayClass10_0.<GenerateParameters>b__1(ApiParameterDescription apiParam)
         at System.Linq.Enumerable.WhereSelectListIterator`2.ToList()
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateParameters(ApiDescription apiDescription, SchemaRepository schemaRespository)
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)
         --- End of inner exception stack trace ---
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
         at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath)
         at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
         at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
         at MyApp.Middleware.ErrorMiddleware.Invoke(HttpContext context) in C:\src\MyApp\Middleware\ErrorMiddleware.cs:line 27
         at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request.

I have to assume it's because of trying to access .SchemaRepository.Schemas? https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/blob/e11c200dc61f416792958edfb20d51ab1f53ae99/src/Unchase.Swashbuckle.AspNetCore.Extensions/Filters/XEnumNamesSchemaFilter.cs#L103

This is how I have my Swagger configured:

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", apiInfo);
                options.DescribeAllParametersInCamelCase();
                options.UseAllOfToExtendReferenceSchemas();

                var xmlFiles = Directory.GetFiles(AppContext.BaseDirectory, "*.xml", SearchOption.TopDirectoryOnly);
                foreach (var xmlFile in xmlFiles)
                {
                    options.IncludeXmlComments(xmlFile);
                }

                options.AddEnumsWithValuesFixFilters(services, o =>
                {
                    o.IncludeDescriptions = true;
                    o.DescriptionSource = DescriptionSources.DescriptionAttributesThenXmlComments;

                    foreach (var xmlFile in xmlFiles)
                    {
                        o.IncludeXmlCommentsFrom(xmlFile);
                    }
                });
            });

            services.AddSwaggerGenNewtonsoftSupport();

            return services;
DaleyKD commented 3 years ago

I'm going to double check ASAP, but it looks like https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v6.0.2 might have fixed the issue. Will report back.

DaleyKD commented 3 years ago

Confirmed. I am currently on version 6.0.3 and the error is now gone. You can close this if you want, or you may want to check your nuget deps.

unchase commented 3 years ago

Hi, @DaleyKD Yes, it was a problem with 6.0.1 version of Swashbuckle.AspNetCore.