domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.27k stars 1.32k forks source link

[Bug]: Sequence contains no matching element when accessing swagger.json #3187

Open moni-dips opened 7 hours ago

moni-dips commented 7 hours ago

Describe the bug

The changes in #3133 made swagger.json crash with an InvalidOperationException when I try to access swagger.json in one of our services.

This is due to the code:

bodyParameterDescription = apiDescription.ParameterDescriptions.Single(desc => desc.IsFromBody());
if (bodyParameterDescription is not null)

Since this is using .Single(), bodyParameterDescription would never be null, so it seems that this was intended to use SingleOrDefault instead.

Expected behavior

No crash on accessing swagger.json

Actual behavior

Crash on accessing swagger.json

Steps to reproduce

No response

Exception(s) (if any)

Sequence contains no matching element System.InvalidOperationException at System.Linq.ThrowHelper.ThrowNoMatchException() at System.Linq.Enumerable.Single[TSource](IEnumerable1 source, Func2 predicate) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOpenApiOperationFromMetadataAsync(ApiDescription apiDescription, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperationAsync(ApiDescription apiDescription, SchemaRepository schemaRepository, Func3 parametersGenerator, Func3 bodyGenerator, Func3 applyFilters) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperationAsync(ApiDescription apiDescription, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperationsAsync(IEnumerable\u00601 apiDescriptions, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePathsAsync(IEnumerable1 apiDescriptions, SchemaRepository schemaRepository, Func3 operationsGenerator) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePathsAsync(IEnumerable1 apiDescriptions, SchemaRepository schemaRepository) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerAsync(String documentName, String host, String basePath) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

Swashbuckle.AspNetCore version

7.1.0

.NET Version

8.0

Anything else?

No response

martincostello commented 7 hours ago

For clarity - this isn't a crash, it's an unhandled exception in an HTTP request.