-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error "InvalidOperationException: Sequence contains more than one matching element" in Swagger when using minimal api with the header versioning #54623
Comments
I took a look at this and I think the issue is at the intersection of Asp.Versioning and some of the code that is used to merge For whatever reason, the Asp.Versioning configuration in the setup populates the
This seems buggy to mean and the bug is magnified by the There's two avenues to fix it:
Also, FWIW, this issue only manifests when cc: @martincostello @commonsensesoftware for any thoughts on this |
Update: I realized that the problem might be the fact that the
I bet this is duplicative with what Asp.Versioning is doing in its ApiExplorer overloads. Removing the |
@captainsafia is correct. @vdevc explicitly defining the API version with the If you want the requested API version passed into your action, you have two options. Option 1Enable binging the incoming API version to your endpoints with: builder.Services.AddApiVersioning().EnableApiVersionBinding(); Since model binders are not supported in Minimal APIs and .MapGet("/", (
[FromRoute] int customerId,
[FromServices] ILoggerFactory loggerFactory,
[FromServices] LinkGenerator linkGenerator,
ApiVersion apiVersion,
) =>
{
return new List<string> { "Business 1", "Business 2" };
}) Today, this works via DI and would be equivalent to Option 2The other option is to use the .MapGet("/", (
[FromRoute] int customerId,
[FromServices] ILoggerFactory loggerFactory,
[FromServices] LinkGenerator linkGenerator,
HttpContext context,
) =>
{
var apiVersion = context.GetRequestedApiVersion()!;
return new List<string> { "Business 1", "Business 2" };
}) Note that it is possible for Either approach will remove the duplicate |
@commonsensesoftware @captainsafia @commonsensesoftware |
Is there an existing issue for this?
Describe the bug
I am trying to use Asp.Versioning package with Header versioning together with minimal apis endpoints.
Every endpoint is like this example
Whenever I launch my project I get the following exception
Please note: this issue was initially reported in issue #53831 having the same effect within a similar context. It has now been splitted on a request from @captainsafia
Expected Behavior
Swagger should not throw the exception.
Steps To Reproduce
You can find a repro here: https://github.com/vdevc/FromHeaderBindingIssue
Exceptions (if any)
System.InvalidOperationException: Sequence contains more than one matching element
.NET Version
Verified with SDKs
8.0.100
,8.0.101
,8.0.201
,8.0.202
and8.0.203
. The behaviour is consistent between all the versions.Anything else?
No response
The text was updated successfully, but these errors were encountered: