Skip to content
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

Fix: Mark catch-all route parameters as optional (#60392) #60544

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Mvc/Mvc.ApiExplorer/src/DefaultApiDescriptionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,18 @@ internal static void ProcessIsRequired(ApiParameterContext context, MvcOptions m
parameter.IsRequired = true;
}

if (parameter.Source == BindingSource.Path && parameter.RouteInfo != null && !parameter.RouteInfo.IsOptional)
if (parameter.Source == BindingSource.Path && parameter.RouteInfo != null)
{
parameter.IsRequired = true;
// If the route template contains a catch-all parameter marker ("{**"), treat it as optional.
var template = context.ActionDescriptor.AttributeRouteInfo?.Template;
if (!string.IsNullOrEmpty(template) && template.Contains("{**"))
Copy link
Member

Choose a reason for hiding this comment

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

Does this effectively end up setting IsRequired = false for route parameters that appear in a route with a catch-all? For example, /products/{category}/items/{group}/inventory/{**any} would end up treating category and group as optional with this logic?

I wonder if there is a way we can use the ApiParameterContext.RouteParameters property to check if a route template part is a catch-all and only set it for the matching parameter source.

How does that sound?

{
parameter.IsRequired = false;
}
else if (!parameter.RouteInfo.IsOptional)
{
parameter.IsRequired = true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ namespace Microsoft.AspNetCore.Mvc.Description;

public class DefaultApiDescriptionProviderTest
{
[Fact]
public void CatchAllParameter_IsReportedAsOptional()
{
// Arrange: Create an action descriptor with a catch-all route template.
var action = CreateActionDescriptor();
action.AttributeRouteInfo = new AttributeRouteInfo { Template = "{**catchAllParameter}" };

// Act: Get the API descriptions using the existing helper.
var descriptions = GetApiDescriptions(action);

// Assert: There should be one description, with one parameter named "catchAllParameter"
// and its IsRequired property should be false.
var description = Assert.Single(descriptions);
var parameter = Assert.Single(description.ParameterDescriptions, p => p.Name == "catchAllParameter");
Assert.False(parameter.IsRequired);
}

[Fact]
public void GetApiDescription_IgnoresNonControllerActionDescriptor()
{
Expand Down
Loading