Skip to content

Fix parameter matching to consider location when names collide - #4126

Merged
martincostello merged 2 commits into
domaindrivendev:masterfrom
nonsoikem:fix/4123-parameter-location-matching
Aug 26, 2026
Merged

martincostello merged 2 commits into
domaindrivendev:masterfrom
nonsoikem:fix/4123-parameter-location-matching

Conversation

@nonsoikem

Copy link
Copy Markdown
Contributor

The issue or feature being addressed

Fixes #4123

When using .WithOpenApi() on a minimal API endpoint that has two
parameters sharing the same name but bound to different locations
(e.g. [FromRoute] int id and [FromQuery(Name = "id")] string queryId),
Swagger generation fails with InvalidOperationException: Sequence contains more than one matching element.

Details on the issue fix or feature implementation

The SingleOrDefault call in GenerateOpenApiOperationFromMetadataAsync
was matching ApiParameterDescription entries by name only. When two
parameters share a name but differ in binding source, this throws.

The fix adds an additional filter: when the metadata OpenApiParameter
has its In property set, the candidate ApiParameterDescription must
also have a Source that maps to the same ParameterLocation via the
existing ParameterLocationMap. When In is null, the original
name-only matching is preserved for backward compatibility.

Two regression tests are included covering both cases (metadata specifies
In = Query and In = Path).

SingleOrDefault was matching OpenApiOperation metadata parameters
by name only, causing InvalidOperationException when two parameters
shared a name but differed in location (e.g. [FromRoute] and
[FromQuery] both named "id").

Now also matches by ParameterLocation via the existing
ParameterLocationMap. When In is not specified in the metadata
parameter, name-only matching is preserved for backward
compatibility.

Fixes domaindrivendev#4123
Comment thread src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs Outdated
@codecov

codecov Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.22%. Comparing base (0b99b04) to head (6b8b905).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4126      +/-   ##
==========================================
+ Coverage   95.21%   95.22%   +0.01%     
==========================================
  Files         111      111              
  Lines        4118     4128      +10     
  Branches      844      845       +1     
==========================================
+ Hits         3921     3931      +10     
  Misses        197      197              
Flag Coverage Δ
Linux 95.22% <100.00%> (+0.01%) ⬆️
Windows 95.22% <100.00%> (+0.01%) ⬆️
macOS 95.22% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@martincostello martincostello added this to the v10.2.4 milestone Aug 26, 2026

@martincostello martincostello left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@martincostello
martincostello merged commit 5ad4753 into domaindrivendev:master Aug 26, 2026
14 checks passed
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution @nonsoikem - the changes from this pull request have been published as part of version 10.3.0 📦, which is now available from NuGet.org 🚀

This was referenced Oct 7, 2026
This was referenced Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Swagger generation fails for WithOpenApi when route and query parameters share the same name

2 participants