Skip to content

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

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

Merged
Merged
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
16 changes: 14 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,21 @@ 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;
// Locate the corresponding route parameter metadata.
var routeParam = context.RouteParameters
.FirstOrDefault(rp => string.Equals(rp.Name, parameter.Name, StringComparison.OrdinalIgnoreCase));

// If the parameter is defined as a catch-all, mark it as optional.
if (routeParam != null && routeParam.IsCatchAll)
{
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,36 @@ namespace Microsoft.AspNetCore.Mvc.Description;

public class DefaultApiDescriptionProviderTest
{
[Fact]
public void OnlyCatchAllParameter_IsReportedAsOptional()
{
// Arrange: Create an action descriptor with a multi-parameter route template.
var action = CreateActionDescriptor();
action.AttributeRouteInfo = new AttributeRouteInfo
{
Template = "/products/{category}/items/{group}/inventory/{**any}"
};

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

// Assert: Only the 'any' parameter should be optional.
var description = Assert.Single(descriptions);
var categoryParameter = Assert.Single(description.ParameterDescriptions,
p => string.Equals(p.Name, "category", StringComparison.OrdinalIgnoreCase));
var groupParameter = Assert.Single(description.ParameterDescriptions,
p => string.Equals(p.Name, "group", StringComparison.OrdinalIgnoreCase));
var anyParameter = Assert.Single(description.ParameterDescriptions,
p => string.Equals(p.Name, "any", StringComparison.OrdinalIgnoreCase));

// The non-catch-all parameters should be required.
Assert.True(categoryParameter.IsRequired);
Assert.True(groupParameter.IsRequired);

// The catch-all parameter should be optional.
Assert.False(anyParameter.IsRequired);
}

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