-
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
Removing from API Description parameters when inferred (FromPath) and not in the route #39607
Changes from all commits
d78f803
f9ab96e
cc4dae2
8cd9eeb
c87e360
3d1046a
80d36a8
d7084a8
31b85d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,14 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq; | ||
using Microsoft.AspNetCore.Http.Metadata; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.ActionConstraints; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Microsoft.AspNetCore.Mvc.Formatters; | ||
using Microsoft.AspNetCore.Mvc.Infrastructure; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.AspNetCore.Routing.Template; | ||
using Microsoft.Extensions.Options; | ||
|
@@ -239,11 +241,13 @@ private void ProcessRouteParameters(ApiParameterContext context) | |
routeParameters.Add(routeParameter.Name!, CreateRouteInfo(routeParameter)); | ||
} | ||
|
||
foreach (var parameter in context.Results) | ||
for (var i = context.Results.Count - 1; i >= 0; i--) | ||
{ | ||
var parameter = context.Results[i]; | ||
|
||
if (parameter.Source == BindingSource.Path || | ||
parameter.Source == BindingSource.ModelBinding || | ||
parameter.Source == BindingSource.Custom) | ||
parameter.Source == BindingSource.ModelBinding || | ||
parameter.Source == BindingSource.Custom) | ||
{ | ||
if (routeParameters.TryGetValue(parameter.Name, out var routeInfo)) | ||
{ | ||
|
@@ -258,6 +262,20 @@ private void ProcessRouteParameters(ApiParameterContext context) | |
parameter.Source = BindingSource.Path; | ||
} | ||
} | ||
else | ||
{ | ||
if (parameter.Source == BindingSource.Path && | ||
parameter.ModelMetadata is DefaultModelMetadata defaultModelMetadata && | ||
!defaultModelMetadata.Attributes.Attributes.OfType<IFromRouteMetadata>().Any()) | ||
{ | ||
// If we didn't see the parameter in the route and no FromRoute metadata is set, it probably means | ||
// the parameter binding source was inferred (InferParameterBindingInfoConvention) | ||
// probably because another route to this action contains it as route parameter and | ||
// will be removed from the API description | ||
// https://github.com/dotnet/aspnetcore/issues/26234 | ||
context.Results.RemoveAt(i); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this situation only occur for parameters with default values? Given the scenario in the issue, [HttpGet]
[Route("foo")]
[Route("bar/{id}")]
public IActionResult Get(int id = 0)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not because we stamp in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to include an example: #39607 (comment) |
||
} | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
else if
?